@Autowired
private HttpServletRequest request;
@RequestMapping(value="upload")
public String upload(@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
+new Date().getTime() +UUID.randomUUID() +"_"+ file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
return "success.jsp";
}
@RequestMapping(value="download")
public void download(HttpServletRequest request,HttpServletResponse response) throws IOException{
String filename="1.jpg";
response.setContentType("application/x-msdownload");
FileInputStream fis = new FileInputStream(
new File(request.getSession().getServletContext().getRealPath("/upload/")+filename));
response.setHeader("Content-Disposition", "attachment;filename="+filename);
OutputStream os = response.getOutputStream();
IOUtils.copy(fis,os);
}
SpringMVC文件的上传与下载
最新推荐文章于 2022-05-02 14:14:26 发布