上传文件映射表
| 37 | * 上传文件映射表 |
| 38 | */ |
| 39 | @RestController |
| 40 | @RequestMapping("file") |
| 41 | @SuppressWarnings({"unchecked","rawtypes"}) |
| 42 | public class FileController{ |
| 43 | @Autowired |
| 44 | private ConfigService configService; |
| 45 | /** |
| 46 | * 上传文件 |
| 47 | */ |
| 48 | @RequestMapping("/upload") |
| 49 | public R upload(@RequestParam("file") MultipartFile file,String type) throws Exception { |
| 50 | if (file.isEmpty()) { |
| 51 | throw new EIException("上传文件不能为空"); |
| 52 | } |
| 53 | String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); |
| 54 | File path = new File(ResourceUtils.getURL("classpath:static").getPath()); |
| 55 | if(!path.exists()) { |
| 56 | path = new File(""); |
| 57 | } |
| 58 | File upload = new File(path.getAbsolutePath(),"/upload/"); |
| 59 | if(!upload.exists()) { |
| 60 | upload.mkdirs(); |
| 61 | } |
| 62 | String fileName = new Date().getTime()+"."+fileExt; |
| 63 | File dest = new File(upload.getAbsolutePath()+"/"+fileName); |
| 64 | file.transferTo(dest); |
| 65 | if(StringUtils.isNotBlank(type) && type.equals("1")) { |
| 66 | ConfigEntity configEntity = configService.selectOne(new EntityWrapper<ConfigEntity>().eq("name", "faceFile")); |
| 67 | if(configEntity==null) { |
| 68 | configEntity = new ConfigEntity(); |
| 69 | configEntity.setName("faceFile"); |
| 70 | configEntity.setValue(fileName); |
| 71 | } else { |
| 72 | configEntity.setValue(fileName); |
| 73 | } |
| 74 | configService.insertOrUpdate(configEntity); |
| 75 | } |
| 76 | return R.ok().put("file", fileName); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * 下载文件 |
| 81 | */ |
| 82 | @IgnoreAuth |
| 83 | @RequestMapping("/download") |
| 84 | public ResponseEntity<byte[]> download(@RequestParam String fileName) { |
| 85 | try { |
| 86 | File path = new File(ResourceUtils.getURL("classpath:static").getPath()); |
| 87 | if(!path.exists()) { |
| 88 | path = new File(""); |
| 89 | } |
| 90 | File upload = new File(path.getAbsolutePath(),"/upload/"); |
| 91 | if(!upload.exists()) { |
| 92 | upload.mkdirs(); |
| 93 | } |
| 94 | File file = new File(upload.getAbsolutePath()+"/"+fileName); |
| 95 | if(file.exists()){ |
| 96 | /*if(!fileService.canRead(file, SessionManager.getSessionUser())){ |
nothing calls this directly
no outgoing calls
no test coverage detected