Commit 446e3531 authored by 廖旭伟's avatar 廖旭伟

增加综窗接口文件预览功能

parent 53e19463
......@@ -233,6 +233,70 @@ public class WordUtil {
}
public static BufferedImage convertWordToJPEG(String inputFile) {
try {
System.out.println(String.format("文件转换开始:%s", DateUtils.getCurrStrDateTime()));
// 转换开始前时间
long old = System.currentTimeMillis();
// 要转换的word文档的路径
Document doc = new Document(inputFile);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
//doc.save(os, SaveFormat.JPEG);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
options.setPrettyFormat(true);
options.setUseAntiAliasing(true);
options.setUseHighQualityRendering(true);
int pageCount = doc.getPageCount();
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 0; i < pageCount; i++) {
OutputStream output = new ByteArrayOutputStream();
options.setPageIndex(i);
doc.save(output, options);
ImageInputStream imageInputStream = ImageIO.createImageInputStream(parse(output));
imageList.add(ImageIO.read(imageInputStream));
}
BufferedImage mergeImage = mergeImage(false, imageList);
// 转换结束后时间
long now = System.currentTimeMillis();
//os.close();
System.out.println("文件转换结束,共耗时:" + ((now - old) / 1000.0) + "秒");
return mergeImage;
} catch (Exception e) {
e.printStackTrace();
throw new AppException(String.format("文件转换异常!", e.getMessage()));
}
}
public static BufferedImage pdfToImages(String filePath) {
log.info(String.format("pdf to images文件转换开始:%s", DateUtils.getCurrStrDateTime()));
// 转换开始前时间
long old = System.currentTimeMillis();
List<BufferedImage> imageInfoList = new ArrayList<>();
try (PDDocument doc = PDDocument.load(new File(filePath))) {
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImage(i);
imageInfoList.add(image);
}
BufferedImage mergeImage = mergeImage(false, imageInfoList);
// 转换结束后时间
long now = System.currentTimeMillis();
//os.close();
log.info("文件转换结束,共耗时:" + ((now - old) / 1000.0) + "秒");
return mergeImage;
} catch (IOException e) {
log.error("生成预览图片异常:", e);
throw new AppException("生成预览图片异常");
}
}
public static void main(String[] args) {
String docPath = "E:\\pic\\doc\\2.docx";
......
......@@ -19,6 +19,7 @@ import com.mortals.xhx.common.code.complex.SfYofn;
import com.mortals.xhx.common.code.complex.StepResult;
import com.mortals.xhx.common.code.complex.YesNo;
import com.mortals.xhx.common.utils.AESUtil;
import com.mortals.xhx.common.utils.WordUtil;
import com.mortals.xhx.module.matter.service.UserMatterApplyService;
import com.mortals.xhx.module.sst.feign.IApiComplexFeign;
import com.mortals.xhx.module.sst.pdu.*;
......@@ -37,6 +38,8 @@ import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
......@@ -329,7 +332,7 @@ public class SyntheticalController extends BaseJsonBodyController {
}
/**
* 查询已受理事项列表
* 下载文件
* @param pdu
* @return
*/
......@@ -340,7 +343,7 @@ public class SyntheticalController extends BaseJsonBodyController {
url += "/complex/api/common/downloadFileFtp";
String resp = null;
Map<String, Object> model = new HashMap();
String busiDesc = "查询已受理事项列表" ;
String busiDesc = "下载文件" ;
try {
resp = iApiComplexFeign.downloadFileFtp(pdu);
Rest<String> eventRest = JSON.parseObject(resp, new TypeReference<Rest<String>>() {
......@@ -413,7 +416,6 @@ public class SyntheticalController extends BaseJsonBodyController {
/**
* 查询字典
* @param query
* @return
*/
@PostMapping({"api/getDict"})
......@@ -431,6 +433,80 @@ public class SyntheticalController extends BaseJsonBodyController {
return JSON.toJSONString(ret);
}
/**
* 预览doc
* @param pdu
* @return
*/
@PostMapping({"api/common/preview"})
@UnAuth
public String convertWordToJPEG(@RequestBody FindHandlingPageReq pdu) {
String resp = null;
Map<String, Object> model = new HashMap();
String busiDesc = "预览文件" ;
try {
resp = iApiComplexFeign.downloadFileFtp(pdu);
Rest<String> eventRest = JSON.parseObject(resp, new TypeReference<Rest<String>>() {
});
if(eventRest.getCode()==1 && eventRest.getData().length()>1024){
String urlStr = pdu.getUrl();
String fileName = urlStr.substring(urlStr.lastIndexOf("/")+1);
String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
byte[] data = Base64.getDecoder().decode(eventRest.getData());
if(data.length>1024) {
String rootPath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/";
String filePath = rootPath + "temp/" + fileName;
OutputStream outputStream = null;
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
outputStream = new FileOutputStream(file);
outputStream.write(data);
outputStream.flush();
outputStream.close();
BufferedImage image = null;
if(fileType.equals("doc")||fileType.equals("docx")){
image = WordUtil.convertWordToJPEG(filePath);
super.doResponseImage(response, image);
}else if(fileType.equals("pdf")){
image = WordUtil.pdfToImages(filePath);
super.doResponseImage(response, image);
}else if(fileType.equals("jpg")||fileType.equals("png")){
image = ImageIO.read(new File(filePath));
super.doResponseImage(response, image);
}else {
Rest<Object> ret = new Rest();
ret.setCode(-1);
ret.setMsg("不支持的文件格式");
resp = JSON.toJSONString(ret);
}
}else {
Rest<Object> ret = new Rest();
ret.setCode(-1);
ret.setMsg("文件不存在");
resp = JSON.toJSONString(ret);
}
}else {
Rest<Object> ret = new Rest();
ret.setCode(-1);
ret.setMsg("调用综窗接口失败");
resp = JSON.toJSONString(ret);
}
this.recordSysLog(this.request, busiDesc + " 【成功】");
} catch (Exception e) {
this.doException(this.request, busiDesc + " 【异常】", model, e);
Rest<Object> ret = new Rest();
ret.setCode(-1);
ret.setMsg(busiDesc + " 【异常】");
resp = JSON.toJSONString(ret);
}
return resp;
}
private String doPost(String url,String body) throws Exception{
HttpRequest httpRequest = HttpRequest.post(url);
httpRequest.body(body);
......@@ -445,30 +521,30 @@ public class SyntheticalController extends BaseJsonBodyController {
return resp;
}
// public static void main(String[] args){
// String url = "http://8.136.255.30:1086/complex/api/event-implementation/get-by-id";
// JSONObject jsonObject=new JSONObject();
// System.out.println(jsonObject.toJSONString());
// jsonObject.put("eventId","2e4575157ed27dbc7330b1595207b49c");
// String resp = null;
// Map<String, Object> model = new HashMap();
// HashMap<String, String> paramsMap = new HashMap<>();
// try {
// HttpRequest httpRequest = HttpRequest.post(url);
// httpRequest.body(jsonObject.toJSONString());
// HttpResponse execute = httpRequest.execute();
//
// // 6. 解析这个http响应类,可以获取到响应主体、cookie、是否请求成功等信息
// boolean ok = execute.isOk(); // 是否请求成功 判断依据为:状态码范围在200~299内
// System.out.println(ok);
// List<HttpCookie> cookies = execute.getCookies();// 获取所有cookie
// cookies.forEach(System.out::println); // 如果为空不会遍历的
// String body = execute.body(); // 获取响应主体
// System.out.println(body);
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public static void main(String[] args){
String url = "http://8.136.255.30:1086/complex/api/event-implementation/get-by-id";
JSONObject jsonObject=new JSONObject();
System.out.println(jsonObject.toJSONString());
jsonObject.put("eventId","2e4575157ed27dbc7330b1595207b49c");
String resp = null;
Map<String, Object> model = new HashMap();
HashMap<String, String> paramsMap = new HashMap<>();
try {
HttpRequest httpRequest = HttpRequest.post(url);
httpRequest.body(jsonObject.toJSONString());
HttpResponse execute = httpRequest.execute();
// 6. 解析这个http响应类,可以获取到响应主体、cookie、是否请求成功等信息
boolean ok = execute.isOk(); // 是否请求成功 判断依据为:状态码范围在200~299内
System.out.println(ok);
List<HttpCookie> cookies = execute.getCookies();// 获取所有cookie
cookies.forEach(System.out::println); // 如果为空不会遍历的
String body = execute.body(); // 获取响应主体
System.out.println(body);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment