|
|
@@ -1,8 +1,25 @@ |
|
|
|
package com.ningdatech.pmapi.wps.manage; |
|
|
|
|
|
|
|
import cn.hutool.core.util.CharsetUtil; |
|
|
|
import com.aliyun.oss.OSS; |
|
|
|
import com.aliyun.oss.OSSClientBuilder; |
|
|
|
import com.aliyun.oss.model.OSSObject; |
|
|
|
import com.aliyun.oss.model.ObjectMetadata; |
|
|
|
import com.ningdatech.file.properties.FileServerProperties; |
|
|
|
import com.ningdatech.file.service.FileService; |
|
|
|
import com.ningdatech.pmapi.wps.contants.WpsContant; |
|
|
|
import com.suwell.ofd.custom.agent.AtomAgent; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.*; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
@@ -13,10 +30,18 @@ import java.util.List; |
|
|
|
* @Author PoffyZhang |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class WpsConvertManage { |
|
|
|
|
|
|
|
@Resource |
|
|
|
private FileService fileService; |
|
|
|
|
|
|
|
private final FileServerProperties fileServerProperties; |
|
|
|
|
|
|
|
// HTTPAgent ha = new HTTPAgent("http://127.0.0.1:8090"); |
|
|
|
//2、定义方法实现 多个文件合并转为1个pdf文件 |
|
|
|
public static String offs2Pdf() { |
|
|
|
public String offs2Pdf() { |
|
|
|
AtomAgent ha = new AtomAgent("http://10.53.157.47"); |
|
|
|
|
|
|
|
try { |
|
|
@@ -61,7 +86,74 @@ public class WpsConvertManage { |
|
|
|
return "转换失败"; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
offs2Pdf(); |
|
|
|
public void downloadToPdfStream(Long fileId, HttpServletResponse response) { |
|
|
|
com.ningdatech.file.entity.File file = fileService.getById(fileId); |
|
|
|
downloadAndToPdf(file, response); |
|
|
|
} |
|
|
|
|
|
|
|
private void downloadAndToPdf(com.ningdatech.file.entity.File file, HttpServletResponse response) { |
|
|
|
OSS oss = new OSSClientBuilder().build(fileServerProperties.getAli().getEndpoint(), fileServerProperties.getAli().getAccessKeyId(), fileServerProperties.getAli().getAccessKeySecret()); |
|
|
|
AtomAgent ha = new AtomAgent(WpsContant.WPS_CONVERT_URL_ONLINE); |
|
|
|
try (OSSObject ossObject = oss.getObject(file.getBucket(), file.getPath()); |
|
|
|
InputStream stream = ossObject.getObjectContent(); |
|
|
|
ServletOutputStream outputStream = response.getOutputStream()) { |
|
|
|
String filePath = WpsContant.FIX_FILE_PATH + file.getOriginalFileName(); |
|
|
|
convert(stream,filePath); |
|
|
|
//转换PDF |
|
|
|
List<File> fileList =new ArrayList<File>(); |
|
|
|
//2)、定义转换后的pdf文件输出流 |
|
|
|
OutputStream out = null; |
|
|
|
//3)、添加原文件到集合 |
|
|
|
fileList.add(new File(filePath)); |
|
|
|
//4)、赋值转换后的pdf文件输出流 |
|
|
|
File pdfFile = new File(WpsContant.PDF_PATH); |
|
|
|
out = new FileOutputStream(pdfFile); |
|
|
|
//5)、调用方法,执行将多个文件转为pdf文件 |
|
|
|
ha.OFDToPDF(fileList, out); |
|
|
|
FileInputStream fileInputStream = new FileInputStream(pdfFile); |
|
|
|
|
|
|
|
ObjectMetadata metadata = ossObject.getObjectMetadata(); |
|
|
|
response.setContentType(WpsContant.PDF_CONTENT_TYPE); |
|
|
|
String fileName = URLEncoder.encode(WpsContant.PDF_NAME, CharsetUtil.UTF_8); |
|
|
|
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName); |
|
|
|
byte[] bytes = new byte[4096]; |
|
|
|
int read; |
|
|
|
while ((read = fileInputStream.read(bytes)) != -1) { |
|
|
|
outputStream.write(bytes, 0, read); |
|
|
|
} |
|
|
|
outputStream.flush(); |
|
|
|
for(File f : fileList){ |
|
|
|
//用完就删 |
|
|
|
f.deleteOnExit(); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
log.error(e.getMessage()); |
|
|
|
}finally { |
|
|
|
oss.shutdown(); |
|
|
|
try { |
|
|
|
ha.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
log.error(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void convert(InputStream inputStream, String filePath) { |
|
|
|
try { |
|
|
|
File file = new File(filePath); |
|
|
|
FileOutputStream outputStream = new FileOutputStream(file); |
|
|
|
|
|
|
|
int bytesRead; |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) { |
|
|
|
outputStream.write(buffer, 0, bytesRead); |
|
|
|
} |
|
|
|
|
|
|
|
outputStream.close(); |
|
|
|
inputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |