feat: 本地上传功能
增加本地上传功能,需在配置文件中配置
This commit is contained in:
@@ -93,6 +93,9 @@ public class IReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity>
|
||||
@Autowired
|
||||
private RoleEODao roleEODao;
|
||||
|
||||
@Autowired
|
||||
private LocalFileUtil localFileUtil;
|
||||
|
||||
/**
|
||||
* 新增或编辑报告
|
||||
*
|
||||
@@ -541,7 +544,7 @@ public class IReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity>
|
||||
} else if ("alioss".equals(uploadType)) {
|
||||
ossClientUtil.getFileFromOSS(fileEntity.getSavePath(), outputStream);
|
||||
} else if ("local".equals(uploadType)) {
|
||||
// do nothing
|
||||
localFileUtil.getFileFromLocal(fileEntity.getSavePath(), outputStream);
|
||||
}
|
||||
|
||||
String html = null;
|
||||
|
||||
@@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
/**
|
||||
* @author Caihaohan
|
||||
@@ -17,8 +16,11 @@ public class LocalFileUtil {
|
||||
@Value("${uploadFile}")
|
||||
private String uploadFile;
|
||||
|
||||
@Value("${localFilePath}")
|
||||
private String localFilePath;
|
||||
|
||||
public void saveFile(InputStream fileInputStream, String fileName) throws IOException {
|
||||
String path = uploadFile + File.separator + fileName;
|
||||
String path = localFilePath + fileName;
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(path);
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
@@ -29,4 +31,29 @@ public class LocalFileUtil {
|
||||
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 org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -14,28 +16,37 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class PdfUtil {
|
||||
|
||||
|
||||
private PdfUtil(){
|
||||
throw new IllegalStateException("Utility class");
|
||||
private static String uploadType;
|
||||
|
||||
@Value("${uploadType}")
|
||||
public void setUploadType(String uploadType) {
|
||||
PdfUtil.uploadType = uploadType;
|
||||
}
|
||||
|
||||
private PdfUtil() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取pdf成图片
|
||||
*
|
||||
* @param inputStream
|
||||
* @param targetPath
|
||||
* @param ipAddress
|
||||
* @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);
|
||||
List<String> list=new ArrayList<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
int pageCount = doc.getNumberOfPages();
|
||||
BufferedImage image = null;
|
||||
log.info("开始时间"+System.currentTimeMillis());
|
||||
log.info("开始时间" + System.currentTimeMillis());
|
||||
String fileName = UUID.randomUUID10();
|
||||
for (int i = 0; i < pageCount; i++) {
|
||||
image = renderer.renderImage(i, 2.0f);
|
||||
@@ -46,32 +57,35 @@ public class PdfUtil {
|
||||
imOut = ImageIO.createImageOutputStream(bs);
|
||||
ImageIO.write(image, "png", imOut);
|
||||
|
||||
try(ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bs.toByteArray());){
|
||||
File ff=new File(targetPath+"/"+fileName);
|
||||
if(!ff.exists()){
|
||||
try (ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bs.toByteArray());) {
|
||||
File ff = new File(targetPath + "/" + fileName);
|
||||
if (!ff.exists()) {
|
||||
ff.mkdirs();
|
||||
}
|
||||
File uploadFile = new File(targetPath+"/"+fileName+"/"+i+".png");
|
||||
try(FileOutputStream fops = new FileOutputStream(uploadFile);){
|
||||
File uploadFile = new File(targetPath + "/" + fileName + "/" + i + ".png");
|
||||
try (FileOutputStream fops = new FileOutputStream(uploadFile);) {
|
||||
fops.write(readInputStream(byteInputStream));
|
||||
}
|
||||
catch (Exception e){
|
||||
log.error(e.getMessage(),e);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
list.add(ipAddress+"api/home/pic"+"/"+fileName+"/"+i+".png");
|
||||
}
|
||||
catch (Exception e){
|
||||
//判断是否是服务器环境
|
||||
if ("local".equals(uploadType)) {
|
||||
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;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(),e);
|
||||
log.error(e.getMessage(), e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -88,15 +102,15 @@ public class PdfUtil {
|
||||
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("</style>\r\n");
|
||||
for(int i=0;i<baseList.size();i++){
|
||||
buffer.append("<img class ='preview-img' src="+baseList.get(i)+" />");
|
||||
for (int i = 0; i < baseList.size(); i++) {
|
||||
buffer.append("<img class ='preview-img' src=" + baseList.get(i) + " />");
|
||||
}
|
||||
|
||||
buffer.append("</body></html>");
|
||||
try {
|
||||
return buffer.toString();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(),e);
|
||||
log.error(e.getMessage(), e);
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -109,7 +123,7 @@ public class PdfUtil {
|
||||
|
||||
public static byte[] readInputStream(InputStream inStream) throws Exception {
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024*1024];
|
||||
byte[] buffer = new byte[1024 * 1024];
|
||||
int len = 0;
|
||||
while ((len = inStream.read(buffer)) != -1) {
|
||||
outStream.write(buffer, 0, len);
|
||||
|
||||
Reference in New Issue
Block a user