*/
@Controller
@RequestMapping(“/file”)
public class FileController {
@Resource(name = “fileServiceImpl”)
private FileService fileService;
@RequestMapping(“/upload”)
public String fileUpload(@RequestParam MultipartFile file, FileVO filevo, HttpServletRequest request) throws IOException {
//上传路径保存设置
// 把文件写到磁盘
String fileName = file.getOriginalFilename();
String[] str = fileName.split(“\.”);
String uuid = UUID.randomUUID().toString().replaceAll(“-”,“”);
String headPath = “E://upload/” + uuid+ “.”+str[str.length-1];
File dest = new File(headPath);
file.transferTo(dest);
filevo.setFileID(uuid);
filevo.setFilePath(headPath);
filevo.setUserID(null);
try {
fileService.save(filevo);
} catch (Exception e) {
e.printStackTrace();
}
return “redirect:/admin/showFile”;
}
@RequestMapping(“/downFile”)
public void down(HttpServletRequest request, HttpServletResponse response,String fileID) throws Exception{
FileVO fileVO = fileService.findById(fileID);
String fileName = fileVO.getFilePath();
String[] str = fileName.split(“\.”);
InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
String filename = fileVO.getFileName()+“\.”+str[str.length-1];
filename = URLEncoder.encode(filename,“UTF-8”);
response.addHeader(“Content-Disposition”, “attachment;filename=” + filename);
response.setContentType(“multipart/form-data”);
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while((len = bis.read()) != -1){
out.write(len);
out.flush();
}
out.close();
}
}
异常处理
package com.system.exception;
/**
- 系统自定义异常类,针对预期异常,需要在程序中抛出此类的