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

This commit is contained in:
gaojiabao
2023-06-19 13:43:48 +08:00
parent 1ffad40423
commit 11c1a29710
4 changed files with 394 additions and 31 deletions
@@ -0,0 +1,310 @@
package com.adc.da.utils.util;
import com.adc.da.http.ResponseMessage;
import com.adc.da.http.Result;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class OnlyofficePdfUtil {
@Value("${file.path}")
private String filePathInfo;//文件存储路径
public ResponseMessage downloadFile(String filePdfName, String key, String onlineFilePath) throws Exception {
String filePath = filePathInfo+"/onlineFile/"+filePdfName;
System.out.println("================"+filePath);
String fileUrl = convertFile(key,onlineFilePath);
System.out.println("=============================="+fileUrl);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpGet get = new HttpGet(fileUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(60000)
.setConnectionRequestTimeout(60000)
.setConnectTimeout(60000)
.build();
get.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
//发送get请求
response = closeableHttpClient.execute(get);
if (response == null || response.getStatusLine() == null) {
throw new Exception("发送GET请求失败,返回结果为空!");
}
//返回状态是否200
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
//得到请求结果
HttpEntity entityRes = response.getEntity();
if (entityRes != null) {
InputStream content = entityRes.getContent();
byte[] getData = readInputStream(content);
File file = new File(filePath);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
fos.close();
}
} else {
throw new Exception("发送GET请求出错:" + response);
}
} catch (Exception e) {
throw new Exception("发送GET请求出现异常," + e.getMessage());
} finally {
try {
// 关闭连接释放资源
if (response != null) {
response.close();
}
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return Result.success();
}
public String analysisXML(String xmlString) throws DocumentException {
Document document = DocumentHelper.parseText(xmlString);
Element rootElement = document.getRootElement();
String fileUrl = rootElement.elementText("FileUrl");
String newFileUrl = fileUrl.replaceAll("&", "&");
return newFileUrl;
}
public String convertFile(String key,String onlineFilePath) throws Exception{
String url = "http://47.92.92.38:8103/7.2.1-34/ConvertService.ashx";
Map<String,Object> map = new HashMap<>();
map.put("async",false);
map.put("filetype","docx");
map.put("key",key);
map.put("outputtype","pdf");
map.put("title","test.pdf");
map.put("url","https://srms.foton.com.cn/file/"+onlineFilePath);
//map.put("url","https://srms.foton.com.cn/file/model/task_768330d8/task_768330d8_top_768330d8_node.docx");
JSONObject requestJson = new JSONObject(map);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(60000)
.setConnectionRequestTimeout(60000)
.setConnectTimeout(60000)
.build();
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
//设置post请求方式为JSON
StringEntity data = new StringEntity(String.valueOf(requestJson), "UTF-8");
data.setContentType("application/json; charset=UTF-8");
httpPost.setEntity(data);
//发送post请求
response = closeableHttpClient.execute(httpPost);
if (response == null || response.getStatusLine() == null) {
throw new Exception("发送POST请求失败,返回结果为空!");
}
//返回状态是否200
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
//得到请求结果
HttpEntity entityRes = response.getEntity();
if (entityRes != null) {
String responsetData = EntityUtils.toString(entityRes, "UTF-8");
String fileUrl = analysisXML(responsetData);
return fileUrl;
}
} else {
throw new Exception("发送POST-JSON请求出错:" + response);
}
} catch (Exception e) {
throw new Exception("发送POST请求出现异常," + e.getMessage());
} finally {
try {
// 关闭连接释放资源
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();
}
}