今天给大家带来springboot获取resources目录下文件的常用方法,示例中的方法是读取resources目录下的txt和xlsx文件,并将xlsx导出到excel的简单写法。完整代码放在最后。
通过this.getclass()方法获取
method1 - method4都是通过这个方法获取文件的写法,这四种写法在idea中都可以正常运行,jar包执行后method1和method2报错,提示找不到文件,method3和method4可以正常运行








通过classpathresource获取
method5是通过这种方法实现,idea中可以正常运行,打包后的jar中提示找不到文件


通过hutool工具类resourceutil获取
method6是通过这种方法实现,和method情况一样,同样是idea中可以正常运行,导出的jar中提示找不到文件


总结
不想折腾的同学可以直接用method3和method4的方法来使用,也可以将模板和资源文件外置,通过绝对路径获取对应文件。有好的方法也欢迎大家一起交流沟通~
代码
import cn.hutool.core.io.fileutil;
import cn.hutool.core.io.resource.classpathresource;
import cn.hutool.core.io.resource.resourceutil;
import com.alibaba.excel.easyexcel;
import com.alibaba.excel.excelwriter;
import com.alibaba.excel.enums.writedirectionenum;
import com.alibaba.excel.write.metadata.writesheet;
import com.alibaba.excel.write.metadata.fill.fillconfig;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import javax.servlet.http.httpservletresponse;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.unsupportedencodingexception;
import java.net.urlencoder;
import java.nio.charset.standardcharsets;
import java.util.list;
@restcontroller
@requestmapping("/temp")
public class templatecontroller {
/**
* this.getclass()方法获取
* @param response
* @throws ioexception
*/
@requestmapping("/method1")
public void method1(httpservletresponse response) throws ioexception {
system.out.println("----------method1 start");
string filename = "template.xlsx";
string bashpatch = this.getclass().getclassloader().getresource("").getpath();
system.out.println(bashpatch);
string textfile = "template.txt";
string textpath = this.getclass().getclassloader().getresource("").getpath();
list<string> datalist = fileutil.readutf8lines(textpath + "/template/" + textfile);
for (string data : datalist) {
system.out.println(data);
}
try (excelwriter excelwriter =
easyexcel.write(response.getoutputstream())
.autoclosestream(false) // 不要自动关闭,交给 servlet 自己处理
// .withtemplate(resource.getfile().getabsolutepath())
.withtemplate(bashpatch + "/template/" + filename)
.build()
) {
writesheet writesheet = easyexcel.writersheet(0).build();
fillconfig userfillconfig = fillconfig.builder().forcenewrow(boolean.true).build();
fillconfig titlefillconfig = fillconfig.builder().direction(writedirectionenum.horizontal).build();
excelwriter.finish();
}
try {
response.addheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, standardcharsets.utf_8.name()));
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
}
@requestmapping("/method2")
public void method2(httpservletresponse response) throws ioexception {
system.out.println("----------method2 start");
string filename = "template.xlsx";
string bashpatch = this.getclass().getclassloader().getresource("template").getpath();
system.out.println(bashpatch);
string textfile = "template.txt";
string textpath = this.getclass().getclassloader().getresource("template").getpath();
list<string> datalist = fileutil.readutf8lines(textpath + "/" + textfile);
for (string data : datalist) {
system.out.println(data);
}
try (excelwriter excelwriter =
easyexcel.write(response.getoutputstream())
.autoclosestream(false) // 不要自动关闭,交给 servlet 自己处理
// .withtemplate(resource.getfile().getabsolutepath())
.withtemplate(bashpatch + "/" + filename)
.build()
) {
writesheet writesheet = easyexcel.writersheet(0).build();
fillconfig userfillconfig = fillconfig.builder().forcenewrow(boolean.true).build();
fillconfig titlefillconfig = fillconfig.builder().direction(writedirectionenum.horizontal).build();
excelwriter.finish();
}
try {
response.addheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, standardcharsets.utf_8.name()));
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
}
@requestmapping("/method3")
public void method3(httpservletresponse response) throws ioexception {
system.out.println("----------method3 start");
string filename = "template.xlsx";
inputstream inputstream = this.getclass().getclassloader().getresourceasstream("template" + "/" + filename);
// system.out.println(inputstream);
string textfile = "template.txt";
inputstream textstream = this.getclass().getclassloader().getresourceasstream("template" + "/" + textfile);
bufferedreader reader = new bufferedreader(new inputstreamreader(textstream));
string line;
try {
while ((line = reader.readline()) != null) {
system.out.println(line);
}
} catch (ioexception e) {
e.printstacktrace(); // 异常处理
}
try (excelwriter excelwriter =
easyexcel.write(response.getoutputstream())
.autoclosestream(false) // 不要自动关闭,交给 servlet 自己处理
// .withtemplate(resource.getfile().getabsolutepath())
.withtemplate(inputstream)
.build()
) {
writesheet writesheet = easyexcel.writersheet(0).build();
fillconfig userfillconfig = fillconfig.builder().forcenewrow(boolean.true).build();
fillconfig titlefillconfig = fillconfig.builder().direction(writedirectionenum.horizontal).build();
excelwriter.finish();
}
try {
response.addheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, standardcharsets.utf_8.name()));
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
}
@requestmapping("/method4")
public void method4(httpservletresponse response) throws ioexception {
system.out.println("----------method4 start");
string filename = "template.xlsx";
inputstream inputstream = this.getclass().getresourceasstream("/template" + "/" + filename);
// system.out.println(inputstream);
string textfile = "template.txt";
inputstream textstream = this.getclass().getresourceasstream("/template" + "/" + textfile);
bufferedreader reader = new bufferedreader(new inputstreamreader(textstream));
string line;
try {
while ((line = reader.readline()) != null) {
system.out.println(line);
}
} catch (ioexception e) {
e.printstacktrace(); // 异常处理
}
try (excelwriter excelwriter =
easyexcel.write(response.getoutputstream())
.autoclosestream(false) // 不要自动关闭,交给 servlet 自己处理
// .withtemplate(resource.getfile().getabsolutepath())
.withtemplate(inputstream)
.build()
) {
writesheet writesheet = easyexcel.writersheet(0).build();
fillconfig userfillconfig = fillconfig.builder().forcenewrow(boolean.true).build();
fillconfig titlefillconfig = fillconfig.builder().direction(writedirectionenum.horizontal).build();
excelwriter.finish();
}
try {
response.addheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, standardcharsets.utf_8.name()));
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
}
/**
* 通过classpathresource获取
* @param response
* @throws ioexception
*/
@requestmapping("/method5")
public void method5(httpservletresponse response) throws ioexception {
system.out.println("----------method5 start");
string filename = "template.xlsx";
classpathresource classpathresource = new classpathresource("template" + "/" + filename);
string textfile = "template.txt";
classpathresource textresource = new classpathresource("template" + "/" + textfile);
list<string> datalist = fileutil.readutf8lines(textresource.getabsolutepath());
for (string data : datalist) {
system.out.println(data);
}
try (excelwriter excelwriter =
easyexcel.write(response.getoutputstream())
.autoclosestream(false) // 不要自动关闭,交给 servlet 自己处理
.withtemplate(classpathresource.getabsolutepath())
.build()
) {
writesheet writesheet = easyexcel.writersheet(0).build();
fillconfig userfillconfig = fillconfig.builder().forcenewrow(boolean.true).build();
fillconfig titlefillconfig = fillconfig.builder().direction(writedirectionenum.horizontal).build();
excelwriter.finish();
}
try {
response.addheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, standardcharsets.utf_8.name()));
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
}
/**
* 通过hutool工具类resourceutil获取
* @param response
* @throws ioexception
*/
@requestmapping("/method6")
public void method6(httpservletresponse response) throws ioexception {
system.out.println("----------method6 start");
string filename = "template.xlsx";
string filepath = resourceutil.getresource("template" + "/" + filename).getpath();
string textfile = "template.txt";
string textpath = resourceutil.getresource("template" + "/" + textfile).getpath();
list<string> datalist = fileutil.readutf8lines(textpath);
for (string data : datalist) {
system.out.println(data);
}
try (excelwriter excelwriter =
easyexcel.write(response.getoutputstream())
.autoclosestream(false) // 不要自动关闭,交给 servlet 自己处理
.withtemplate(filepath)
.build()
) {
writesheet writesheet = easyexcel.writersheet(0).build();
fillconfig userfillconfig = fillconfig.builder().forcenewrow(boolean.true).build();
fillconfig titlefillconfig = fillconfig.builder().direction(writedirectionenum.horizontal).build();
excelwriter.finish();
}
try {
response.addheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, standardcharsets.utf_8.name()));
} catch (unsupportedencodingexception e) {
throw new runtimeexception(e);
}
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
}
}
pom依赖
<dependency>
<groupid>cn.hutool</groupid>
<artifactid>hutool-all</artifactid>
<version>5.8.9</version>
</dependency>
<dependency>
<groupid>com.alibaba</groupid>
<artifactid>easyexcel</artifactid>
<version>3.3.3</version>
</dependency>到此这篇关于springboot下获取resources目录下文件的常用方法的文章就介绍到这了,更多相关springboot获取resources目录下文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论