文件上传

This commit is contained in:
wangqiy 2025-04-16 15:15:15 +08:00
parent 6f271e53b9
commit ab74c21561
4 changed files with 21 additions and 11 deletions

View File

@ -7,6 +7,8 @@ import javax.servlet.http.HttpServletResponse;
import com.agri.common.config.OSSConfig;
import com.agri.common.utils.AliyunOSSUtils;
import com.agri.common.utils.ApiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.agri.common.config.RuoYiConfig;
@ -29,7 +32,9 @@ import com.agri.framework.config.ServerConfig;
*
* @author ruoyi
*/
@Api(value = "通用接口", tags = "通用接口")
@RestController
@RequestMapping(value = "/common", produces = "application/json")
public class CommonController
{
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@ -49,7 +54,8 @@ public class CommonController
* @param fileName 文件名称
* @param delete 是否删除
*/
@GetMapping("common/download")
@GetMapping("/download")
@ApiOperation(value = "下载", notes = "下载")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
{
try
@ -78,7 +84,8 @@ public class CommonController
/**
* 通用上传请求
*/
@PostMapping("/common/upload")
@ApiOperation(value = "上传", notes = "上传")
@PostMapping("/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception {
try {
// 上传文件路径
@ -105,7 +112,8 @@ public class CommonController
/**
* 本地资源通用下载
*/
@GetMapping("/common/download/resource")
@GetMapping("/download/resource")
@ApiOperation(value = "本地资源通用下载", notes = "本地资源通用下载")
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
@ -135,7 +143,7 @@ public class CommonController
* 获取上传文件签名
* @return
*/
@Operation(summary = "获取上传文件签名",method = "GET")
@ApiOperation(value = "获取上传文件签名", notes = "获取上传文件签名")
@GetMapping("/getSign")
public String getOssSign() {
return ApiUtils.successData(aliyunOSSUtils.getSign());

View File

@ -22,6 +22,6 @@ aliyun:
accessKeySecret: 4kOMnSCvTN9KAufb1ul0dHzQuYo8Mz
bucketName: gov-cloud
prefixKey: backend/
# domainName: https://ossnew.ljnhs.cn/
domainName: https://oss.sznyagri.com/
maxSize: 100 #文件大小限制(单位: m) 默认10m
uploadType: oss

View File

@ -35,7 +35,7 @@ public class OSSConfig {
/**
* 自定义域名,预览图片用到
*/
// private String domainName;
private String domainName;
/**
* 文件大小限制(单位: m)
*/

View File

@ -81,7 +81,7 @@ public class AliyunOSSUtils {
throw new BusinessException("图片上传失败");
}
//返回文件访问路径
return key;
return ossConfig.getDomainName() + key;
}
/**
@ -98,12 +98,14 @@ public class AliyunOSSUtils {
try {
ossClient.putObject(ossConfig.getBucketName(), key, inputStream);
inputStream.close();
log.info("--------文件上传成功--------");
} catch (Exception e) {
log.error("oos上传失败:{}", e.getMessage());
throw new BusinessException("文件上传失败");
log.error("系统发生异常:",e);
log.info("oos上传失败:" + e.getMessage());
throw new BusinessException("文件上传成功");
}
//返回文件访问路径
return key;
return ossConfig.getDomainName() + key;
}
/**
@ -199,7 +201,7 @@ public class AliyunOSSUtils {
throw new BusinessException("图片上传失败");
}
//返回文件访问路径
return key;
return ossConfig.getDomainName() + key;
}
}