package com.mochasoft.maritimecourt.shipreport.service.impl;
import com.mochasoft.maritimecourt.common.utils.HttpClientUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* @author zb
* @date 2022/4/28 14:24
*/
public class WordToPdf {
public static void main(String[] args) {
File dir = new File("C:/Users/周斌/Desktop/wordToPdf");
List<File> allFileList = new ArrayList<>();
// 判断文件夹是否存在
if (!dir.exists()) {
System.out.println("目录不存在");
return;
}
getAllFile(dir, allFileList);
int i = 1;
for (File file : allFileList) {
String filePath = file.getPath();
String[] filePathArray = filePath.split(File.separator + File.separator);
HashMap<String, String> params = new HashMap<>();
HashMap<String, String> heads = new HashMap<>();
String fileName = file.getName();
String suffixFileName = fileName.substring(fileName.lastIndexOf(".") + 1);
if (suffixFileName == "doc" || suffixFileName == "docx") {
}
String url = "http://192.168.2.191:10090/office/process?wordPath=/data/wordToPdf/" + filePathArray[5] + file.getName() + "&taskType=CONVERT&convertType=pdf";
String respDataStr = httpPost(url, params, heads);
JSONParser parse = new JSONParser();
JSONObject respDataObject = null;
try {
respDataObject = (JSONObject) parse.parse(respDataStr);
} catch (ParseException e) {
e.printStackTrace();
}
String receiveRespData = respDataObject != null ? respDataObject.get("data").toString() : null;
System.out.println("fileName:" + file.getName() + ",");
}
System.out.println("该文件夹下共有" + allFileList.size() + "个文件");
}
public void replaceName() {
// String fileName = file.getName();
// String filePath = file.getPath();
// String[] filePathArray = filePath.split(File.separator + File.separator);
// String newFileName = i+"."+fileName.substring(fileName.lastIndexOf(".") + 1);
// i++;
// String newFilePath = filePathArray[0]+"/"+filePathArray[1]+"/"+filePathArray[2]+"/"+filePathArray[3]+"/"
// +filePathArray[4]+"/"+filePathArray[5]+"/"+newFileName;
// file.renameTo(new File(newFilePath));
}
private static void setPostHead(HttpPost httpPost, Map<String, String> headMap) {
if (headMap != null && headMap.size() > 0) {
Set<String> keySet = headMap.keySet();
for (String key : keySet) {
httpPost.addHeader(key, headMap.get(key));
}
}
}
private static void setPostParams(HttpPost httpPost, Map<String, String> paramsMap)
throws Exception {
if (paramsMap != null && paramsMap.size() > 0) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<String> keySet = paramsMap.keySet();
for (String key : keySet) {
nvps.add(new BasicNameValuePair(key, paramsMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
}
}
private static String getRespString(HttpEntity entity) throws Exception {
if (entity == null) {
return null;
}
InputStream is = entity.getContent();
StringBuffer strBuf = new StringBuffer();
byte[] buffer = new byte[4096];
int r = 0;
while ((r = is.read(buffer)) > 0) {
strBuf.append(new String(buffer, 0, r, "UTF-8"));
}
return strBuf.toString();
}
public static String httpPost(String url, Map<String, String> paramsMap,
Map<String, String> headMap) {
String responseContent = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
setPostHead(httpPost, headMap);
setPostParams(httpPost, paramsMap);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
responseContent = getRespString(entity);
EntityUtils.consume(entity);
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("responseContent = " + responseContent);
return responseContent;
}
public static void getAllFile(File fileInput, List<File> allFileList) {
// 获取文件列表
File[] fileList = fileInput.listFiles();
assert fileList != null;
for (File file : fileList) {
if (file.isDirectory()) {
// 递归处理文件夹
// 如果不想统计子文件夹则可以将下一行注释掉
getAllFile(file, allFileList);
} else {
// 如果是文件则将其加入到文件数组中
allFileList.add(file);
}
}
}
}