feat: 本地上传功能

增加本地上传功能,需在配置文件中配置
This commit is contained in:
2023-04-28 09:14:45 +08:00
parent 44971c6e60
commit 2b50369f0a
4 changed files with 79 additions and 28 deletions
@@ -63,9 +63,11 @@ swaggerLevel=0
# \u672C\u5730\u5B58\u50A8\u6587\u4EF6\u7684\u78C1\u76D8\u5730\u5740 # \u672C\u5730\u5B58\u50A8\u6587\u4EF6\u7684\u78C1\u76D8\u5730\u5740
#uploadFile=D:\\work\\idea\\changan\\ #uploadFile=D:\\work\\idea\\changan\\
uploadFile=/data/DeploymentPackage/report_library/uploadfile uploadFile=D:\\uploadfile\\tmp\\
#uploadFile=/data/DeploymentPackage/report_library/uploadfile/
# \u672C\u5730\u6587\u4EF6\u8BBF\u95EE\u7684url\u5730\u5740 # \u672C\u5730\u6587\u4EF6\u8BBF\u95EE\u7684url\u5730\u5740
picPath=/api/home/pic/ picPath=/api/home/pic/
#picPath=D:\\uploadfile\\tmp\\pic\\
serverIp=http://127.0.0.1:7060/ serverIp=http://127.0.0.1:7060/
@@ -92,11 +94,13 @@ IPANDPORT=http://localhost:7061/
#redis #redis
#Redis\u670D\u52A1\u5668\u5730\u5740 #Redis\u670D\u52A1\u5668\u5730\u5740
spring.redis.host=192.168.144.29 spring.redis.host=192.168.144.29
#spring.redis.host=192.168.1.103
#spring.redis.host=localhost #spring.redis.host=localhost
## Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3 ## Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
spring.redis.port=6379 spring.redis.port=6379
## Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09 ## Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
spring.redis.password=123321 spring.redis.password=123321
#spring.redis.password=
## \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09 ## \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09
spring.redis.timeout=200000ms spring.redis.timeout=200000ms
## \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5\uFF0C\u9ED8\u8BA4\u503C\u662F8\u3002 ## \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5\uFF0C\u9ED8\u8BA4\u503C\u662F8\u3002
@@ -113,6 +117,9 @@ spring.redis.database=6
# \u4E0A\u4F20\u65B9\u5F0F\uFF1A\u963F\u91CC\u4E91\uFF1Aalioss \u534E\u4E3A\u4E91\uFF1Ahwobs \u672C\u5730\uFF1Alocal # \u4E0A\u4F20\u65B9\u5F0F\uFF1A\u963F\u91CC\u4E91\uFF1Aalioss \u534E\u4E3A\u4E91\uFF1Ahwobs \u672C\u5730\uFF1Alocal
uploadType: local uploadType: local
# \u672C\u5730\u6587\u4EF6\u5B58\u50A8\u5730\u5740
localFilePath=D:\\uploadfile\\
# ======= \u963F\u91CCOSS\u914D\u7F6E ========= # ======= \u963F\u91CCOSS\u914D\u7F6E =========
aliyun.OSS.endpoint=https://oss-cn-hangzhou.aliyuncs.com aliyun.OSS.endpoint=https://oss-cn-hangzhou.aliyuncs.com
aliyun.OSS.accessKeyId= LTAI5tBmUfVF6Z8R6sHTdTG7 aliyun.OSS.accessKeyId= LTAI5tBmUfVF6Z8R6sHTdTG7
@@ -93,6 +93,9 @@ public class IReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity>
@Autowired @Autowired
private RoleEODao roleEODao; private RoleEODao roleEODao;
@Autowired
private LocalFileUtil localFileUtil;
/** /**
* 新增或编辑报告 * 新增或编辑报告
* *
@@ -541,7 +544,7 @@ public class IReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity>
} else if ("alioss".equals(uploadType)) { } else if ("alioss".equals(uploadType)) {
ossClientUtil.getFileFromOSS(fileEntity.getSavePath(), outputStream); ossClientUtil.getFileFromOSS(fileEntity.getSavePath(), outputStream);
} else if ("local".equals(uploadType)) { } else if ("local".equals(uploadType)) {
// do nothing localFileUtil.getFileFromLocal(fileEntity.getSavePath(), outputStream);
} }
String html = null; String html = null;
@@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.*; import java.io.*;
import java.net.URISyntaxException;
/** /**
* @author Caihaohan * @author Caihaohan
@@ -17,8 +16,11 @@ public class LocalFileUtil {
@Value("${uploadFile}") @Value("${uploadFile}")
private String uploadFile; private String uploadFile;
@Value("${localFilePath}")
private String localFilePath;
public void saveFile(InputStream fileInputStream, String fileName) throws IOException { public void saveFile(InputStream fileInputStream, String fileName) throws IOException {
String path = uploadFile + File.separator + fileName; String path = localFilePath + fileName;
FileOutputStream fileOutputStream = new FileOutputStream(path); FileOutputStream fileOutputStream = new FileOutputStream(path);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int length; int length;
@@ -29,4 +31,29 @@ public class LocalFileUtil {
fileOutputStream.close(); fileOutputStream.close();
} }
public void getFileFromLocal(String savePath, FileOutputStream outputStream) throws IOException {
// 创建源文件对象
File sourceFile = new File(localFilePath, savePath);
// 如果源文件不存在,则抛出异常
if (!sourceFile.exists()) {
throw new IOException("Source file does not exist.");
}
// 创建输入流
FileInputStream inputStream = new FileInputStream(sourceFile);
// 创建缓冲区
byte[] buffer = new byte[1024];
int bytesRead;
// 从输入流读取数据,并写入输出流
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 关闭输入流和输出流
inputStream.close();
outputStream.close();
}
} }
@@ -4,6 +4,8 @@ import com.adc.da.util.utils.UUID;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@@ -14,15 +16,24 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Slf4j @Slf4j
@Component
public class PdfUtil { public class PdfUtil {
private static String uploadType;
@Value("${uploadType}")
public void setUploadType(String uploadType) {
PdfUtil.uploadType = uploadType;
}
private PdfUtil() { private PdfUtil() {
throw new IllegalStateException("Utility class");
} }
/** /**
* 截取pdf成图片 * 截取pdf成图片
*
* @param inputStream * @param inputStream
* @param targetPath * @param targetPath
* @param ipAddress * @param ipAddress
@@ -54,14 +65,17 @@ public class PdfUtil {
File uploadFile = new File(targetPath + "/" + fileName + "/" + i + ".png"); File uploadFile = new File(targetPath + "/" + fileName + "/" + i + ".png");
try (FileOutputStream fops = new FileOutputStream(uploadFile);) { try (FileOutputStream fops = new FileOutputStream(uploadFile);) {
fops.write(readInputStream(byteInputStream)); fops.write(readInputStream(byteInputStream));
} } catch (Exception e) {
catch (Exception e){
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
//判断是否是服务器环境
if ("local".equals(uploadType)) {
list.add(targetPath + fileName + "/" + i + ".png");
} else {
list.add(ipAddress + "api/home/pic" + "/" + fileName + "/" + i + ".png"); list.add(ipAddress + "api/home/pic" + "/" + fileName + "/" + i + ".png");
} }
catch (Exception e){ } catch (Exception e) {
} }