OA附件读取定时任务优化
This commit is contained in:
		
							parent
							
								
									8eb663faf9
								
							
						
					
					
						commit
						149967bdfe
					
				| 
						 | 
				
			
			@ -79,8 +79,8 @@ public class DobeContractAttachmentTask extends AbstractTask implements Plugin {
 | 
			
		|||
            try {
 | 
			
		||||
                // 发送get请求oa附件接口并获取响应
 | 
			
		||||
                resultjson = HttpClientUtils.get(requrl, customerHeader,null);
 | 
			
		||||
                resultjsonObject = JSONObject.parseObject(resultjson);
 | 
			
		||||
                logger.info(contractno+" OA附件接口返回结果\n{}", resultjson);
 | 
			
		||||
                resultjsonObject = JSONObject.parseObject(resultjson);
 | 
			
		||||
                // 检查JSON对象是否为空
 | 
			
		||||
                if (resultjsonObject != null && !resultjsonObject.isEmpty()) {
 | 
			
		||||
                    if(resultjsonObject.getBoolean("success")){
 | 
			
		||||
| 
						 | 
				
			
			@ -93,7 +93,7 @@ public class DobeContractAttachmentTask extends AbstractTask implements Plugin {
 | 
			
		|||
                }
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                //记录OA接口异常信息
 | 
			
		||||
                logger.info(String.format(contractno+" 调用OA附件接口异常\n{}", e.getMessage()));
 | 
			
		||||
                logger.info(contractno+" 调用OA附件接口异常\n{}"+e.getMessage());
 | 
			
		||||
                DobeDWUtils.saveLog(contractno,"致远附件",requrl,e.getMessage(), false,"调用OA附件接口");
 | 
			
		||||
                throw new RuntimeException(e);
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -103,14 +103,15 @@ public class DobeContractAttachmentTask extends AbstractTask implements Plugin {
 | 
			
		|||
            if(!DobeDWUtils.isEmpty(nasPath)){
 | 
			
		||||
                //处理表单附件目录
 | 
			
		||||
                nasPath = nasPath.substring(2).replace("\\","/");
 | 
			
		||||
//                resultjsonObject.getJSONArray("");
 | 
			
		||||
                try {
 | 
			
		||||
                    //nasPath+fj_oaid;
 | 
			
		||||
                    logger.info(contractno+" OA附件接口返回nasPath\n{}", nasPath+"/"+fj_oaid);
 | 
			
		||||
                    boolean upresult = AttachmentFileUtil.saveDirFile2Attchment(appId,entityName,contractid,nasPath+"/"+fj_oaid,attachKey);
 | 
			
		||||
                    boolean upresult = AttachmentFileUtil.saveDirFile2Attchment(appId,entityName,contractid,nasPath+"/"+fj_oaid,attachKey,null);
 | 
			
		||||
                    //处理合同正文附件目录
 | 
			
		||||
                    if(!DobeDWUtils.isEmpty(fj_zwid)){
 | 
			
		||||
                        //nasPath+fj_zwid;
 | 
			
		||||
                        AttachmentFileUtil.saveDirFile2Attchment(appId,entityName,contractid,nasPath+"/"+fj_zwid,zwattKey);
 | 
			
		||||
//                        AttachmentFileUtil.saveDirFile2Attchment(appId,entityName,contractid,nasPath+"/"+fj_zwid,zwattKey);
 | 
			
		||||
                    }
 | 
			
		||||
                    //上传成功后更新合同未上传附件的标记为false
 | 
			
		||||
                    if(upresult){
 | 
			
		||||
| 
						 | 
				
			
			@ -120,11 +121,11 @@ public class DobeContractAttachmentTask extends AbstractTask implements Plugin {
 | 
			
		|||
                    }
 | 
			
		||||
                } catch (Exception e) {
 | 
			
		||||
                    //记录附件上传异常信息
 | 
			
		||||
                    logger.info(String.format(contractno+" 从NAS上传文件至星瀚异常\n{}", e.getMessage()));
 | 
			
		||||
                    logger.info(contractno+" 从NAS上传文件至星瀚异常"+e.getMessage());
 | 
			
		||||
                    DobeDWUtils.saveLog(contractno,"NAS附件",nasPath,e.getMessage(),false,"NAS上传文件至星瀚");
 | 
			
		||||
                }
 | 
			
		||||
            }else{
 | 
			
		||||
                logger.info(contractno+" OA附件接口返回路径为空,fj_oaid\n{}", fj_oaid);
 | 
			
		||||
                logger.info(contractno+" OA附件接口返回路径为空,fj_oaid"+ fj_oaid);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,8 @@ import java.nio.file.Files;
 | 
			
		|||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import com.alibaba.fastjson.JSONArray;
 | 
			
		||||
import com.alibaba.fastjson.JSONObject;
 | 
			
		||||
import kd.bos.cache.CacheFactory;
 | 
			
		||||
import kd.bos.cache.TempFileCache;
 | 
			
		||||
import kd.bos.context.RequestContext;
 | 
			
		||||
| 
						 | 
				
			
			@ -111,6 +113,59 @@ public class AttachmentFileUtil {
 | 
			
		|||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**将文件服务器上某个目录下的所有文件上传至星瀚对应单据附件上
 | 
			
		||||
     * @param appId 目标单据所属应用标识
 | 
			
		||||
     * @param entity 目标单据实体标识
 | 
			
		||||
     * @param pk 目标数据id
 | 
			
		||||
     * @param attachKey 附件面板标识
 | 
			
		||||
     * @param dirpath 文件目录路径
 | 
			
		||||
     * @param filenames 文件名称集合
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    public static boolean saveDirFile2Attchment(String appId, String entity, Object pk, String dirpath, String attachKey, JSONArray filenames) throws IOException {
 | 
			
		||||
        logger.info("传入的文件夹路径"+dirpath);
 | 
			
		||||
        filenames = new JSONArray();
 | 
			
		||||
        JSONObject f1 = new JSONObject();
 | 
			
		||||
        f1.put("filename","1.docx");
 | 
			
		||||
        filenames.add(f1);
 | 
			
		||||
        JSONObject f2 = new JSONObject();
 | 
			
		||||
        f2.put("filename","2.docx");
 | 
			
		||||
        filenames.add(f2);
 | 
			
		||||
        logger.info("传入的文件名称集合"+filenames);
 | 
			
		||||
        List<Map<String, Object>> attachments = new ArrayList<>(filenames.size());
 | 
			
		||||
        Map<String, Object> attachItem;
 | 
			
		||||
        String fileName;
 | 
			
		||||
        JSONObject destFile;
 | 
			
		||||
        String tempUrl;//星瀚临时文件url
 | 
			
		||||
        String actUrl;//星瀚正式文件url
 | 
			
		||||
        //获取临时文件缓存
 | 
			
		||||
        TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
 | 
			
		||||
        FileService service = FileServiceFactory.getAttachmentFileService();
 | 
			
		||||
        //遍历目标目录,得到所有文件
 | 
			
		||||
        for (int i = 0; i < filenames.size(); i++) {
 | 
			
		||||
            destFile = filenames.getJSONObject(i);
 | 
			
		||||
            fileName = destFile.getString("filename");//TODO 文件名称可能涉及字符集转义,需要注意
 | 
			
		||||
            attachItem = new HashMap<>();
 | 
			
		||||
            //文件名称
 | 
			
		||||
            attachItem.put("name", fileName);
 | 
			
		||||
            //文件大小
 | 
			
		||||
            attachItem.put("size", 1);
 | 
			
		||||
            //将文件流存入临时文件缓存(拷贝完成)(最后一个参数为缓存有效期,600秒)
 | 
			
		||||
            tempUrl = cache.saveAsUrl(fileName, service.getInputStream(dirpath+"/"+fileName), 900);
 | 
			
		||||
            //修改时间
 | 
			
		||||
            attachItem.put("lastModified",System.currentTimeMillis());
 | 
			
		||||
            //将文件缓存中的附件文件上传到正式文件服务器
 | 
			
		||||
            actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity,pk,fileName);
 | 
			
		||||
 | 
			
		||||
            //将新文件的物理路径存入map
 | 
			
		||||
            attachItem.put("url", actUrl);
 | 
			
		||||
            attachments.add(attachItem);
 | 
			
		||||
        }
 | 
			
		||||
        //维护单据和附件的关系(非文件上传)
 | 
			
		||||
        AttachmentServiceHelper.upload(entity, pk, attachKey, attachments);
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**将文件服务器上某个目录下的所有文件上传至星瀚对应单据附件上
 | 
			
		||||
     * @param appId 目标单据所属应用标识
 | 
			
		||||
     * @param entity 目标单据实体标识
 | 
			
		||||
| 
						 | 
				
			
			@ -188,30 +243,39 @@ public class AttachmentFileUtil {
 | 
			
		|||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    public static boolean realtionDirFile2Attchment(String appId,String entity,Object pk,String dirpath,String attachKey) {
 | 
			
		||||
        File[] files = new File(dirpath).listFiles();
 | 
			
		||||
        if(files.length == 0){
 | 
			
		||||
            //目录下没有文件,不需要处理
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
//        File[] files = new File(dirpath).listFiles();
 | 
			
		||||
//        if(files.length == 0){
 | 
			
		||||
//            //目录下没有文件,不需要处理
 | 
			
		||||
//            return false;
 | 
			
		||||
//        }
 | 
			
		||||
        List<Map<String, Object>> attachments = new ArrayList<>();
 | 
			
		||||
        Map<String, Object> attachItem;
 | 
			
		||||
        String fileName;
 | 
			
		||||
        File destFile;
 | 
			
		||||
        String fileName = "1.docx";
 | 
			
		||||
//        File destFile;
 | 
			
		||||
        //遍历目标目录,得到所有文件
 | 
			
		||||
        for (int i = 0; i < files.length; i++) {
 | 
			
		||||
            destFile = files[i];
 | 
			
		||||
            fileName = destFile.getName();
 | 
			
		||||
//        for (int i = 0; i < files.length; i++) {
 | 
			
		||||
//            destFile = files[i];
 | 
			
		||||
//            fileName = destFile.getName();
 | 
			
		||||
            attachItem = new HashMap<>();
 | 
			
		||||
            //文件名称
 | 
			
		||||
            attachItem.put("name", fileName);
 | 
			
		||||
            //文件大小
 | 
			
		||||
            attachItem.put("size", destFile.length());
 | 
			
		||||
            attachItem.put("size", 1);
 | 
			
		||||
            //修改时间
 | 
			
		||||
            attachItem.put("lastModified",System.currentTimeMillis());
 | 
			
		||||
        TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
 | 
			
		||||
        FileService service = FileServiceFactory.getAttachmentFileService();
 | 
			
		||||
//        service.download("/oa/uat/ht-202502001-nj/232906839800890085/1.docx");
 | 
			
		||||
        String tempUrl = cache.saveAsUrl(fileName, service.getInputStream("/oa/uat/ht-202502001-nj/232906839800890085/1.docx"), 900);
 | 
			
		||||
        String actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity,pk,fileName);
 | 
			
		||||
            //将文件的物理路径存入map
 | 
			
		||||
            attachItem.put("url", destFile.getPath());
 | 
			
		||||
            attachItem.put("url", actUrl);
 | 
			
		||||
            attachments.add(attachItem);
 | 
			
		||||
        }
 | 
			
		||||
//        }
 | 
			
		||||
 | 
			
		||||
//        long filesize = service.getFileSize("/oa/uat/ht-202502001-nj/232906839800890085/1.docx");
 | 
			
		||||
//        service.download();
 | 
			
		||||
//        logger.info("文件大小"+filesize);
 | 
			
		||||
        //维护单据和附件的关系(非文件上传)
 | 
			
		||||
        AttachmentServiceHelper.upload(entity, pk, attachKey, attachments);
 | 
			
		||||
        return true;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue