Browser file download not working for a file store

2019-08-08 18:04发布

I have a file on server which is to be downloaded using the browser download. No error is being thrown but no download window appears for the file download. Please help.

My controller code is:

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

My response in the firebug is like:

%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

And the response header is:

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

1条回答
【Aperson】
2楼-- · 2019-08-08 18:39

From the top of my head:

It seems your code is writing properly to response's output stream so maybe you just need to call flush() on it when your done writing. I was in a similar situation some time ago.

查看更多
登录 后发表回答