@RequestMapping(value ="/upload",method = RequestMethod.POST) @Permission(isAjax=false) public String Upload(HttpServletRequest request, HttpServletResponse response,@RequestParam("files") MultipartFile[] files) { List<String> fileurl=new ArrayList<String>(); try {
if(files.length>0){ String uploaddir="d:/upload/2017-12-18/"; File dir=new File(uploaddir); if(!dir.exists()){ dir.mkdirs(); } for(MultipartFile file :files){ if(!file.isEmpty()){ String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); String filename=String.format("%s%s", UUID.randomUUID().toString(),suffix); File fileinfo=new File(String.format("%s%s", uploaddir,filename)); FileUtils.writeByteArrayToFile(fileinfo, file.getBytes()); String url=String.format("/2017-12-18/%s", filename); fileurl.add(url); } } } // fileurl 图片地址清单
} catch (IOException e) { } }
下面是图片预览地址的解析方法
@Controller public class HomeController { private final ResourceLoader resourceLoader; @Autowired public HomeController(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @RequestMapping(method = RequestMethod.GET, value = "/{yyyy}-{MM}-{dd}/{filename:.+}") @ResponseBody public ResponseEntity<?> getFile(@PathVariable String yyyy, @PathVariable String MM, @PathVariable String dd, @PathVariable String filename) { try { return ResponseEntity.ok(resourceLoader.getResource( "file:" + Paths.get("d:/upload/" + yyyy+"-"+MM+"-"+dd + "/", filename).toString())); } catch (Exception e) { return ResponseEntity.notFound().build(); } } }
访问图片地址为:http://localhost:8080/2017-12-18/956f293b-876e-4347-9fae-29ac290f357b.jpg
可以直接访问jar包以外,上传的文件、图片
时间: 2024-10-11 15:21:26