增加上传图片文件

master
王堂东 2023-10-20 20:05:27 +08:00
parent 5e27be5d15
commit 2bd77f3750
7 changed files with 122 additions and 4 deletions

View File

@ -1,6 +1,6 @@
# Tomcat # Tomcat
server: server:
port: 9203 port: 9204
# Spring # Spring
spring: spring:

View File

@ -17,7 +17,11 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
<dependency> <dependency>
<groupId>com.february</groupId> <groupId>com.february</groupId>
<artifactId>february-patient-circle-common</artifactId> <artifactId>february-patient-circle-common</artifactId>
@ -47,6 +51,16 @@
<groupId>com.github.tobato</groupId> <groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId> <artifactId>fastdfs-client</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
</dependencies> </dependencies>

View File

@ -130,4 +130,8 @@ public class PatientController {
public Result patientToSick(@RequestBody ReviewPatientRequest reviewPatientRequest){ public Result patientToSick(@RequestBody ReviewPatientRequest reviewPatientRequest){
return patientService.patientToSick(reviewPatientRequest); return patientService.patientToSick(reviewPatientRequest);
} }
@PostMapping("/uploadMsg")
public Result uploadMsg(@RequestParam("file")MultipartFile file){
return patientService.uploadMsg(file);
}
} }

View File

@ -43,4 +43,5 @@ public interface PatientService {
Result patientToSick(ReviewPatientRequest reviewPatientRequest); Result patientToSick(ReviewPatientRequest reviewPatientRequest);
Result uploadMsg(MultipartFile file);
} }

View File

@ -10,6 +10,7 @@ import com.february.patient.circle.mapper.PatientMapper;
import com.february.patient.circle.result.Result; import com.february.patient.circle.result.Result;
import com.february.patient.circle.service.PatientService; import com.february.patient.circle.service.PatientService;
import com.february.patient.circle.util.FastUtil; import com.february.patient.circle.util.FastUtil;
import com.february.patient.circle.util.OssUtil;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -29,6 +30,8 @@ public class PatientServiceImpl implements PatientService {
private PatientMapper patientMapper; private PatientMapper patientMapper;
@Autowired @Autowired
private FastUtil fastUtil; private FastUtil fastUtil;
@Autowired
private OssUtil ossUtil;
@Override @Override
public Result<List<PatientCircle>> patientCircleList(Integer userId) { public Result<List<PatientCircle>> patientCircleList(Integer userId) {
List<PatientCircle> list=patientMapper.patientCircleList(userId); List<PatientCircle> list=patientMapper.patientCircleList(userId);
@ -87,7 +90,7 @@ public class PatientServiceImpl implements PatientService {
public Result upload(MultipartFile file) { public Result upload(MultipartFile file) {
try { try {
String upload = fastUtil.upload(file); String upload = fastUtil.upload(file);
return Result.success("http://192.168.192.128:8888/"+upload,"上传成功"); return Result.success("http://10.100.1.2:8888/"+upload,"上传成功");
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
@ -108,4 +111,15 @@ public class PatientServiceImpl implements PatientService {
} }
return Result.success("评论失败"); return Result.success("评论失败");
} }
@Override
public Result uploadMsg(MultipartFile file) {
try {
String s = OssUtil.UploadImage(file);
return Result.success(s);
}catch (Exception e){
e.printStackTrace();
}
return Result.success("上传失败");
}
} }

View File

@ -0,0 +1,71 @@
package com.february.patient.circle.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
* @author Administrator
*/
@Component
public class OssUtil {
static String endpoint = "oss-cn-shanghai.aliyuncs.com";
static String accessKeyId = "LTAI5tMtBBNwEhU9PxTgQJd9";
static String accessKeySecret = "y9pJbQtL7cSAlSPmTFugI8aH86mLzE";
static String bucketName = "srb-file-cong";
static String filePath = "avatar";
public static String UploadImage(MultipartFile file) throws IOException {
// String filePath = "C:\\Users\\Administrator\\Pictures\\微信图片\\QQ图片20231015190011.jpg";
// 获取文件的输入流
InputStream inputStream = file.getInputStream();
// 获取文件的原始名称
String originalFilename = file.getOriginalFilename();
// 路径 + 名称
String fileName = originalFilename;
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 日期格式化
String folderTime = new DateTime().toString("/yyyy/MM/dd/");
// 唯一ID + 切割.之后的文件类型
fileName = filePath + folderTime + UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
// 上传文件。
ossClient.putObject(bucketName, fileName, inputStream);
// 释放OSSClient资源。
ossClient.shutdown();
return "https://" + bucketName + "." + endpoint + "/" + fileName;
}
public static void removeFile(String url) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String host = "https://" + bucketName + "." + endpoint + "/";
String objectName = url.substring(host.length());
try {
ossClient.deleteObject(bucketName, objectName);
} catch (Exception e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
}

View File

@ -1,7 +1,9 @@
# Tomcat # Tomcat
server: server:
port: 9203 port: 9203
mybatis:
configuration:
map-underscore-to-camel-case: true
# Spring # Spring
spring: spring:
application: application:
@ -25,3 +27,15 @@ spring:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
rabbitmq: rabbitmq:
host: 10.100.1.3 host: 10.100.1.3
fdfs:
so-timeout: 1500 # socket 连接时长
connect-timeout: 600 # 连接 tracker 服务器超时时长
# 这两个是你服务器的 IP 地址,注意 23000 端口也要打开阿里云服务器记得配置安全组。tracker 要和 stroage 服务进行交流
tracker-list: 10.100.1.2:22122
web-server-url: 10.100.1.2:8888
pool:
jmx-enabled: false
# 生成缩略图
thumb-image:
height: 500
width: 500