Browse Source

wps 转换

master
PoffyZhang 1 year ago
parent
commit
ba0f2acb76
5 changed files with 148 additions and 4 deletions
  1. +16
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/wps/contants/WpsContant.java
  2. +13
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/wps/controller/WpsConvertController.java
  3. +95
    -3
      pmapi/src/main/java/com/ningdatech/pmapi/wps/manage/WpsConvertManage.java
  4. +5
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/wps/service/IConvertService.java
  5. +19
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/wps/service/impl/ConvertService.java

+ 16
- 0
pmapi/src/main/java/com/ningdatech/pmapi/wps/contants/WpsContant.java View File

@@ -0,0 +1,16 @@
package com.ningdatech.pmapi.wps.contants;

/**
* @Classname WpsContant
* @Description
* @Date 2023/8/15 14:09
* @Author PoffyZhang
*/
public interface WpsContant {

String WPS_CONVERT_URL_ONLINE = "http://10.53.157.47";
String FIX_FILE_PATH = "/opt/file/";
String PDF_PATH = "/opt/file/offices2pdf.pdf";
String PDF_CONTENT_TYPE = "application/pdf";
String PDF_NAME = "offices2pdf.pdf";
}

+ 13
- 1
pmapi/src/main/java/com/ningdatech/pmapi/wps/controller/WpsConvertController.java View File

@@ -13,8 +13,11 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

@@ -32,6 +35,10 @@ public class WpsConvertController {
@Autowired
private IRefreshTokenService refreshTokenService;

@Autowired
private WpsConvertManage wpsConvertManage;


@Value("${irs.interface-refresh.request-token-url}")
private String govRequestTokenUrl;
@Value("${irs.interface-refresh.refresh-token-url}")
@@ -39,7 +46,12 @@ public class WpsConvertController {

@GetMapping("/office2pdf")
public String office2pdf(){
return WpsConvertManage.offs2Pdf();
return wpsConvertManage.offs2Pdf();
}

@GetMapping("/downloadToPdfStream")
public void downloadToPdfStream(@RequestParam Long fileId, HttpServletResponse response){
wpsConvertManage.downloadToPdfStream(fileId,response);
}

@GetMapping("/test-core")


+ 95
- 3
pmapi/src/main/java/com/ningdatech/pmapi/wps/manage/WpsConvertManage.java View File

@@ -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();
}
}
}

+ 5
- 0
pmapi/src/main/java/com/ningdatech/pmapi/wps/service/IConvertService.java View File

@@ -0,0 +1,5 @@
package com.ningdatech.pmapi.wps.service;

public interface IConvertService {

}

+ 19
- 0
pmapi/src/main/java/com/ningdatech/pmapi/wps/service/impl/ConvertService.java View File

@@ -0,0 +1,19 @@
package com.ningdatech.pmapi.wps.service.impl;

import com.ningdatech.pmapi.wps.service.IConvertService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* @Classname ConvertService
* @Description
* @Date 2023/8/15 10:56
* @Author PoffyZhang
*/
@Service
@Slf4j
public class ConvertService implements IConvertService {



}

Loading…
Cancel
Save