不工作的文件浏览器下载文件存储在服务器上 [英] Browser file download not working for a file stored on server

查看:302
本文介绍了不工作的文件浏览器下载文件存储在服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对服务器文件,该文件是使用浏览器的下载下载。被抛出错误,但出现在文件下载不下载窗口。
请帮助。

我的控制器code是:

  @RequestMapping(值=下载,方法= RequestMethod.GET)
   公共无效downloadFile(
        @ModelAttribute(路径)最终字符串路径,HttpServletRequest的请求,HttpServletResponse的响应)抛出异常{
    尝试{        logger.error(内下载获取尝试);
        ServletContext的上下文= request.getServletContext();
        文件downloadFile =新的文件(路径);
        的FileInputStream的InputStream =新的FileInputStream(downloadFile);
        //取得MIME类型的文件
        字符串mime类型= context.getMimeType(路径);
        如果(mime类型== NULL){
            //设置为二进制类型,如果没有找到MIME映射
            mime类型=应用程序/八位字节流;
        }
        response.setContentType(mime类型);
        response.setContentLength((int)的downloadFile.length());
        //为响应集头
        字符串headerKey =内容处置;
        字符串headerValue =的String.format(附件;文件名= \\%s \\的
                downloadFile.getName());
        response.setHeader(headerKey,headerValue);
        //获取响应的输出流
        的OutputStream outStream = response.getOutputStream();
        字节[]缓冲区=新的字节[BUFFER_SIZE];
        INT读取动作= -1;
        //从输入流中读取到输出流中写入的字节
        而((读取动作= inputStream.read(缓冲液))!= - 1){
            outStream.write(缓冲液,0,读取动作);
        }        inputStream.close();
        outStream.close();    }赶上(例外五){
        logger.error(内下载获取捕);
        抛出新IndiciumException(无法下载审计文件);
    }

我在萤火虫的反应是这样的:

 %PDF-1.4

5 0 OBJ
<< /过滤/ FlateDe code /长度502 GT;>流
XT] O0} W!?Feʤk4x]'
NLEA || ^ MtcHiPpBcmF#
h ?N a~ 3 =,r} _ J8M *u [q(q $U) ? |o : |
PT
/Ƭ| M〜\\ _X * M98 S_ + cut77vݻ(M
VK @ MW = HS(Х-8ն
ܮJ g a1 M Ƿ 3E &Ǧ! $a|ܩ вb $цT kSׁ8 m >
0E(|?BJBf2.38'sbκLިO9
endstream
endobj
7 0 OBJ
<< /目录5 0 R /类型/页/资源与LT;< / ProcSet [/ PDF /文字/ ImageB / ImageC
/ ImageI] /字体<< / F1 1 0 R
/ F2 2 0 R / F3 3 0 R / F4 4 0 R>>>> /家长6 0 R /多媒体[0 0 595 842] GT;>
endobj
1 0 OBJ
<< /亚型/类型1 /类型/字体/ BASEFONT /时代粗体/编码/ WinAnsiEncoding>>
endobj
2 0 OBJ
<< /亚型/类型1 /类型/字体/ BASEFONT / Times斜体/编码/ WinAnsiEncoding>>
endobj
3 0 OBJ
<< /亚型/类型1 /类型/字体/ BASEFONT /黑体/编码/ WinAnsiEncoding>>
endobj
4 0 OBJ
<< /亚型/类型1 /类型/字体/ BASEFONT /时代 - 罗马/编码/ WinAnsiEncoding>>
endobj
6 0 OBJ
<< /童装[7 0 R] /类型/页/共1 / ITXT(5.0.6)GT;>
endobj
8 0 OBJ
<< /类型/目录/页6 0 R>>
endobj
9 0 OBJ
1T3XT BVBA)GT;>
endobj
外部参照
0 10
0000000000 65535˚F
0000000768 00000ñ
0000000857 00000ñ
拖车
<< /信息9 0 R / ID [其中p 1b6717a8f36527b1db89d35fc22e9da5>
< c2e3cf9ec60f1d29ea766dc>] /根8 0 R /尺寸
10 -10;>
startxref
1364
%% EOF

和响应标题是:

 的Cache-Control
必重新验证,检查后= 0,pre-检查= 0
内容处置
附件;文件名= exportpdf1432033492.pdf
内容类型
应用程序/ PDF
日期
星期二,2015年11点04分52秒格林尼治标准​​时间5月19日
过期
0
编译
无缓存
服务器
Apache的狼/ 1.1
传输编码
分块
X框,选项
拒绝
的X XSS防护
1;模式=块
的x内容类型的选项
nosniff


解决方案

从我的头顶:

看来你的code正确写入响应的输出流中,所以也许你只需要当你写完调用的刷新()就可以了。我是在一个类似的情况,前一段时间。

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

解决方案

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.

这篇关于不工作的文件浏览器下载文件存储在服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆