|
@@ -1,10 +1,12 @@
|
|
|
package nc.impl.th;
|
|
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.DataOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
@@ -13,10 +15,25 @@ import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
-
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
|
|
+import nc.bs.dao.BaseDAO;
|
|
|
+import nc.bs.dao.DAOException;
|
|
|
+import nc.bs.framework.common.InvocationInfoProxy;
|
|
|
+import nc.bs.framework.common.NCLocator;
|
|
|
+import nc.bs.framework.common.RuntimeEnv;
|
|
|
+import nc.itf.uap.IUAPQueryBS;
|
|
|
+import nc.jdbc.framework.processor.ArrayListProcessor;
|
|
|
+import nc.jdbc.framework.processor.BeanListProcessor;
|
|
|
+import nc.log.NcLog;
|
|
|
+import nc.ui.pub.beans.MessageDialog;
|
|
|
+import nc.vo.bd.defdoc.DefdocVO;
|
|
|
+import nc.vo.pub.BusinessException;
|
|
|
import nc.bs.framework.common.NCLocator;
|
|
|
+import nc.bs.pub.filesystem.IFileSystemService;
|
|
|
import nc.itf.uap.IVOPersistence;
|
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
@@ -28,11 +45,12 @@ import org.apache.commons.httpclient.methods.StringRequestEntity;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-
|
|
|
+import nc.itf.bd.defdoc.IDefdocService;
|
|
|
import nc.itf.th.IThOcrService;
|
|
|
import nc.pub.toolkits.Toolkits;
|
|
|
import nc.th.redistoken.RedisTokenManager;
|
|
|
import nc.vo.pub.BusinessException;
|
|
|
+import nc.vo.pub.filesystem.NCFileNode;
|
|
|
import nc.vo.th.ocr.OCRInvoiceVO;
|
|
|
import net.sf.json.JSONString;
|
|
|
|
|
@@ -54,7 +72,7 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
* 获取token
|
|
|
*/
|
|
|
public String getToken(String appId,Long cid,String secret,String userid) throws HttpException, IOException, BusinessException {
|
|
|
-
|
|
|
+ NcLog.info("=======获取token开始=======");
|
|
|
|
|
|
String userToken = RedisTokenManager.getToken(userid);
|
|
|
|
|
@@ -64,8 +82,8 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
|
|
|
RedisTokenManager.setTokenTTL(userid,36000, userToken);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ NcLog.info("=======token======="+userToken);
|
|
|
+ NcLog.info("=======获取token结束=======");
|
|
|
return userToken;
|
|
|
}
|
|
|
|
|
@@ -75,7 +93,10 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
* 上传发票
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public String uploadInvoice(String url,String token,File file) throws Exception {
|
|
|
+ public String uploadInvoice(String url,String token,String vdef17,String invoicename) throws Exception {
|
|
|
+ NcLog.info("=======上传发票开始=======");
|
|
|
+ File file=new File(RuntimeEnv.getInstance().getNCHome()
|
|
|
+ +File.separator +"uploadfile" + File.separator+ vdef17+File.separator+invoicename);
|
|
|
String resmsg=httpFilePost(url,token,file);
|
|
|
String id="";
|
|
|
if(null!=resmsg&&resmsg.indexOf("uploading")!=-1){
|
|
@@ -101,6 +122,7 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
@Override
|
|
|
public String ocrIdentification(String pathUrl, String token,String data) {
|
|
|
|
|
|
+ NcLog.info("=======发票OCR识别开始=======");
|
|
|
JSONObject json = JSONObject.parseObject(data);
|
|
|
JSONObject jsonobject = new JSONObject();
|
|
|
jsonobject.put("image_index", json.getJSONObject("data").getString("id"));
|
|
@@ -153,7 +175,7 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
String token=json.getString("data");
|
|
|
String status=json.getString("status");
|
|
|
|
|
|
-
|
|
|
+ NcLog.info("token:"+token);
|
|
|
|
|
|
if(null!=status){
|
|
|
throw new BusinessException(responseMsg);
|
|
@@ -189,7 +211,8 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
stringBuilder.append("Content-Type: " + HttpURLConnection.guessContentTypeFromName(file.getName()) + LINEND);
|
|
|
stringBuilder.append("Content-Transfer-Encoding: binary"+ LINEND);
|
|
|
stringBuilder.append(LINEND);
|
|
|
- outStream.write(stringBuilder.toString().getBytes());
|
|
|
+
|
|
|
+ outStream.writeUTF(stringBuilder.toString());
|
|
|
InputStream is = new FileInputStream(file);
|
|
|
byte[] buffer = new byte[1024];
|
|
|
int len = 0;
|
|
@@ -204,7 +227,8 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
fileInputStream.close();
|
|
|
String response = readStream(httpURLConnection.getInputStream());
|
|
|
outStream.close();
|
|
|
- httpURLConnection.disconnect();
|
|
|
+ httpURLConnection.disconnect();
|
|
|
+ NcLog.info("发票上传返回json:"+response);
|
|
|
return response;
|
|
|
|
|
|
}
|
|
@@ -314,7 +338,322 @@ public class ThOcrServiceImpl implements IThOcrService{
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ NcLog.info("发票OCR识别返回json:"+result);
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ * 发票保存到文件夹里
|
|
|
+ * vdef17 发票主键
|
|
|
+ * fileName 发票名
|
|
|
+ * file 选中的发票
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public String saveUploadFile(String vdef17,String fileName,byte[] bytes) throws Exception {
|
|
|
+
|
|
|
+ String filepath = RuntimeEnv.getInstance().getNCHome()
|
|
|
+ +File.separator +"uploadfile" + File.separator+ vdef17;
|
|
|
+ NcLog.info("文件保存的路径:"+filepath);
|
|
|
+ NcLog.info("原文件路径:"+file.getPath());
|
|
|
+ File folder = new File(filepath);
|
|
|
+ if(!folder.exists() && !folder.isDirectory()){
|
|
|
+ folder.mkdirs();
|
|
|
+ }
|
|
|
+ String newFileName = filepath + File.separator + fileName;
|
|
|
+ NcLog.info("文件保存新路径:"+newFileName);
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(newFileName);
|
|
|
+ NcLog.info("=====FileOutputStream=====");
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ NcLog.info("=====读取发票信息开始=====");
|
|
|
+ while ((len = fis.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ NcLog.info("=====读取发票信息结束=====");
|
|
|
+ outputStream.close();
|
|
|
+ fis.close();
|
|
|
+ NcLog.info("=====发票保存结束=====");*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ NcLog.info("=====发票保存开始=====");
|
|
|
+
|
|
|
+ String filepath = RuntimeEnv.getInstance().getNCHome()
|
|
|
+ +File.separator +"uploadfile" + File.separator+ vdef17;
|
|
|
+ String newFileName = filepath + File.separator + fileName;
|
|
|
+ NcLog.info("文件保存新路径:"+newFileName);
|
|
|
+
|
|
|
+ String savefilepath=SaveTempFile(filepath,newFileName,bytes);
|
|
|
+
|
|
|
+
|
|
|
+ return savefilepath;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 保存文件
|
|
|
+ *
|
|
|
+ * @param filepath 文件夹路径
|
|
|
+ * @param fileName 文件名路径
|
|
|
+ * @param bs 文件流
|
|
|
+ * @return 返回文件保存的路径
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public String SaveTempFile(String filepath,String fileName,byte[] bytes) throws Exception {
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ BufferedOutputStream bos=null;
|
|
|
+ try {
|
|
|
+ File folder = new File(filepath);
|
|
|
+ if(!folder.exists() && !folder.isDirectory()){
|
|
|
+ folder.mkdirs();
|
|
|
+ }
|
|
|
+ fos = new FileOutputStream(fileName);
|
|
|
+ bos = new BufferedOutputStream(fos);
|
|
|
+ bos.write(bytes);
|
|
|
+ } catch (FileNotFoundException e1) {
|
|
|
+
|
|
|
+ e1.printStackTrace();
|
|
|
+ NcLog.info("保存发票FileNotFoundException异常:"+e1.getMessage());
|
|
|
+ throw new Exception("保存发票FileNotFoundException异常:"+e1.getMessage());
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("保存发票IOException异常:"+e.getMessage());
|
|
|
+ throw new Exception("保存发票IOException异常:"+e.getMessage());
|
|
|
+ }finally {
|
|
|
+ if (bos != null) {
|
|
|
+ try {
|
|
|
+ bos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("关闭bos流异常:"+e.getMessage());
|
|
|
+ throw new Exception("关闭bos流异常:"+e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fos != null) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("关闭fos流异常:"+e.getMessage());
|
|
|
+ throw new Exception("关闭fos流异常:"+e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return filepath;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * NC文件上传
|
|
|
+ * @param parentPath 单据主键pk_jkbx
|
|
|
+ * @param newFile 文件路径
|
|
|
+ * @param filename 文件名
|
|
|
+ * @param user_code 创建人
|
|
|
+ * @return 0表示上传成功,-1表示失败
|
|
|
+ * @throws BusinessException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public int Upload(String parentPath, String filepath, String filename,String user_code) throws BusinessException, IOException {
|
|
|
+ NcLog.info("============防止重复上传相同名称的附件开始============");
|
|
|
+ QueryFile(parentPath, filename);
|
|
|
+ NcLog.info("============防止重复上传相同名称的附件结束============");
|
|
|
+ File file = new File(filepath);
|
|
|
+ NcLog.info("文件路径file:"+file.getPath());
|
|
|
+ Long fileLen = file.length();
|
|
|
+ String creator = user_code;
|
|
|
+ NCFileNode node = null;
|
|
|
+ InputStream fileinput = null;
|
|
|
+ try {
|
|
|
+ fileinput = new FileInputStream(file);
|
|
|
+ NcLog.info("FileInputStream:"+fileinput);
|
|
|
+ IFileSystemService service = (IFileSystemService) NCLocator
|
|
|
+ .getInstance().lookup(IFileSystemService.class);
|
|
|
+ NcLog.info("接口service开始:"+service);
|
|
|
+ node = service.createNewFileNodeWithStream(parentPath, filename,
|
|
|
+ creator, fileinput, fileLen);
|
|
|
+ NcLog.info("接口service结束:"+node);
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("FileNotFoundException保存失败:"+e.getMessage());
|
|
|
+ throw new FileNotFoundException();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("IOException保存失败:"+e.getMessage());
|
|
|
+ throw new IOException();
|
|
|
+ } catch (BusinessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("BusinessException保存失败:"+e.getMessage());
|
|
|
+ throw new BusinessException();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ fileinput.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (node == null) {
|
|
|
+ NcLog.info("-1表示失败:"+"【"+-1+"】");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ NcLog.info("0表示上传成功:"+"【"+0+"】");
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 防止重复上传相同名称的附件
|
|
|
+ *
|
|
|
+ * @param parentPath
|
|
|
+ * 单据主键
|
|
|
+ * @param filename
|
|
|
+ * 文件名
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public void QueryFile(String parentPath, String filename) throws BusinessException {
|
|
|
+ try {
|
|
|
+ String path = parentPath + "/" + filename;
|
|
|
+ IUAPQueryBS iuap= (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
|
|
|
+ String sql = "select filepath from sm_pub_filesystem where filepath = '"
|
|
|
+ + path + "'";
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Object[]> filelist = (List<Object[]>) iuap.executeQuery(sql,
|
|
|
+ new ArrayListProcessor());
|
|
|
+ if (filelist != null && filelist.size() > 0) {
|
|
|
+ IFileSystemService service = (IFileSystemService) NCLocator
|
|
|
+ .getInstance().lookup(IFileSystemService.class);
|
|
|
+ String[] arg = new String[] { path };
|
|
|
+ service.deleteNCFileNodes(arg);
|
|
|
+ }
|
|
|
+ } catch (DAOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("=====DAOException防止重复上传相同名称的附件====="+e.getMessage());
|
|
|
+ throw new DAOException();
|
|
|
+ } catch (BusinessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ NcLog.info("=====BusinessException防止重复上传相同名称的附件====="+e.getMessage());
|
|
|
+ throw new BusinessException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取home路径
|
|
|
+ */
|
|
|
+ public String getNCHomePath() {
|
|
|
+
|
|
|
+ return RuntimeEnv.getInstance().getNCHome();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 删除home里对应的发票
|
|
|
+ */
|
|
|
+ public Boolean deleteFile(String filepath) {
|
|
|
+ NcLog.info("=====删除home里对应的发票开始=====");
|
|
|
+
|
|
|
+ String deletefilepath = RuntimeEnv.getInstance().getNCHome()
|
|
|
+ +File.separator +"uploadfile" + File.separator+ filepath;
|
|
|
+
|
|
|
+ File file = new File(deletefilepath);
|
|
|
+
|
|
|
+ Boolean bo= file.delete();
|
|
|
+ NcLog.info("=====删除home里对应的发票结束=====");
|
|
|
+ return bo;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 由于删除太慢,改成自定义档案数据停用
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public DefdocVO[] enablestateDate(String filepath,String[] filname) throws BusinessException {
|
|
|
+ NcLog.info("=====改成自定义档案数据停用开始=====");
|
|
|
+ DefdocVO[] deletevos=QryDefdocvoInfo(filname[1]);
|
|
|
+ IDefdocService idefservice = NCLocator.getInstance().lookup(IDefdocService.class);
|
|
|
+ DefdocVO defvo =deletevos[0];
|
|
|
+ defvo.setAttributeValue("enablestate", 3);
|
|
|
+ DefdocVO[] defdocVOs= idefservice.updateDefdocs(InvocationInfoProxy.getInstance().getGroupId(),deletevos);
|
|
|
+ NcLog.info("=====改成自定义档案数据停用结束=====");
|
|
|
+ return defdocVOs;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private DefdocVO[] QryDefdocvoInfo(String name){
|
|
|
+ String sql = "select * from bd_defdoc where nvl(dr, 0) = 0 and pk_defdoclist="
|
|
|
+ + "(select pk_defdoclist from bd_defdoclist where code='OCRTHNC' and nvl(dr,0)=0) and memo='" + name + "' ";
|
|
|
+ ArrayList<Object> arrayvos=null;
|
|
|
+ try {
|
|
|
+ IUAPQueryBS iuap= (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
|
|
|
+ arrayvos=(ArrayList<Object>) iuap.executeQuery(sql, new BeanListProcessor(DefdocVO.class));
|
|
|
+ } catch (BusinessException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (arrayvos==null) {
|
|
|
+ return new DefdocVO[0];
|
|
|
+ }else {
|
|
|
+ return arrayvos.toArray(new DefdocVO[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取"+vdef17+"文件夹下所有发票名
|
|
|
+ */
|
|
|
+ public List<String> getInvoiceInfo(String vdef17) {
|
|
|
+ String filepath = RuntimeEnv.getInstance().getNCHome()
|
|
|
+ +File.separator +"uploadfile" + File.separator+ vdef17;
|
|
|
+ List<String> filewjlis=getFileNames(filepath);
|
|
|
+
|
|
|
+ return filewjlis;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 得到文件名称
|
|
|
+ *
|
|
|
+ * @param path 路径
|
|
|
+ * @return {@link List}<{@link String}>
|
|
|
+ */
|
|
|
+ private List<String> getFileNames(String path) {
|
|
|
+ File file = new File(path);
|
|
|
+ if (!file.exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<String> fileNames = new ArrayList<String>();
|
|
|
+ return getFileNames(file, fileNames);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 得到文件名称
|
|
|
+ *
|
|
|
+ * @param file 文件
|
|
|
+ * @param fileNames 文件名
|
|
|
+ * @return {@link List}<{@link String}>
|
|
|
+ */
|
|
|
+ private List<String> getFileNames(File file, List<String> fileNames) {
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File f : files) {
|
|
|
+ if (f.isDirectory()) {
|
|
|
+ getFileNames(f, fileNames);
|
|
|
+ } else {
|
|
|
+ fileNames.add(f.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fileNames;
|
|
|
+ }
|
|
|
|
|
|
}
|