|
@@ -2,7 +2,6 @@ package com.lc.ibps.cloud.file.provider;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.FileInputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
import java.net.URLConnection;
|
|
@@ -12,12 +11,20 @@ import java.util.Map;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.validation.constraints.NotNull;
|
|
import javax.validation.constraints.NotNull;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.lc.ibps.api.base.file.FileInfo;
|
|
import com.lc.ibps.api.base.file.FileInfo;
|
|
|
-import com.lc.ibps.base.core.encrypt.Base64;
|
|
|
|
|
import com.lc.ibps.base.db.util.TableMetaUtil;
|
|
import com.lc.ibps.base.db.util.TableMetaUtil;
|
|
|
-import com.lc.ibps.cloud.entity.APIRequest;
|
|
|
|
|
import com.lc.ibps.cloud.file.util.PDFFileUtil;
|
|
import com.lc.ibps.cloud.file.util.PDFFileUtil;
|
|
|
-import com.lc.ibps.components.upload.util.UploadUtil;
|
|
|
|
|
|
|
+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.HttpClients;
|
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
@@ -63,6 +70,9 @@ import io.swagger.annotations.ExtensionProperty;
|
|
|
@Service
|
|
@Service
|
|
|
public class UploadProvider extends GenericUploadProvider implements IUploadService, IUploadMgrService{
|
|
public class UploadProvider extends GenericUploadProvider implements IUploadService, IUploadMgrService{
|
|
|
|
|
|
|
|
|
|
+ @Value("${runqian.server}")
|
|
|
|
|
+ private String baseUrl; // 非静态字段
|
|
|
|
|
+
|
|
|
@ApiOperation(value = "文件上传", notes = "根据上传参数上传文件,请在网关地址后添加“/zuul”否则中文文件会出现乱码;\r\n"
|
|
@ApiOperation(value = "文件上传", notes = "根据上传参数上传文件,请在网关地址后添加“/zuul”否则中文文件会出现乱码;\r\n"
|
|
|
+ "request可选(暂时这样吧)参数1,"
|
|
+ "request可选(暂时这样吧)参数1,"
|
|
|
+ "uploadFileVo:{\r\n" +
|
|
+ "uploadFileVo:{\r\n" +
|
|
@@ -532,12 +542,15 @@ public class UploadProvider extends GenericUploadProvider implements IUploadServ
|
|
|
this.getUploadService();
|
|
this.getUploadService();
|
|
|
FileInfo fileInfo = uploadService.downloadFile(attachmentId);
|
|
FileInfo fileInfo = uploadService.downloadFile(attachmentId);
|
|
|
if (BeanUtils.isNotEmpty(fileInfo)) {
|
|
if (BeanUtils.isNotEmpty(fileInfo)) {
|
|
|
- if(!"pdf".equalsIgnoreCase(fileInfo.getExt())){
|
|
|
|
|
- result.setMessage("不支持非PDF文件!");
|
|
|
|
|
|
|
+ if(!"pdf".equalsIgnoreCase(fileInfo.getExt()) && !"doc".equalsIgnoreCase(fileInfo.getExt()) && !"docx".equalsIgnoreCase(fileInfo.getExt())){
|
|
|
|
|
+ result.setMessage("仅支持doc,docx,pdf格式的文件!");
|
|
|
result.setState(StateEnum.ERROR_ATTACHMENT.getCode());
|
|
result.setState(StateEnum.ERROR_ATTACHMENT.getCode());
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
byte[] fileBlob = fileInfo.getFileBytes();
|
|
byte[] fileBlob = fileInfo.getFileBytes();
|
|
|
|
|
+ if("doc".equalsIgnoreCase(fileInfo.getExt()) || "docx".equalsIgnoreCase(fileInfo.getExt())){
|
|
|
|
|
+ fileBlob = convertWordToPdf(fileInfo);
|
|
|
|
|
+ }
|
|
|
//InputStream input = new ByteArrayInputStream(fileBlob);
|
|
//InputStream input = new ByteArrayInputStream(fileBlob);
|
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
PDFFileUtil.stampByPdfbox(fileBlob,output,action);
|
|
PDFFileUtil.stampByPdfbox(fileBlob,output,action);
|
|
@@ -563,4 +576,43 @@ public class UploadProvider extends GenericUploadProvider implements IUploadServ
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public byte[] convertWordToPdf(FileInfo fileInfo) throws Exception {
|
|
|
|
|
+ String path = baseUrl + StringPool.SLASH + fileInfo.getFilePath();
|
|
|
|
|
+ String onlyOfficeUrl = baseUrl + ":9999/ConvertService.ashx";
|
|
|
|
|
+ Map<String,Object> paramMap = new HashMap<>();
|
|
|
|
|
+ paramMap.put("async", false);
|
|
|
|
|
+ paramMap.put("filetype", fileInfo.getExt());
|
|
|
|
|
+ paramMap.put("key", fileInfo.getId());
|
|
|
|
|
+ paramMap.put("outputtype", StringPool.FileType.Office.PDF);
|
|
|
|
|
+ paramMap.put("title", fileInfo.getFileName());
|
|
|
|
|
+ paramMap.put("url", path);
|
|
|
|
|
+ JSONObject retJson = new JSONObject(paramMap);
|
|
|
|
|
+
|
|
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
|
|
+ HttpPost httpPost = new HttpPost(onlyOfficeUrl);
|
|
|
|
|
+ httpPost.setHeader("Accept", "application/json");
|
|
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
|
|
+ httpPost.setEntity(new StringEntity(new Gson().toJson(retJson)));
|
|
|
|
|
+
|
|
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
|
|
+ String jsonResponse = EntityUtils.toString(response.getEntity());
|
|
|
|
|
+ JSONObject res = JSONUtil.parseObj(jsonResponse);
|
|
|
|
|
+ boolean endConvert = res.getBool("endConvert");
|
|
|
|
|
+ String fileUrl = res.getStr("fileUrl");
|
|
|
|
|
+
|
|
|
|
|
+ if (!endConvert || fileUrl == null) {
|
|
|
|
|
+ throw new RuntimeException("Conversion failed: " + jsonResponse);
|
|
|
|
|
+ }
|
|
|
|
|
+ HttpGet httpGet = new HttpGet(fileUrl);
|
|
|
|
|
+ try (CloseableHttpResponse getResponse = httpClient.execute(httpGet)) {
|
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
+ getResponse.getEntity().writeTo(outputStream);
|
|
|
|
|
+ fileInfo.setExt(StringPool.FileType.Office.PDF);
|
|
|
|
|
+ return outputStream.toByteArray();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|