admin管理员组文章数量:1606853
使用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();
}
}
}
}
版权声明:本文标题:IO流下载文件,浏览器无响应 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726114869a1055991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论