#2.如何实现本地上传pdf文件同时转化为一张长图片上传到OSS服务器:
上传文件:
在这里插入代码片
//本地上传协议 pdf 和 一张完整的jpg/png到服务器的方法:
public String insertProtocol(MultipartFile file) throws IOException {
/*-------------------------上传协议 start ---------------------*/
//根据文件名进行查重,判断是否已经上传过该文件
String fileName = file.getOriginalFilename().replaceAll(" ", "");
String[] split = fileName.split("\\.");
String s = split[0];
Map<String, Object> map = new HashMap<String, Object>();
map.put("protocolName", s);
List<Map<String, Object>> list = protocolDao.list(map);
if (list.size() > 0) {
return "请不要重复上传!";
}
//判断上传的文件是否是pdf格式的:
if (!file.getOriginalFilename().endsWith(".pdf")) {
return "选择的文件格式错误,请刷新重试!";
}
Protocol protocol=new Protocol();
//获取上传文件的大小-插表
if (file != null && file.getSize() != 0) {
long size = file.getSize();
protocol.setProtocolSize(size);
String fileFormat = ".pdf";
//获取文件名+后缀
// map.put("protocolName", s);
String ossPath = s + fileFormat;
//上传文件:
//实例化ossFUtils工具类,否则报空指针:
OssFUtils ossFUtils = OssFUtils.getInstance();
//获取文件流:
InputStream inputStreamPDF = file.getInputStream();
//以流的形式调用阿里云的上传文件方法将文件上传到服务器
String descPath =ossFUtils.uploadFile("tope365-agent", inputStreamPDF, "agent/protocol/" + ossPath);
//获取文件在服务器上的下载地址:
String protocolyUrl ="";
//可直接下载地址
if(descPath !=null){
protocolyUrl = descPath.replace("tope365-agent@", properties.getProperty("tope365_agent_domain"));
}
//将下载地址更新入库:
protocol.setUploadUrl(protocolyUrl);
/*-------------------------上传协议 end ---------------------*/
转长图上传:
在这里插入代码片
/*-------------------------pdf转jpg/png start ---------------------*/
//合同文件对应的预览图片上传到oss 属性设置
String fileExtensionImg = ".png";
String newFileNameImg = s + fileExtensionImg;
String osspathImg = "agent/protocol/" + newFileNameImg;
InputStream inputStreamJpg = file.getInputStream();
//图像合并使用参数:
// 设置图片的总宽度:
int width = 0;
// 保存一张图片中的RGB数据:
int[] singleImgRGB;
int shiftHeight = 0;
//保存每张图片的像素值
BufferedImage imageResult = null;
// 加载解析PDF文件
//利用PdfBox生成图像
//注:使用pdf转图片时需要在pom中引入三个jar包,缺一不可:
PDDocument pdf = PDDocument.load(inputStreamJpg);
PDFTextStripper text = new PDFTextStripper();
String text1 = text.getText(pdf);
logger.info("读取PDF文字:" + text1);
PDFRenderer renderer = new PDFRenderer(pdf);
//循环文件的每个页码
for (int i = 0, len = pdf.getNumberOfPages(); i < len; i++) {
//dpi参数越大,越清晰
BufferedImage image = renderer.renderImageWithDPI(i, 200, ImageType.RGB);
//计算高度和偏移量
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
if (i == 0) {
//使用第一张图片宽度;
width = imageWidth;
//保存每页图片的像素值
imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
} else {
// 计算偏移高度
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
// 将解析的图像参数写入流中
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(imageResult, "png", os);
//获取流数组
byte[] datas = os.toByteArray();
//传入流中
inputStreamJpg = new ByteArrayInputStream(datas);
//调用阿里云方法进行上传:
ossFUtils = OssFUtils.getInstance();
//上传png
ossFUtils.uploadFile("tope365-agent", inputStreamJpg, osspathImg);
}
/*------------------------pdf转jpg/png end ---------------------*/
//入库
}
辅助:
1.引入依赖:注:使用pdf转图片时需要在pom中引入三个jar包,缺一不可:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.1</version>
</dependency>
**辅助:
2.如何将pdf文件转成一张长图片:jpg/png格式均可:
***将PDF转成一页长图:*****
```java
public static BufferedImage pdfChangeImage(InputStream inputStream) {
//图像合并使用参数:
// 设置图片的总宽度
int width = 0;
// 保存一张图片中的RGB数据
int[] singleImgRGB;
int shiftHeight = 0;
//保存每张图片的像素值
BufferedImage imageResult = null;
try {
//利用PdfBox生成图像
PDDocument pdDocument = PDDocument.load(inputStream);
PDFTextStripper text = new PDFTextStripper();
String text1 = text.getText(pdDocument);
logger.info("读取PDF文字:" + text1);
PDFRenderer renderer = new PDFRenderer(pdDocument);
//循环文件中的每个页码
for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
//dpi参数越大,越清晰
BufferedImage image = renderer.renderImageWithDPI(i, 300, ImageType.RGB);
//计算高度和偏移量
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
if (i == 0) {
//使用第一张图片宽度;
width = imageWidth;
//保存每页图片的像素值
imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
} else {
// 计算偏移高度
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
// 将图片参数写入流中
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
pdDocument.close();
} catch (Exception e) {
logger.error(" 从PDF转换JPG格式异常!", e);
}
return imageResult;
}
public static void main(String[] args) {
InputStream inputStream = null;
BufferedImage bufferedImage = null;
String url = "";
try {
//pdf转img
bufferedImage = PDFUtils.pdfChangeImage(new URL(url));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os);
inputStream = new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}