文件下载

指定<a>元素的download属性,该属性为文件被下载时的文件名

代码如下:

<a href="<%=path %>/upload/test.doc" download="测试文档">下载文档</a>
    
    <form action="testUpload" method="post" enctype="multipart/form-data">
        <input type="hidden" name="fileName" value="logo.png">
        <button>下载图片</button>
    </form>

 

创建一个InputStream到文件用于下载

查找MIME类型下载文件的内容

可以是application/pdf,text/html,application/xml,image/png等等

将内容类型与上述发现的MIME类型响应(HttpServletResponse)

respnse.setContentType(mimeType);

针对以上找到MIME类型设置内容长度

response.setContentLength(file..getLength());//length in bytes

为响应设置内容处理标头

response.setHeader("Content-Disposition","attachment;filename="+fileName);//随着附件文件将下载,可能会显示一个另存为基于浏览器的设置对话框

response.setHeader("Content-Disposition","inline;filename="+fileName);//通过内联浏览器将尝试显示内容到浏览器中(图片,PDF,文本....),对于其他内容类型,文件将直接下载

从InputStream中复制字节响应到OutputStream,一旦复制完成后,关闭输入输出流

代码:

 @RequestMapping("/download")
        public String downloadFile(@RequestParam("fileName") String fileName,
                HttpServletRequest request, HttpServletResponse response) {
            if (fileName != null) {
                String realPath = request.getServletContext().getRealPath(
                        "/WEB-INF/File/");
                File file = new File(realPath, fileName);
                if (file.exists()) {
                    response.setContentType("application/force-download");// 设置强制下载不打开                
                    response.setHeader("Content-Disposition","attachment; filename="+ fileName); 
                    byte[] buffer = new byte[1024];
                    FileInputStream fis = null;
                    BufferedInputStream bis = null;
                    try {
                        fis = new FileInputStream(file);
                        bis = new BufferedInputStream(fis);
                        OutputStream os = response.getOutputStream();
                        int i = bis.read(buffer);
                        while (i != -1) {
                            os.write(buffer, 0, i);
                            i = bis.read(buffer);
                        }
                    } catch (Exception e) {                       
                        e.printStackTrace();
                    } finally {
                        if (bis != null) {
                            try {
                                bis.close();
                            } catch (IOException e) {                               
                                e.printStackTrace();
                            }
                        }
                        if (fis != null) {
                            try {
                                fis.close();
                            } catch (IOException e) {                               
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            return null;
        }

 

posted @ 2018-03-14 16:04  specialangel  阅读(126)  评论(0)    收藏  举报