| | |
| | | package com.ruoyi.web.controller.api; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.ruoyi.common.config.FileUploadConfig; |
| | | import com.ruoyi.common.constant.CacheConstants; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import com.ruoyi.system.model.TFile; |
| | | import com.ruoyi.system.service.SysFileService; |
| | | import com.ruoyi.system.service.impl.SysFileServiceImpl; |
| | | import com.ruoyi.web.controller.tool.TencentCosUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.net.URLEncoder; |
| | | |
| | | /** |
| | | * @author HJL |
| | |
| | | @RestController |
| | | @RequestMapping("/cos") |
| | | @Api(tags = "公共-文件上传") |
| | | @Slf4j |
| | | public class COSController { |
| | | |
| | | @Resource |
| | | private TencentCosUtil tencentCosUtil; |
| | | |
| | | @Autowired |
| | | SysFileService sysFileService; |
| | | |
| | | @Autowired |
| | | FileUploadConfig fileUploadConfig; |
| | | |
| | | @Autowired |
| | | TokenService tokenService; |
| | | |
| | | public String getLocalUrlPrefix(){ |
| | | ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()); |
| | | if (servletRequestAttributes==null || servletRequestAttributes.getRequest()==null){ |
| | | return fileUploadConfig.getFileUrlPrefix(); |
| | | } |
| | | HttpServletRequest request = servletRequestAttributes.getRequest(); |
| | | StringBuffer url = new StringBuffer(); |
| | | url.append(request.getScheme()).append("://") |
| | | .append(request.getServerName()) |
| | | .append((request.getServerPort() == 80 ? "" : ":" + request.getServerPort())) |
| | | .append(request.getContextPath()); |
| | | return url.toString(); |
| | | } |
| | | |
| | | public String getLocalFileUrlPrefix(String fileId){ |
| | | String token = tokenService.getLoginUser().getToken(); |
| | | StringBuffer url = new StringBuffer(); |
| | | url.append(getLocalUrlPrefix()) |
| | | .append("/cos/get/").append(fileId).append("?s=").append(URLEncoder.encode(token)) |
| | | ; |
| | | return url.toString(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static void failResponse(String message){ |
| | | ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()); |
| | | HttpServletResponse response = servletRequestAttributes.getResponse(); |
| | | String failResult = JSON.toJSONString(R.fail(message)); |
| | | //设置编码格式 |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setContentType("application/json;charset=UTF-8"); |
| | | PrintWriter pw = null; |
| | | try { |
| | | pw = response.getWriter(); |
| | | pw.write(failResult); |
| | | pw.flush(); |
| | | } catch (IOException e) { |
| | | log.error("io异常"); |
| | | }finally { |
| | | if (pw!=null) { |
| | | pw.close(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | @GetMapping("get/{fileId}") |
| | | public void get(@PathVariable("fileId") String fileid,@RequestParam("s") String s){ |
| | | if (StringUtils.isEmpty(fileid)){ |
| | | failResponse("文件ID不能为空"); |
| | | return; |
| | | } |
| | | if (StringUtils.isEmpty(s)){ |
| | | failResponse("token不能为空"); |
| | | return; |
| | | } |
| | | Object object = redisCache.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + s); |
| | | if (object==null){ |
| | | failResponse("用户登录已失效"); |
| | | return; |
| | | } |
| | | TFile file = sysFileService.getById(fileid); |
| | | if (file==null){ |
| | | failResponse("图片不存在"); |
| | | return; |
| | | } |
| | | tencentCosUtil.download(file); |
| | | } |
| | | |
| | | |
| | | @GetMapping("get/file") |
| | | public void getFile(@RequestParam("fileUrl") String fileUrl,@RequestParam("s") String s){ |
| | | if (StringUtils.isEmpty(fileUrl)){ |
| | | failResponse("文件路径不能为空"); |
| | | return; |
| | | } |
| | | if (StringUtils.isEmpty(s)){ |
| | | failResponse("token不能为空"); |
| | | return; |
| | | } |
| | | Object object = redisCache.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + s); |
| | | if (object==null){ |
| | | failResponse("用户登录已失效"); |
| | | return; |
| | | } |
| | | tencentCosUtil.download(fileUrl); |
| | | } |
| | | |
| | | /** |
| | | * 新上传接口,下一版更新 |
| | | * @param file |
| | | * @param folder 上传到cos的文件目录:如/contract/ |
| | | * @return |
| | | */ |
| | | @PostMapping("/uploadnew") |
| | | @ApiOperation(value = "文件上传,带上传目录,返回文件ID", tags = "公共-文件上传") |
| | | @ApiImplicitParams({ |
| | | @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) |
| | | }) |
| | | public R<TFile> uploadnew(@RequestParam("file") MultipartFile file, @RequestParam("folder") String folder) { |
| | | TFile tFile = tencentCosUtil.upload(file,folder); |
| | | tFile.setFileUrl(getLocalFileUrlPrefix(tFile.getId())); |
| | | return R.ok(tFile); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param file |
| | | * @param |
| | | * @return |
| | | */ |
| | | @PostMapping("/upload") |
| | | @ApiOperation(value = "文件上传", tags = "公共-文件上传") |
| | | @ApiImplicitParams({ |
| | | @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) |
| | | }) |
| | | public R<String> upload(@RequestParam("file") MultipartFile file) { |
| | | String s = tencentCosUtil.upLoadFile(file); |
| | | return R.ok(s, s); |
| | | public R<String> upload(@RequestParam("file") MultipartFile file,@RequestParam("folder") String folder) { |
| | | String url = tencentCosUtil.upLoadFile(file,folder); |
| | | return R.ok(url, url); |
| | | } |
| | | |
| | | @PostMapping("/downloadImg") |
| | | @ApiOperation(value = "文件下载", tags = "公共-文件下载") |
| | | @ApiImplicitParams({ |
| | | @ApiImplicitParam(value = "文件url", name = "url", dataType = "String", required = true) |
| | | }) |
| | | public String downloadImg(@RequestParam("url") String url) { |
| | | return tencentCosUtil.downLoadFileImg(url); |
| | | } |
| | | @PostMapping("/download") |
| | | @ApiOperation(value = "文件下载", tags = "公共-文件下载") |
| | | @ApiImplicitParams({ |
| | | @ApiImplicitParam(value = "文件url", name = "url", dataType = "String", required = true) |
| | | }) |
| | | public void download(@RequestParam("url") String url) { |
| | | tencentCosUtil.downLoadFile(url); |
| | | } |
| | | } |