数据返回工具类package com.sykj.util;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;/** * @ClassName ResponseResult * @Description TODO * Author JiangPengCheng * Date 2020/7/15 9:13 **/@Data@NoArgsConstructor@AllArgsConstructorpublic class ResponseResult implements Serializable {private Integer code;private Stringmessage;private Object object;public static ResponseResult ok(String message){return new ResponseResult(200,message,null);}public static ResponseResult ok(String message, Object object){return new ResponseResult(200,message,object);}public staticResponseResult error(String message){return new ResponseResult(500,message,null);}public staticResponseResult error(String message, Object o){return new ResponseResult(500,message,o);}}Service类package com.sykj.service;import com.sykj.util.ResponseResult;import org.springframework.http.ResponseEntity;import org.springframework.web.multipart.MultipartFile;/** * @Description: 公共业务 * @Author: junqiang.lu * @Date: 2018/12/24 */public interface CommonService {/*** 上传文件至阿里云 oss** @param file* @param* @return* @throws Exception*/ResponseResult uploadOSS(MultipartFile file) throws Exception;ResponseResult delete(String objectName);}Service实现类package com.sykj.service.impl;import com.sykj.config.OSSConfig;import com.sykj.service.CommonService;import com.sykj.util.OSSBootUtil;import com.sykj.util.ResponseResult;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;/** * @Description: 公共业务具体实现类 * @Author: junqiang.lu * @Date: 2018/12/24 */@Service("commonService")public class CommonServiceImpl implements CommonService {@Autowiredprivate OSSConfig ossConfig;/*** 上传文件至阿里云 oss** @param file* @param* @return* @throws Exception*/@Overridepublic ResponseResult uploadOSS(MultipartFile file) throws Exception {// 格式化时间SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");String format = simpleDateFormat.format(new Date());// 高依赖版本 oss 上传工具String ossFileUrlBoot = null;/*** ossConfig 配置类* file 文件* "jpc/"+format 上传文件地址 加时间戳*/ossFileUrlBoot = OSSBootUtil.upload(ossConfig, file, "jpc/"+format);System.out.println(ossFileUrlBoot);Map<String, Object> resultMap = new HashMap<>(16);//resultMap.put("ossFileUrlSingle", ossFileUrlSingle);resultMap.put("ossFileUrlBoot", ossFileUrlBoot);return ResponseResult.ok("上传成功~~",ossFileUrlBoot);}@Overridepublic ResponseResult delete(String objectName) {ResponseResult delete = OSSBootUtil.delete(objectName, ossConfig);return delete;}}Controller类package com.sykj.comtroller;import com.sun.org.apache.regexp.internal.RE;import com.sykj.service.CommonService;import com.sykj.util.ResponseResult;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;/** * @Description: 公共模块控制中心 * @Author: junqiang.lu * @Date: 2018/12/24 */@RestController@RequestMapping("api/demo/common")public class CommonController {private static final Logger logger = LoggerFactory.getLogger(CommonController.class);@Autowiredprivate CommonService commonService;/*** 上传文件至阿里云 oss** @param file* @param* @return* @throws Exception*/@RequestMapping(value = https://www.isolves.com/it/cxkf/kj/2020-08-21/"/upload/oss", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})public ResponseResult uploadOSS(@RequestParam(value = "file") MultipartFile file) throws Exception {System.out.println(file.getInputStream());//ResponseResult responseResult = commonService.uploadOSS(file);//HttpHeaders headers = new HttpHeaders();//headers.setContentType(MediaType.APPLICATION_JSON_UTF8);return ResponseResult.ok("ok");}@RequestMapping("/delete/oss")public ResponseResult deltetOss(String objectName){System.out.println(objectName+"-------------------------------");ResponseResult delete = commonService.delete(objectName);return delete;}}注意postman选择file
推荐阅读
- SpringBoot整合JPA案例
- SpringBoot中使用dubbo实现RPC调用
- SpringBoot使用OpenCV总结
- 在 SpringBoot 中实现多数据源访问的最佳实践
- 附英文 两分钟浅述JBOSS Netty
- SpringBoot教程-HelloWorld
- 十年架构师吐血整合Java开发必备宝典,凭借这份宝典定级腾讯T3-4
- 化佛茶生态环境,安徽池州推进茶叶品牌整合
- SpringBoot常用属性配置
- 基于springboot+shiro+freemarker的快速开发框架,代码免费分享
