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,28 +16,37 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Slf4j @Slf4j
@Component
public class PdfUtil { public class PdfUtil {
private PdfUtil(){ private static String uploadType;
throw new IllegalStateException("Utility class");
@Value("${uploadType}")
public void setUploadType(String uploadType) {
PdfUtil.uploadType = uploadType;
}
private PdfUtil() {
} }
/** /**
* 截取pdf成图片 * 截取pdf成图片
*
* @param inputStream * @param inputStream
* @param targetPath * @param targetPath
* @param ipAddress * @param ipAddress
* @return * @return
*/ */
public static String pdfStreamToPng( InputStream inputStream,String targetPath,String ipAddress) { public static String pdfStreamToPng(InputStream inputStream, String targetPath, String ipAddress) {
try(PDDocument doc=PDDocument.load( inputStream)) { try (PDDocument doc = PDDocument.load(inputStream)) {
PDFRenderer renderer = new PDFRenderer(doc); PDFRenderer renderer = new PDFRenderer(doc);
List<String> list=new ArrayList<>(); List<String> list = new ArrayList<>();
int pageCount = doc.getNumberOfPages(); int pageCount = doc.getNumberOfPages();
BufferedImage image = null; BufferedImage image = null;
log.info("开始时间"+System.currentTimeMillis()); log.info("开始时间" + System.currentTimeMillis());
String fileName = UUID.randomUUID10(); String fileName = UUID.randomUUID10();
for (int i = 0; i < pageCount; i++) { for (int i = 0; i < pageCount; i++) {
image = renderer.renderImage(i, 2.0f); image = renderer.renderImage(i, 2.0f);
@@ -46,32 +57,35 @@ public class PdfUtil {
imOut = ImageIO.createImageOutputStream(bs); imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(image, "png", imOut); ImageIO.write(image, "png", imOut);
try(ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bs.toByteArray());){ try (ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bs.toByteArray());) {
File ff=new File(targetPath+"/"+fileName); File ff = new File(targetPath + "/" + fileName);
if(!ff.exists()){ if (!ff.exists()) {
ff.mkdirs(); ff.mkdirs();
} }
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);
} }
list.add(ipAddress+"api/home/pic"+"/"+fileName+"/"+i+".png"); //判断是否是服务器环境
} if ("local".equals(uploadType)) {
catch (Exception e){ list.add(targetPath + fileName + "/" + i + ".png");
} else {
list.add(ipAddress + "api/home/pic" + "/" + fileName + "/" + i + ".png");
}
} catch (Exception e) {
} }
} }
log.info("结束时间"+System.currentTimeMillis()); log.info("结束时间" + System.currentTimeMillis());
String htmlByBase64 = createHtmlByImg(list); String htmlByBase64 = createHtmlByImg(list);
return htmlByBase64; return htmlByBase64;
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(), e);
return ""; return "";
} }
} }
@@ -88,15 +102,15 @@ public class PdfUtil {
buffer.append("<style>\r\n"); buffer.append("<style>\r\n");
buffer.append(".preview-img {background-color:#fff; text-align:center; width:100%; max-width:100%;margin-top:6px;}\r\n"); buffer.append(".preview-img {background-color:#fff; text-align:center; width:100%; max-width:100%;margin-top:6px;}\r\n");
buffer.append("</style>\r\n"); buffer.append("</style>\r\n");
for(int i=0;i<baseList.size();i++){ for (int i = 0; i < baseList.size(); i++) {
buffer.append("<img class ='preview-img' src="+baseList.get(i)+" />"); buffer.append("<img class ='preview-img' src=" + baseList.get(i) + " />");
} }
buffer.append("</body></html>"); buffer.append("</body></html>");
try { try {
return buffer.toString(); return buffer.toString();
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(), e);
return ""; return "";
} }
@@ -109,7 +123,7 @@ public class PdfUtil {
public static byte[] readInputStream(InputStream inStream) throws Exception { public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*1024]; byte[] buffer = new byte[1024 * 1024];
int len = 0; int len = 0;
while ((len = inStream.read(buffer)) != -1) { while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len); outStream.write(buffer, 0, len);