vue+axios+springboot文件下载
@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
public static final String downLoad(HttpServletRequest req, HttpServletResponse res){
Map<String, Object> reMap = new HashMap<>();
String fileName = "aaa.txt";
String path = "f:/svss/";
String filepath = path+fileName;
OutputStream os = null;
InputStream is = null;
try {
// 清空输出流
res.reset();
res.setCharacterEncoding("utf-8");
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
// 读取流
File f = new File(filepath);
is = new FileInputStream(f);
if (is == null) {
reMap.put("msg", "下载附件失败");
}
ServletOutputStream sout = res.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(sout);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
bos.close();
bis.close();
is.close();
os.close();
} catch (Exception e) {
reMap.put("msg", "下载附件失败,error:" + e.getMessage());
}
String str = JsonUtil.map2Json(reMap);
return str;
}