JAVA 使用springMvc下载文件 有响应,但是不触发下载动作怎么办?

myzhongw 2017-06-09 02:41:49
以下是controller
@RequestMapping("/download")
public void download(@RequestParam("filePath") String path2, HttpServletResponse response) {
String path = servletContext.getRealPath("/");
File file = new File(path2);
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="+file.getName());
ServletOutputStream out;
file = new File(path + "File/" + file.getName());
try {
FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);
String str = reader.readLine();
while (str != null) {
//String fileName = new String(str.getBytes("GBK"),"utf-8");
System.out.println(str);
str = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
byte[] buffer = new byte[8192];
while (inputStream.read(buffer)!=-1){
out.write(buffer);
out.flush();
}
inputStream.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
页面的响应头

页面的响应内容
...全文
283 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
myzhongw 2017-06-12
  • 打赏
  • 举报
回复
引用 1 楼 hanjun0612 的回复:
大兄弟,你把技术贴发到IT缘分。也是有才
分数不够有什么办法
正怒月神 2017-06-09
  • 打赏
  • 举报
回复
大兄弟,你把技术贴发到IT缘分。也是有才

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧