/**
* 点击链接下载相应的文件
* @param response
* @return
*/
@RequestMapping(value = "majorSystem/downloadfile", method = RequestMethod.GET)
public ModelAndView downloadfile( HttpServletResponse response, String matUrl, String matName){
ModelAndView mav = new ModelAndView("jsonView");
try {
if(matUrl != null){
response.setContentType(ContextLoader.getCurrentWebApplicationContext().getServletContext().getMimeType(matUrl));
response.setHeader("Content-Disposition", "attachment;filename="+new String(matName.getBytes("ISO8859-1"), "UTF-8"));
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("")+"/jsp/professional/scoreRuleDocument/"+matUrl;
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//写文件
byte[] buffer = new byte[1024];
while((in.read(buffer))!= -1)
{
out.write(buffer);
}
in.close();
out.close();
return null;
}
} catch (Exception e) {
}
return null;
}
基于springmvc的简单下载
最新推荐文章于 2023-03-28 23:14:47 发布