test:测试调用在线编辑文件获取

This commit is contained in:
wxyclub
2023-06-29 17:00:23 +08:00
parent f73bfdf8e2
commit d33d1f769a
2 changed files with 29 additions and 42 deletions
@@ -5,6 +5,7 @@ import com.adc.da.http.ResponseMessage;
import com.adc.da.http.Result;
import com.adc.da.slrs.sarBussionessStand.service.impl.SarBussionessStandServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
@@ -21,20 +22,26 @@ import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class OnlyofficePdfUtil {
private static final Logger logger = LoggerFactory.getLogger(SarBussionessStandServiceImpl.class);
@Value("${onlyoffice.url}")
private static String onlyOfficeUrl; //onlyoffice文件转换服务URL
public static ResponseMessage downloadFile(String url,String filePathInfo,String filePdfName, String key, String onlineFilePath) throws Exception {
public static File downloadFile(String filePathInfo,String filePdfName, String key, String onlineFilePath) throws Exception {
String filePath = filePathInfo+"/onlineFile/"+filePdfName;
logger.info("================================="+filePath);
String fileUrl = convertFile(url,filePdfName,key,onlineFilePath);
logger.info("====================filePdfName:{},key:{},onlineFilePath:{}",filePdfName,key,onlineFilePath);
String fileUrl = convertFile(filePdfName,key,onlineFilePath);
logger.info("================================="+fileUrl);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
@@ -62,11 +69,15 @@ public class OnlyofficePdfUtil {
HttpEntity entityRes = response.getEntity();
if (entityRes != null) {
InputStream content = entityRes.getContent();
byte[] getData = readInputStream(content);
// byte[] getData = readInputStream(content);
// File file = new File(filePath);
// FileOutputStream fos = new FileOutputStream(file);
// fos.write(getData);
// fos.close();
// File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("")).getPath()+ filePdfName);
File file = new File(filePath);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
fos.close();
FileUtils.copyInputStreamToFile(content,file);
return file;
}
} else {
throw new Exception("发送GET请求出错:" + response);
@@ -87,8 +98,8 @@ public class OnlyofficePdfUtil {
e.printStackTrace();
}
}
return Result.success();
// return Result.success();
return null;
}
public static String analysisXML(String xmlString) throws DocumentException {
@@ -99,7 +110,7 @@ public class OnlyofficePdfUtil {
return newFileUrl;
}
public static String convertFile(String url,String titleName,String key, String onlineFilePath) throws Exception{
public static String convertFile(String titleName,String key, String onlineFilePath) throws Exception{
//String url = "http://47.92.92.38:8103/7.2.1-34/ConvertService.ashx";
// String url = "http://10.100.5.116:8088/7.2.1-34/ConvertService.ashx";
Map<String,Object> map = new HashMap<>();
@@ -108,16 +119,18 @@ public class OnlyofficePdfUtil {
map.put("key",key);
map.put("outputtype","pdf");
map.put("title",titleName+".pdf");
// map.put("url","https://10.100.5.111:3306/file/"+onlineFilePath);
map.put("url","https://srms.foton.com.cn/file/"+onlineFilePath);
logger.info("=======================================https://srms.foton.com.cn/file/"+onlineFilePath);
logger.info("=======================================服务器路径"+url);
//map.put("url","https://srms.foton.com.cn/file/model/task_768330d8/task_768330d8_top_768330d8_node.docx");
logger.info("=========================map:{}",map);
JSONObject requestJson = new JSONObject(map);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
logger.info("=======================================服务器路径"+onlyOfficeUrl);
HttpPost httpPost = new HttpPost(onlyOfficeUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(60000)
@@ -159,40 +172,13 @@ public class OnlyofficePdfUtil {
if (response != null) {
response.close();
}
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}