IO流下载文件,浏览器无响应

使用io流实现下载文件,运行时打断点发现产生了流,但是浏览器并没有下载文件。
1.设置response的头文件
response.addHeader(“Content-disposition”, “attachment; filename=” +fileName);
其中Content-disposition设置表示浏览器自动下载。
后来在网上查询有说法是使用io流下载文件,不能通过ajax请求,要使用http请求或者表单请求。按道理说是没有问题的,包括响应头的设置都没问题,但是点击下载按钮过后,浏览器没任何反应,也没有报错,但是就是没有按照我想的执行文件下载的效果。ajax返回的是字符型数据,而此处我们需要的是返回文件流到浏览器,我们才能下载到文件。所以在做文件下载请求的时候我们不能用ajax()方法。
JS部分

                var flag=true;
                searchFrom.form('submit',{
                    url: "/disclosuredata/report/reportImport",
                    onSubmit: function(param){
                        $.messager.show({
                            title : '提示',
                            msg : '导出成功',
                            timeout:2000
                        });
                        return flag;
                    },
                    success: function(result){
                        var result = eval('('+result+')');
                        if (result.errorMsg){
                            $.messager.show({
                                title : '提示',
                                msg : '下载错误,请稍后重试!',
                                timeout:2000
                            });
                        }
                    }
                });

后台部分

 /**导出报告*/
    @RequestMapping("/reportImport")
    public AjaxResult loadword(HttpServletRequest request,
                               HttpServletResponse response) throws UnsupportedEncodingException {

//        String fwqpath= "/data/doc/";
        String fwqpath="D:\\documents\\";
        String path=fwqpath+"IFRS17Report.docx";
        System.out.println(path);
        String fileName="IFRS17Report.docx";
        String[]arr={fileName,path};
        try {
            uploadUtil.download(request, response, arr);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "导出失败,原因是:" + e.getMessage());
        }
        return new AjaxResult(true,"导出成功");
    }

公共类

/**
 * 公共下载类
 * @author jtk
 */
@Component
public class UploadUtil {

	String repositoryPath;
	String uploadPath;
	String fileName=null;
	String date;
	
	public String getRepositoryPath() {
		return repositoryPath;
	}
	public void setRepositoryPath(String repositoryPath) {
		this.repositoryPath = repositoryPath;
	}
	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}
	public String getUploadPath(){
		return uploadPath;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public void download(HttpServletRequest request,HttpServletResponse response,String[]param) throws Exception{
		BufferedInputStream bis = null;  
		BufferedOutputStream bos = null;  
		try{
			String contentType="application/octet-stream";
	        response.setContentType("text/html;charset=UTF-8");  
	        request.setCharacterEncoding("UTF-8");  
	        String name=param[0];
	        String path=param[1];
	        long fileLength = new File(path).length();  
	        
	        System.out.println("========================"+URLEncoder.encode(name,"UTF-8"));
	        
	        response.setContentType(contentType);  
	        response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(name,"UTF-8"));
	        response.setHeader("Content-Length", String.valueOf(fileLength));  
	  
	        bis = new BufferedInputStream(new FileInputStream(path));  
	        bos = new BufferedOutputStream(response.getOutputStream());  
	        byte[] buff = new byte[2048];  
	        int bytesRead;  
	        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
	            bos.write(buff, 0, bytesRead);  
	        }  
	        System.out.println("===========================================1");
//	        bis.close(); 
	        bos.flush();
//			bos.close();
		}catch(Exception e){
			e.printStackTrace();
		
		}finally {
			if(bos!=null){
				bos.close();			 
			}
			if(bis!=null){
				bis.close(); 
			}
		}
		
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值