| | |
| | | import com.ycl.api.CommonResult; |
| | | import com.ycl.dto.media.Media; |
| | | import com.ycl.dto.media.PictureZoomParameter; |
| | | import com.ycl.service.oss.OssService; |
| | | import com.ycl.utils.AliyunUtils; |
| | | import com.ycl.utils.MediaFileUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.InputStream; |
| | | |
| | | @RestController |
| | | @Api(tags = "文件上传") |
| | | @RequestMapping("/file") |
| | | public class FileController { |
| | | |
| | | private MediaFileUtil mediaFileUtil; |
| | | // private MediaFileUtil mediaFileUtil; |
| | | // @Autowired |
| | | // public void setMediaFileUtil(MediaFileUtil mediaFileUtil) { |
| | | // this.mediaFileUtil = mediaFileUtil; |
| | | // } |
| | | |
| | | // /** |
| | | // * 上传图片 |
| | | // * @param file |
| | | // * @return |
| | | // */ |
| | | // @ApiOperation("上传图片") |
| | | // @RequestMapping(value = "/medias", method = RequestMethod.POST) |
| | | // public CommonResult<Media> mediaUpload(@RequestParam("file") MultipartFile file) { |
| | | // try { |
| | | // PictureZoomParameter zoomPar = PictureZoomParameter.getBoardPar(); |
| | | // Media res = mediaFileUtil.save(file, zoomPar); |
| | | // |
| | | // return CommonResult.success(res, "ok"); |
| | | // } catch (Exception e) { |
| | | // return CommonResult.failed("图片格式只能为jpg、jpeg、png,只修改文件的后缀无效!"); |
| | | // } |
| | | // } |
| | | |
| | | // @ApiOperation("删除图片") |
| | | // @RequestMapping(value = "media/delete", method = RequestMethod.DELETE) |
| | | // public CommonResult<Boolean> delete(@RequestBody Media media) { |
| | | // try { |
| | | // mediaFileUtil.removeMedia(media); |
| | | // return CommonResult.success(true, "ok"); |
| | | // } catch (Exception e) { |
| | | // return CommonResult.failed("fail!"); |
| | | // } |
| | | // } |
| | | |
| | | @Autowired |
| | | public void setMediaFileUtil(MediaFileUtil mediaFileUtil) { |
| | | this.mediaFileUtil = mediaFileUtil; |
| | | private OssService ossService; |
| | | |
| | | @ApiOperation(value = "上传图片") |
| | | @PostMapping("/upload") |
| | | public CommonResult<String> uploadImages(MultipartFile file) { |
| | | String url = ossService.uploadImages(file); |
| | | return CommonResult.success(url, "ok"); |
| | | } |
| | | |
| | | /** |
| | | * 上传图片 |
| | | * |
| | | * @param file |
| | | * @return |
| | | */ |
| | | |
| | | @ApiOperation("上传图片") |
| | | @RequestMapping(value = "/medias", method = RequestMethod.POST) |
| | | public CommonResult<Media> mediaUpload(@RequestParam("file") MultipartFile file) { |
| | | try { |
| | | PictureZoomParameter zoomPar = PictureZoomParameter.getBoardPar(); |
| | | Media res = mediaFileUtil.save(file, zoomPar); |
| | | |
| | | return CommonResult.success(res, "ok"); |
| | | } catch (Exception e) { |
| | | return CommonResult.failed("fail!"); |
| | | @ApiOperation(value = "删除图片") |
| | | @PostMapping("/delete") |
| | | public CommonResult<Boolean> deleteImages(String fileUrl) { |
| | | boolean flag = ossService.deleteImages(fileUrl); |
| | | if (flag) { |
| | | return CommonResult.success(true, "删除成功"); |
| | | } |
| | | return CommonResult.failed("fail!"); |
| | | } |
| | | |
| | | @ApiOperation("删除图片") |
| | | @RequestMapping(value = "media/delete", method = RequestMethod.DELETE) |
| | | public CommonResult<Boolean> delete(@RequestBody Media media) { |
| | | try { |
| | | mediaFileUtil.removeMedia(media); |
| | | |
| | | return CommonResult.success(true, "ok"); |
| | | } catch (Exception e) { |
| | | return CommonResult.failed("fail!"); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |