后台
@Controller
public class IndexController {
@Value("${my-config.file-path}")
private String myFilePath;
@RequestMapping("test")
public String test() {
return "index";
}
@RequestMapping("upload")
public String upload(@RequestParam("file") MultipartFile file, Model model, HttpServletRequest req) {
try {
String fileName = System.currentTimeMillis()+file.getOriginalFilename();
String destFileName=myFilePath+"uploaded"+ File.separator+fileName;
File destFile = new File(destFileName);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
file.transferTo(destFile);
model.addAttribute("filename","uploaded/"+fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
}
return "index";
}
@ResponseBody
@GetMapping("/download")
public String downloadImage(@RequestParam(value = "imageName",required = false) String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {