File 浏览器文件下载不适用于存储在服务器上的文件
我在服务器上有一个文件,该文件将使用浏览器下载。没有抛出错误,但没有显示文件下载的下载窗口。 请帮忙 我的控制器代码是:File 浏览器文件下载不适用于存储在服务器上的文件,file,spring-mvc,browser,controller,force-download,File,Spring Mvc,Browser,Controller,Force Download,我在服务器上有一个文件,该文件将使用浏览器下载。没有抛出错误,但没有显示文件下载的下载窗口。 请帮忙 我的控制器代码是: @RequestMapping(value = "download", method = RequestMethod.GET) public void downloadFile( @ModelAttribute("path") final String path, HttpServletRequest request, HttpServletRes
@RequestMapping(value = "download", method = RequestMethod.GET)
public void downloadFile(
@ModelAttribute("path") final String path, HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
logger.error("inside download get try");
ServletContext context = request.getServletContext();
File downloadFile = new File(path);
FileInputStream inputStream = new FileInputStream(downloadFile);
// get MIME type of the file
String mimeType = context.getMimeType(path);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
// set headers for the response
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
downloadFile.getName());
response.setHeader(headerKey, headerValue);
// get output stream of the response
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
// write bytes read from the input stream into the output stream
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
} catch (Exception e) {
logger.error("inside download get catch");
throw new IndiciumException("Can't download the audit file");
}
我在firebug中的反应如下:
%PDF-1.4
%����
5 0 obj
<</Filter/FlateDecode/Length 502>>stream
x��T]o�0}�W���!��?��F�eʤ�k���4x]�����ʿ�
Nł��Ea||���^�Mt�cHi��PpBc��m��F�#
��h�?N����a~��3��=,r}�_����J8M��*u��[q(q�$U)�?��|o�:�|
�P�T�
/�Ƭ���|��m��~\_��X�*���m98 s��_��+�c��ut�77��v��ݻ��(M
�V�K���@�m�w��=�HS����(Х-�8���ն
ܮJ�g��a1������M�Ƿ��3E��&Ǧ!��$a|ܩ�вb���$цT�kSׁ8�m�>��
�0E(����|�BJb �?����f2.3�8�'�sbϰLި�O9ˎ�
endstream
endobj
7 0 obj
<</Contents 5 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC
/ImageI]/Font<</F1 1 0 R
/F2 2 0 R/F3 3 0 R/F4 4 0 R>>>>/Parent 6 0 R/MediaBox[0 0 595 842]>>
endobj
1 0 obj
<</Subtype/Type1/Type/Font/BaseFont/Times-Bold/Encoding/WinAnsiEncoding>>
endobj
2 0 obj
<</Subtype/Type1/Type/Font/BaseFont/Times-Italic/Encoding/WinAnsiEncoding>>
endobj
3 0 obj
<</Subtype/Type1/Type/Font/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>
endobj
4 0 obj
<</Subtype/Type1/Type/Font/BaseFont/Times-Roman/Encoding/WinAnsiEncoding>>
endobj
6 0 obj
<</Kids[7 0 R]/Type/Pages/Count 1/ITXT(5.0.6)>>
endobj
8 0 obj
<</Type/Catalog/Pages 6 0 R>>
endobj
9 0 obj
1T3XT BVBA)>>
endobj
xref
0 10
0000000000 65535 f
0000000768 00000 n
0000000857 00000 n
trailer
<</Info 9 0 R/ID [<1b6717a8f36527b1db89d35fc22e9da5>
<c2e3cf9ec60f1d29ea766dc>]/Root 8 0 R/Size
10>>
startxref
1364
%%EOF
从我的头顶:
似乎您的代码正确地写入了响应的输出流,所以您可能只需要在完成编写后调用flush。不久前我也遇到过类似的情况 您是否尝试在调试器中单步执行服务器端代码?浏览器的网络流量显示了什么?响应的类型/标题/有效负载是什么?响应标题和响应与浏览器控制台中的预期相同。我这样做了,但仍然不起作用:请有人帮助…即使调试也不会引发任何错误:'-您可以检查以下几点:1。如果有任何东西被写入到输出流中;2.如果不关闭输出流会发生什么,那么无论如何都不应该这样做;您好,输出流已按预期获得数据,如果我不关闭输出流,则不会发生任何事情,但其行为方式相同。我仍然在挣扎:谢谢你的回复。经过所有的研究,我发现我的代码是正确的,spring配置有问题,或者我缺少了一些.jar。有人能帮帮忙吗?
Cache-Control
must-revalidate, post-check=0, pre-check=0
Content-Disposition
attachment; filename = exportpdf1432033492.pdf
Content-Type
application/pdf
Date
Tue, 19 May 2015 11:04:52 GMT
Expires
0
Pragma
no-cache
Server
Apache-Coyote/1.1
Transfer-Encoding
chunked
X-Frame-Options
DENY
X-XSS-Protection
1; mode=block
x-content-type-options
nosniff