简体   繁体   English

奇怪的servlet响应问题

[英]Strange servlet response issue

I have a servlet witch is generating a .xls file and then it's sending the generated file to the user for download with the code below: 我有一个servlet女巫正在生成.xls文件,然后将生成的文件发送给用户,并使用以下代码下载:

// Write the output to a file
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= 
new SimpleDateFormat("yyyy_MMM_dd");
String dateNow = formatter.format(currentDate.getTime());

String path = "webapps/myapp/exports/";
String fileName = ("Table_export_"+ dateNow + ".xls");

FileOutputStream fileOut = new FileOutputStream(path+fileName);
wb.write(fileOut);
fileOut.close();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName);

The file is saved on the server with a size of 5 kb, but after the browser dialog and after choosing save or open, the file is empty and the size is 0 kb. 该文件以5 kb的大小保存在服务器上,但是在浏览器对话框中,选择“保存”或“打开”后,该文件为空,大小为0 kb。 I can not understand what is wrong. 我不明白什么是错的。

I'm fighting with this issue for about 2 hours without a success. 我为此问题奋战了大约2个小时,但没有成功。 I have tried to set the full path: 我试图设置完整路径:

response.setHeader("Content-Disposition","attachment;filename="path+fileName);

But I got a document with a strange name and it also was 0 kb. 但是我得到的文件名很奇怪,它也是0 kb。

I'm pretty sure that I'm missing something really small, but as a junior developer I still can not figure it out. 我很确定我缺少一些很小的东西,但是作为一个初级开发人员,我仍然无法弄清楚。

Since you already have a method that can write your file to an output stream you can simply change your code this way to send it to the user : 由于您已经有了一种可以将文件写入输出流的方法,因此您可以简单地通过以下方式更改代码以将其发送给用户:

Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MMM_dd");
String dateNow = formatter.format(currentDate.getTime());

String fileName = ("Table_export_"+ dateNow + ".xls");

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName);

OutputStream outputStream = response.getOutputStream();
try {
    wb.write(outputStream);
} finally {
    outputStream.close();
}

It won't be saved on your server HDD though. 但是,它不会保存在服务器硬盘上。

The method HttpServletResponse#getOutputStream() allows you to access the stream of bytes that will be sent back to the client. 方法HttpServletResponse#getOutputStream()允许您访问将发送回客户端的字节流。

When you used the format - 使用格式时-

String path = "webapps/myapp/exports/";
String fileName = ("Table_export_"+ dateNow + ".xls");
...
response.setHeader("Content-Disposition","attachment;filename="path+fileName);

Was the filename "webapps%2Fmyapps%2Fexports%2fTable_export_....xls"? 文件名是否为“ webapps%2Fmyapps%2Fexports%2fTable_export _.... xls”?

Als the filesize would have been zero because you are not putting any data in the file Als文件大小将为零,因为您没有在文件中放入任何数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM