java web界面下载文件

参考:https://bbs.csdn.net/topics/392094166 

摘自:https://www.cnblogs.com/linkworld/p/7636734.html

 

1. 下载概述

  • 下载就是向客户端响应字节数据! 将一个文件变成字节数组, 使用 response.getOutputStream()
    来响应给浏览器!!

2. 下载要求

  1. 两个头一个流
    • Content-Type: 传递给客户端的文件的 MIME 类型;
      可以使用文件名称调用 ServletContext 的 getMimeType() 方法, 得到 MIME 类型!
    • Content-Disposition:attachment;filename=xxx:
      它的默认值为 inline, 表示在浏览器窗口中打开! attachment 表示附件, filname 后面跟随的是显示在
      下载框中的文件名称!
    • 流:要下载的文件数据!
    • 下载文件编码问题 显示在下载框中为中文名称时,会出现乱码

      • FireFox: Base64 编码;
      • 其他大部分浏览器: URL 编码. 通用方案: filename = new String(filename.getBytes("GBK"),"ISO-88859-1");

public void DownLoad()throws ParseException{
        response.setContentType("text/html;charset=utf-8");  
        OutputStream os = null;
        
        try {
            //参数

            //解决中文乱码
            String recordId = new String(request.getParameter("recordId").getBytes("ISO-8859-1"),"UTF-8");
            String fileName = new String(request.getParameter("fileName").getBytes("ISO-8859-1"),"UTF-8");


          //获取文件存储路径  
            String path = getClass().getProtectionDomain().getCodeSource()
                    .getLocation().getPath();
            path = path.substring(1, path.indexOf("WEB-INF/classes")) + "files/recordfile/" + recordId;
            
            path = path + "/record/" + fileName;
            File file = new File(path);
            // 读取文件
            InputStream fis = new BufferedInputStream(new FileInputStream(
                    path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
 
            // 设置下载文件名等
            response.setCharacterEncoding("utf-8");


            //new String(fileName.getBytes("GBK"),"ISO-88859-1");解决浏览器界面显示文件名中文乱码问题


            String showFileName = new String(fileName.replaceAll(" ", "").getBytes("GBK"),"ISO-8859-1");
            response.addHeader("Content-Disposition", "attachment;filename="
                    + showFileName);
            
            response.addHeader("Content-Length", "" + file.length());
         

            os = new BufferedOutputStream(response.getOutputStream());
            os.write(buffer);// 输出文件
            os.flush();
 
            response.flushBuffer();
 
        } catch (Exception e) {
            if (os != null) {
                    os.flush();
            }
        } finally {
            if (os != null) {
                    os.close();
            }
        }
 
    }

 

onDblClickRow:function(rowIndex,rowData){
                            var recordId = rowData.recordId;
                            var fileName = rowData.fileName;
                            var urlEndajax = "recordAction!DownLoad.action?recordId="+recordId+"&fileName="+fileName;
                            urlEndajax = convertURL(urlEndajax);
                            $("#downLoad").html("<iframe width='100%' height='100%' src='" + encodeURI(urlEndajax) + "' />");

                        }});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值