@RequestMapping("/upload")
public String upload(MultipartFile pic) throws IOException {
//获取文件的名字
String filename = pic.getOriginalFilename();
//文件复制
FileCopyUtils.copy(pic.getInputStream(), new FileOutputStream("F:/upload"+filename));
//调用业务层,存入数据库
File file = new File(null, "F:/upload"+filename);
fileService.addFile(file);
return "redirect:/show.do";
}
@RequestMapping("/download")
public void download(String filename, HttpServletResponse resp) throws IOException {
//输入流读取文件
FileInputStream fileInputStream = new FileInputStream(filename);
//2.设置响应格式和文件的名字
resp.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(filename,"utf-8"));
//获取输出流
ServletOutputStream out = resp.getOutputStream();
FileCopyUtils.copy(fileInputStream, out);
}
上传与下载
最新推荐文章于 2023-06-07 12:28:46 发布