|
|
@@ -1,119 +1,176 @@
|
|
|
package com.kingdee.shr.customer.gtiit.handler;
|
|
|
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.URLEncoder;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.apache.log4j.Logger;
|
|
|
-import org.springframework.ui.ModelMap;
|
|
|
-
|
|
|
import com.aspose.pdf.DocSaveOptions;
|
|
|
import com.aspose.pdf.Document;
|
|
|
import com.aspose.pdf.SaveFormat;
|
|
|
-import com.kingdee.bos.BOSException;
|
|
|
+import com.kingdee.bos.Context;
|
|
|
+import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
|
|
|
+import com.kingdee.eas.base.attachment.AttachmentFactory;
|
|
|
+import com.kingdee.eas.base.attachment.AttachmentInfo;
|
|
|
+import com.kingdee.eas.base.attachment.IAttachment;
|
|
|
+import com.kingdee.shr.base.syssetting.context.SHRContext;
|
|
|
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
|
|
|
+import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
|
|
|
import com.kingdee.shr.empCertification.web.handler.HRcertificationbillListHandler;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.ui.ModelMap;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class HRcertificationbillListHandlerEx extends HRcertificationbillListHandler{
|
|
|
|
|
|
private static Logger logger =
|
|
|
Logger.getLogger("com.kingdee.shr.customer.gtiit.handler.HRcertificationbillListHandlerEx");
|
|
|
-
|
|
|
+ //更新
|
|
|
public void convertPdfToWordAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
|
|
|
- logger.error("杩涘叆闄勪欢杞�崲鏂规硶");
|
|
|
- String downloadUrl = request.getParameter("downloadUrl");
|
|
|
- downloadUrl = downloadUrl.replace(" ", "+");
|
|
|
- String fileName = request.getParameter("fileName");
|
|
|
- logger.error("闄勪欢涓嬭浇璺�緞锛�"+downloadUrl);
|
|
|
- logger.error("闄勪欢鍚嶇О锛�"+fileName);
|
|
|
- File attachFile = null;
|
|
|
+ logger.error("进入附件转换方法");
|
|
|
+ String attachId = request.getParameter("attachId");
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ String fileName = request.getParameter("fileName");
|
|
|
+ logger.error("附件下载路径:"+attachId);
|
|
|
+ logger.error("附件名称:"+fileName);
|
|
|
+ Document doc= null;
|
|
|
+ ByteArrayInputStream fileInputStream= null;
|
|
|
+ ByteArrayOutputStream byteOutputStream= null;
|
|
|
try {
|
|
|
- // 鍒涘缓 URL 瀵硅薄
|
|
|
- URL url = new URL(downloadUrl);
|
|
|
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
- // 璁剧疆瓒呮椂鏃堕棿涓�100绉�
|
|
|
- conn.setConnectTimeout(100 * 1000);
|
|
|
- // 寰楀埌杈撳叆娴�
|
|
|
- InputStream inputStream = conn.getInputStream();
|
|
|
- // 鍒涘缓鏂囦欢杈撳嚭娴�
|
|
|
- attachFile = File.createTempFile("temp", ".pdf");
|
|
|
- // 灏嗚緭鍏ユ祦涓�殑鏁版嵁鍐欏叆涓存椂鏂囦欢
|
|
|
- FileOutputStream outputStream = new FileOutputStream(attachFile);
|
|
|
- // 璇诲彇杈撳叆娴佷腑鐨勬暟鎹�苟鍐欏叆杈撳嚭娴�
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int bytesRead;
|
|
|
- while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
- outputStream.write(buffer, 0, bytesRead);
|
|
|
+ Context ctx = SHRContext.getInstance().getContext();
|
|
|
+ IAttachment attachment = AttachmentFactory.getLocalInstance(ctx);
|
|
|
+ AttachmentInfo info = attachment.getAttachmentInfo(new ObjectUuidPK(attachId));
|
|
|
+
|
|
|
+ fileInputStream = new ByteArrayInputStream(info.getFile());
|
|
|
+ // 创建输出流
|
|
|
+ byteOutputStream = new ByteArrayOutputStream();
|
|
|
+ //doc是将要被转化的word文档
|
|
|
+ doc = new Document(fileInputStream);
|
|
|
+ //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
|
|
|
+ // 创建 Word 保存选项
|
|
|
+ DocSaveOptions options = new DocSaveOptions();
|
|
|
+ // 设置输出格式为 DOCX
|
|
|
+ options.setFormat(DocSaveOptions.DocFormat.DocX);
|
|
|
+ doc.save(byteOutputStream, options);
|
|
|
+ info.setSimpleName("docx");
|
|
|
+ info.setType("Microsoft Word Doc");
|
|
|
+ if(StringUtils.isNotBlank(fileName)) {
|
|
|
+ String name = fileName.replace(".docx","");
|
|
|
+ info.setName(name);
|
|
|
+ }
|
|
|
+ info.setFile(byteOutputStream.toByteArray());
|
|
|
+ attachment.save(info);
|
|
|
+ map.put("code","ok");
|
|
|
+ map.put("msg","ok");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error("附件类型转换失败:"+e.getMessage());
|
|
|
+ map.put("code","err");
|
|
|
+ map.put("msg",e.getMessage());
|
|
|
+ }finally {
|
|
|
+ if(doc != null)doc.close();
|
|
|
+ try {
|
|
|
+ if(fileInputStream != null) fileInputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if(byteOutputStream != null) byteOutputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- // 鍏抽棴娴�
|
|
|
- inputStream.close();
|
|
|
- outputStream.close();
|
|
|
-
|
|
|
- FileInputStream fileInputStream = new FileInputStream(attachFile);
|
|
|
- // 鍒涘缓杈撳嚭娴�
|
|
|
- ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
|
|
- //doc鏄�皢瑕佽�杞�寲鐨剋ord鏂囨。
|
|
|
- Document doc = new Document(fileInputStream);
|
|
|
- //鍏ㄩ潰鏀�寔DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 鐩镐簰杞�崲
|
|
|
- doc.save(byteOutputStream, SaveFormat.DocX);
|
|
|
- // 灏� ByteArrayOutputStream 杞�崲涓� InputStream
|
|
|
- InputStream input = new ByteArrayInputStream(byteOutputStream.toByteArray());
|
|
|
-
|
|
|
- try (BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream())) {
|
|
|
-// //閫氱煡娴忚�鍣ㄤ互闄勪欢褰㈠紡涓嬭浇
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
- //鏂囦欢杈撳嚭鏍煎紡
|
|
|
- response.setContentType("multipart/form-data;charset=utf-8");
|
|
|
- byte[] car = new byte[1024];
|
|
|
- int len;
|
|
|
- while ((len = input.read(car)) != -1) {
|
|
|
- out.write(car, 0, len);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.getMessage();
|
|
|
- }
|
|
|
- doc.close();
|
|
|
- fileInputStream.close();
|
|
|
- byteOutputStream.close();
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- logger.error("闄勪欢绫诲瀷杞�崲澶辫触锛�"+e.getMessage());
|
|
|
}
|
|
|
- if(attachFile != null) {
|
|
|
- attachFile.deleteOnExit();
|
|
|
- attachFile.delete();
|
|
|
- }
|
|
|
+ JSONUtils.SUCCESS(map);
|
|
|
}
|
|
|
+
|
|
|
+// public void convertPdfToWordAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
|
|
|
+// logger.error("进入附件转换方法");
|
|
|
+// String downloadUrl = request.getParameter("downloadUrl");
|
|
|
+// downloadUrl = downloadUrl.replace(" ", "+");
|
|
|
+// String fileName = request.getParameter("fileName");
|
|
|
+// logger.error("附件下载路径:"+downloadUrl);
|
|
|
+// logger.error("附件名称:"+fileName);
|
|
|
+// File attachFile = null;
|
|
|
+// try {
|
|
|
+// // 创建 URL 对象
|
|
|
+// URL url = new URL(downloadUrl);
|
|
|
+// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+// // 设置超时时间为100秒
|
|
|
+// conn.setConnectTimeout(100 * 1000);
|
|
|
+// // 得到输入流
|
|
|
+// InputStream inputStream = conn.getInputStream();
|
|
|
+// // 创建文件输出流
|
|
|
+// attachFile = File.createTempFile("temp", ".pdf");
|
|
|
+// // 将输入流中的数据写入临时文件
|
|
|
+// FileOutputStream outputStream = new FileOutputStream(attachFile);
|
|
|
+// // 读取输入流中的数据并写入输出流
|
|
|
+// byte[] buffer = new byte[4096];
|
|
|
+// int bytesRead;
|
|
|
+// while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+// outputStream.write(buffer, 0, bytesRead);
|
|
|
+// }
|
|
|
+// // 关闭流
|
|
|
+// inputStream.close();
|
|
|
+// outputStream.close();
|
|
|
+//
|
|
|
+// FileInputStream fileInputStream = new FileInputStream(attachFile);
|
|
|
+// // 创建输出流
|
|
|
+// ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
|
|
+// //doc是将要被转化的word文档
|
|
|
+//
|
|
|
+// Document doc = new Document(fileInputStream);
|
|
|
+// //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
|
|
|
+// doc.save(byteOutputStream, SaveFormat.DocX);
|
|
|
+// // 将 ByteArrayOutputStream 转换为 InputStream
|
|
|
+// InputStream input = new ByteArrayInputStream(byteOutputStream.toByteArray());
|
|
|
+//
|
|
|
+// try (BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream())) {
|
|
|
+//// //通知浏览器以附件形式下载
|
|
|
+// response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+// //文件输出格式
|
|
|
+// response.setContentType("multipart/form-data;charset=utf-8");
|
|
|
+// byte[] car = new byte[1024];
|
|
|
+// int len;
|
|
|
+// while ((len = input.read(car)) != -1) {
|
|
|
+// out.write(car, 0, len);
|
|
|
+// }
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.getMessage();
|
|
|
+// }
|
|
|
+// doc.close();
|
|
|
+// fileInputStream.close();
|
|
|
+// byteOutputStream.close();
|
|
|
+//
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// logger.error("附件类型转换失败:"+e.getMessage());
|
|
|
+// }
|
|
|
+// if(attachFile != null) {
|
|
|
+// attachFile.deleteOnExit();
|
|
|
+// attachFile.delete();
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
/*public void convertPdfToWordAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
|
|
|
- logger.error("杩涘叆闄勪欢杞�崲鏂规硶");
|
|
|
+ logger.error("进入附件转换方法");
|
|
|
String downloadUrl = request.getParameter("downloadUrl");
|
|
|
downloadUrl = downloadUrl.replace(" ", "+");
|
|
|
String fileName = request.getParameter("fileName");
|
|
|
- logger.error("闄勪欢涓嬭浇璺�緞锛�"+downloadUrl);
|
|
|
- logger.error("闄勪欢鍚嶇О锛�"+fileName);
|
|
|
+ logger.error("附件下载路径:"+downloadUrl);
|
|
|
+ logger.error("附件名称:"+fileName);
|
|
|
try {
|
|
|
URL url = new URL(downloadUrl);
|
|
|
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
|
|
|
int responseCode = httpConn.getResponseCode();
|
|
|
- // 妫€鏌ユ槸鍚︽垚鍔熻繛鎺ュ埌鏂囦欢
|
|
|
+ // 检查是否成功连接到文件
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
InputStream inputStream = url.openStream();
|
|
|
|
|
|
File tempFile = File.createTempFile("temp", ".pdf");
|
|
|
- // 灏嗚緭鍏ユ祦涓�殑鏁版嵁鍐欏叆涓存椂鏂囦欢
|
|
|
+ // 将输入流中的数据写入临时文件
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
|
|
|
byte[] buffer = new byte[1024];
|
|
|
int bytesRead;
|
|
|
@@ -125,23 +182,23 @@ public class HRcertificationbillListHandlerEx extends HRcertificationbillListHan
|
|
|
InputStream fileInputStream = new FileInputStream(tempFile.getPath());
|
|
|
tempFile.deleteOnExit();
|
|
|
tempFile.delete();
|
|
|
- // 鍒涘缓杈撳嚭娴�
|
|
|
+ // 创建输出流
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
- //doc鏄�皢瑕佽�杞�寲鐨剋ord鏂囨。
|
|
|
+ //doc是将要被转化的word文档
|
|
|
Document doc = new Document(fileInputStream);
|
|
|
- //鍏ㄩ潰鏀�寔DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 鐩镐簰杞�崲
|
|
|
+ //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
|
|
|
DocSaveOptions saveOptions = new DocSaveOptions();
|
|
|
doc.save(outputStream, saveOptions);
|
|
|
- // 灏� ByteArrayOutputStream 杞�崲涓� InputStream
|
|
|
+ // 将 ByteArrayOutputStream 转换为 InputStream
|
|
|
InputStream input = new ByteArrayInputStream(outputStream.toByteArray());
|
|
|
|
|
|
doc.close();
|
|
|
inputStream.close();
|
|
|
|
|
|
try (BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream())) {
|
|
|
-// //閫氱煡娴忚�鍣ㄤ互闄勪欢褰㈠紡涓嬭浇
|
|
|
+// //通知浏览器以附件形式下载
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
- //鏂囦欢杈撳嚭鏍煎紡
|
|
|
+ //文件输出格式
|
|
|
response.setContentType("multipart/form-data;charset=utf-8");
|
|
|
byte[] car = new byte[1024];
|
|
|
int len;
|
|
|
@@ -154,12 +211,12 @@ public class HRcertificationbillListHandlerEx extends HRcertificationbillListHan
|
|
|
input.close();
|
|
|
outputStream.close();
|
|
|
}else {
|
|
|
- logger.error("闄勪欢璺�緞杞�崲寮傚父");
|
|
|
+ logger.error("附件路径转换异常");
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- logger.error("闄勪欢绫诲瀷杞�崲澶辫触锛�"+e.getMessage());
|
|
|
+ logger.error("附件类型转换失败:"+e.getMessage());
|
|
|
}
|
|
|
|
|
|
}*/
|