Java REST-为什么生成的excel文件在尝试作为web应用程序时没有实际创建?
我创建了一个简单的Java REST-为什么生成的excel文件在尝试作为web应用程序时没有实际创建?,java,excel,rest,path,jax-rs,Java,Excel,Rest,Path,Jax Rs,我创建了一个简单的restweb应用程序,它将创建Excel文件,并放入当前目录。 但不知何故,生成的excel不在目录中。事实上,它甚至没有被创造出来 输出“done”显示在GlassFish服务器日志中,因此过程实际上结束时没有任何错误。我唯一怀疑的是我为“MyExcelFileThatwouldnotShow”指定的文件路径 我给出了我能想到的完整路径或相对路径,但Excel文件并没有显示出来。 有趣的是,如果我不以web应用程序的形式运行它(即将代码放在local main()函数中并运
restweb应用程序
,它将创建Excel文件
,并放入当前目录。
但不知何故,生成的excel不在目录中。事实上,它甚至没有被创造出来
输出“done”
显示在GlassFish服务器日志中,因此过程实际上结束时没有任何错误。我唯一怀疑的是我为“MyExcelFileThatwouldnotShow”指定的文件路径
我给出了我能想到的完整路径或相对路径,但Excel文件并没有显示出来。
有趣的是,如果我不以web应用程序的形式运行它(即将代码放在local main()函数中并运行它,它就会工作)
因此,我认为这与玻璃鱼有关,但无法真正理解:(
- 玻璃鱼v3
- REST/JAX-RS
- 用于从模板(myTemplate.xls)生成Excel电子表格的Excel框架
代码片段
@Path("horizontalProcess")
@GET
@Produces("application/xml")
public String getProcessHorizontally() {
try {
URL templateFileUrl = this.getClass().getResource("myTemplate.xls");
// getPath() outputs...
// /C:/Users/m-takayashiki/Documents/NetBeansProjects/KogaAlpha/build/web/WEB-INF/classes/local/test/jaxrs/myTemplate.xls
System.out.println(templateFileUrl.getPath());
String templateFilePath = URLDecoder.decode(templateFileUrl.getPath(), "UTF-8");
//specify output path which is current dir and should create
//myExcelFileThatWouldNotShowup.xls but it is not..
String outputFileDir = "myExcelFileThatWouldNotShowUp";
//<<template path>>, <<output path>>, <<file format>>
ReportBook outputBook = new ReportBook(templateFilePath, outputFileDir, ExcelExporter.FORMAT_TYPE);
ReportSheet outputSheet = new ReportSheet("myExcelSheet");
outputBook.addReportSheet(outputSheet);
//this is printed out so process actually gets here
System.out.println("done!!");
}
catch(Exception e) {
System.out.println(e);
}
return null;
}//end method
@Path(“水平过程”)
@得到
@生成(“应用程序/xml”)
公共字符串getProcessHandle(){
试一试{
URL templateFileUrl=this.getClass().getResource(“myTemplate.xls”);
//getPath()输出。。。
///C:/Users/m-takayashiki/Documents/NetBeansProjects/KogaAlpha/build/web/web-INF/classes/local/test/jaxrs/myTemplate.xls
System.out.println(templateFileUrl.getPath());
字符串templateFilePath=URLDecoder.decode(templateFileUrl.getPath(),“UTF-8”);
//指定输出路径,该路径为当前目录并应创建
//MyExcelf不会显示。xls但它不是。。
String outputFileDir=“myExcelFileThatWouldNotShowUp”;
//, ,
ReportBook outputBook=新的ReportBook(templateFilePath、outputFileDir、ExcelExporter.FORMAT_TYPE);
ReportSheet outputSheet=新的ReportSheet(“myExcelSheet”);
outputBook.addReportSheet(outputSheet);
//这是打印出来的,所以过程实际上到了这里
System.out.println(“完成!!”;
}
捕获(例外e){
系统输出打印ln(e);
}
返回null;
}//结束方法
这就是为什么一个人不应该在累了/加班的时候编码
我忘了在最后添加两行代码,实际上生成了excel。。。
花费了我几个小时的调试…(不要问我是如何调试的:p
ReportProcessor reportProcessor = new ReportProcessor();
reportProcessor.process(outputBook);
顺便说一句,如果您不指定,生成的文件将默认存储在下面的目录中
//C:\Users\m-takayashiki\.netbeans\6.9\config\GF3\domain1