MCPcopy Create free account
hub / github.com/bsxj97/flower-mall / FileController

Class FileController

src/main/java/com/controller/FileController.java:29–83  ·  view source on GitHub ↗

上传文件映射表

Source from the content-addressed store, hash-verified

27 * 上传文件映射表
28 */
29@RestController
30@RequestMapping("file")
31@SuppressWarnings({"unchecked","rawtypes"})
32public class FileController{
33 @Autowired
34 private ConfigService configService;
35 /**
36 * 上传文件
37 */
38 @RequestMapping("/upload")
39 public R upload(@RequestParam("file") MultipartFile file, String type,HttpServletRequest request) throws Exception {
40 if (file.isEmpty()) {
41 throw new EIException("上传文件不能为空");
42 }
43 String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
44 String fileName = new Date().getTime()+"."+fileExt;
45 File dest = new File("F:/xy/tmp/鲜花/前后端分离/OnlineFlowerTransactionManagementSystem/src/main/webapp/upload"+"/"+fileName);
46 file.transferTo(dest);
47 if(StringUtils.isNotBlank(type) && type.equals("1")) {
48 ConfigEntity configEntity = configService.selectOne(new EntityWrapper<ConfigEntity>().eq("name", "faceFile"));
49 if(configEntity==null) {
50 configEntity = new ConfigEntity();
51 configEntity.setName("faceFile");
52 configEntity.setValue(fileName);
53 } else {
54 configEntity.setValue(fileName);
55 }
56 configService.insertOrUpdate(configEntity);
57 }
58 return R.ok().put("file", fileName);
59 }
60
61 /**
62 * 下载文件
63 */
64 @IgnoreAuth
65 @RequestMapping("/download")
66 public void download(@RequestParam String fileName, HttpServletRequest request, HttpServletResponse response) {
67 try {
68 File file = new File(request.getSession().getServletContext().getRealPath("/upload")+"/"+fileName);
69 if (file.exists()) {
70 response.reset();
71 response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName+"\"");
72 response.setHeader("Cache-Control", "no-cache");
73 response.setHeader("Access-Control-Allow-Credentials", "true");
74 response.setContentType("application/octet-stream; charset=UTF-8");
75 IOUtils.write(FileUtils.readFileToByteArray(file), response.getOutputStream());
76 }
77
78 } catch (IOException e) {
79 e.printStackTrace();
80 }
81 }
82
83}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected