springboot 文件上传 各种姿势 解锁,策略模式简单运用

总结:从文件上传谈 策略模式运用 ,虚拟路径配置,纸上得来终觉浅,绝知此事要躬行。

           单文件,多文件,base64 几种上传方式 。具体细节参考我的代码库 https://gitee.com/www.lyc.com/lyc-demo.git

   

/**
* tip 小知识这里其实有多套(本地 阿里 七牛,又拍,腾讯) 实现
* 可以根据 传参不同 从容器 取对于的实现 然后管理不同的 文件服务器
* 联想策略模式 消除 if else 设计模式优雅> switch case 优雅> if else
*/
//@Autowired
private FileService fileService = (FileServiceImpl) SptingUtils.getBean("fileServiceImpl");

@GetMapping
public String index() {
return "index";
}

/**
* 单文件
* @param file
* @param request
* @return
*/
@PostMapping("/uploads/upload1")
@ResponseBody
public ResponseResult upload1(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

String result = null;
try {
result = fileService.upload(file,request);
} catch (IOException e) {
ResponseResultFactory.fieldFail("上传失败",result);
}
return ResponseResultFactory.success("上传成功",result);
}

/**
* 多文件
* @param files
* @return
* @throws IOException
*/
@PostMapping("/uploads/upload2")
@ResponseBody
public ResponseResult upload2(@RequestParam("file") MultipartFile[] files) {
String result = null;
try {
result = fileService.uploads(files);
} catch (IOException e) {
ResponseResultFactory.fieldFail("上传失败",result);
}
return ResponseResultFactory.success("上传成功",result);
}

/**
* base64
* @param base64Image
* @return
* @throws IOException
*/
@PostMapping("/uploads/upload3")
@ResponseBody
public ResponseResult upload2(String base64Image) {
String result = null;
try {
result = fileService.uploadBase64(base64Image);
} catch (IOException e) {
ResponseResultFactory.fieldFail("上传失败",result);
}
return ResponseResultFactory.success("上传成功",result);
}

      

posted @ 2019-06-07 17:15  川流不息&  阅读(497)  评论(0编辑  收藏  举报