1

我在服务器上有一个文件,要使用浏览器下载来下载。没有引发错误,但没有出现文件下载的下载窗口。请帮忙。

我的控制器代码是:

  @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");
    }

我在萤火虫中的反应是:

%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�&gt;��
�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

响应头是:

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
4

1 回答 1

0

从我的头顶:

看起来您的代码正在正确地写入响应的输出流,所以也许您只需要在完成写入后调用它即可。前段时间我也遇到过类似的情况。

于 2015-05-18T14:24:51.900 回答