dobe_comic8/main/java/shkd/utils/AttachmentFileUtil.java

340 lines
15 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package shkd.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
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;
import kd.bos.entity.MainEntityType;
import kd.bos.fileservice.FileItem;
import kd.bos.fileservice.FileService;
import kd.bos.fileservice.FileServiceFactory;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.MetadataServiceHelper;
import kd.bos.util.FileNameUtils;
public class AttachmentFileUtil {
private static final Log logger = LogFactory.getLog(AttachmentFileUtil.class);
/**将第三方提供的某个在线文件上传至星瀚对应单据附件上
* @param entity 目标单据实体标识
* @param pk 目标数据id
* @param fileUrl 待存入文件的url
* @param fileName 待存入文件名称
* @param fileSize 待存入文件大小
* @param attachKey 附件面板标识
* @throws IOException
*/
public static boolean saveUrlFile2Attchment(String entity,Object pk,String fileUrl,String fileName,String fileSize,String attachKey){
List<Map<String, Object>> attachments = new ArrayList<>();
Map<String, Object> attachItem = new HashMap<>();
//文件名称
attachItem.put("name", fileName);
//将url文件转换成输入流
InputStream inputStream = null;
HttpURLConnection conn = null;
String tempUrl = null;
try {
URL url = new URL(fileUrl);
conn = (HttpURLConnection)url.openConnection();
//设置超时间为30秒
conn.setConnectTimeout(30000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
inputStream = conn.getInputStream();
//文件大小
attachItem.put("size", fileSize);
//获取临时文件缓存
TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
//将文件流存入临时文件缓存拷贝完成最后一个参数为缓存有效期600秒
tempUrl = cache.saveAsFullUrl(fileName, inputStream, 600);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
//关闭流
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
//关闭连接
conn.disconnect();
}
//获取文件的缓存路径
// tempUrl = EncreptSessionUtils.encryptSession(tempUrl);
// //获取域名前缀
// String address = RequestContext.get().getClientFullContextPath();
// if (!address.endsWith("/")) {
// address = address + "/";
// }
// //拼接url路径
// String tempUrl3 = address + tempUrl;
//修改时间
attachItem.put("lastModified",System.currentTimeMillis());
//获取appId
MainEntityType dataEntityType = MetadataServiceHelper.getDataEntityType(entity);
String appId = dataEntityType.getAppId();
//将文件缓存中的附件文件上传到正式文件服务器
String actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity, pk, (String)attachItem.get("name"));
TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
InputStream in = cache.getInputStream(tempUrl);
FileService service = FileServiceFactory.getAttachmentFileService();
RequestContext requestContext = RequestContext.get();
String uuid = UUID.randomUUID().toString().replace("-", "");
// 生成文件路径-上传附件时远程服务器需要存储文件的位置
String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(),
requestContext.getAccountId(), uuid, fileName);
FileItem fileItem = new FileItem(fileName, pathParam, in);
// cache.remove(url);
// 上传附件到文件服务器
String downUrl =service.upload(fileItem);
//将新文件的物理路径存入map
attachItem.put("url", actUrl);
attachments.add(attachItem);
//维护单据和附件的关系(非文件上传)
AttachmentServiceHelper.upload(entity, pk, attachKey, attachments);
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);
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");//文件名称可能涉及字符集转义,需要注意
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 目标单据实体标识
* @param pk 目标数据id
* @param attachKey 附件面板标识
* @param dirpath 文件目录路径
* @throws IOException
*/
public static boolean saveDirFile2Attchment(String appId,String entity,Object pk,String dirpath,String attachKey) throws IOException {
File currentDir = new File(".");
logger.info("当前系统路径"+currentDir.getAbsolutePath());
logger.info("传入的路径"+dirpath);
File dirfile = new File(dirpath);
if(!dirfile.isDirectory()){
//传入的路径不是目录或者目录不存在
logger.info("不是目录或者目录不存在"+dirpath);
return false;
}
File[] files = dirfile.listFiles();
if(files.length == 0){
//目录下没有文件,不需要处理
logger.info("目录下没有文件,不需要处理"+dirpath);
return false;
}
List<Map<String, Object>> attachments = new ArrayList<>();
Map<String, Object> attachItem;
String fileName;
File destFile;
String tempUrl;//星瀚临时文件url
String actUrl;//星瀚正式文件url
//获取临时文件缓存
TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
//遍历目标目录,得到所有文件
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());
//将文件流存入临时文件缓存拷贝完成最后一个参数为缓存有效期600秒
tempUrl = cache.saveAsFullUrl(fileName, Files.readAllBytes(destFile.toPath()), 900);
//修改时间
attachItem.put("lastModified",System.currentTimeMillis());
//将文件缓存中的附件文件上传到正式文件服务器
actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity,pk,fileName);
//如下代码功能与saveTempToFileService实际功能重复先注释掉待调试确认
// FileService service = FileServiceFactory.getAttachmentFileService();
// RequestContext requestContext = RequestContext.get();
// String uuid = UUID.randomUUID().toString().replace("-", "");
// // 生成文件路径-上传附件时远程服务器需要存储文件的位置
// String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(),
// requestContext.getAccountId(), uuid, fileName);
// FileItem fileItem = new FileItem(fileName, pathParam, cache.getInputStream(tempUrl));
// // 上传附件到文件服务器
// service.upload(fileItem);
//将新文件的物理路径存入map
attachItem.put("url", actUrl);
attachments.add(attachItem);
}
//维护单据和附件的关系(非文件上传)
AttachmentServiceHelper.upload(entity, pk, attachKey, attachments);
return true;
}
/**将星瀚nfs上某个目录下的所有文件关联至星瀚对应单据附件上不需要文件上传
* @param appId 目标单据所属应用标识
* @param entity 目标单据实体标识
* @param pk 目标数据id
* @param attachKey 附件面板标识
* @param dirpath 文件目录路径
* @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;
// }
List<Map<String, Object>> attachments = new ArrayList<>();
Map<String, Object> attachItem;
String fileName = "1.docx";
// File destFile;
//遍历目标目录,得到所有文件
// for (int i = 0; i < files.length; i++) {
// destFile = files[i];
// fileName = destFile.getName();
attachItem = new HashMap<>();
//文件名称
attachItem.put("name", fileName);
//文件大小
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", 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;
}
/**将第三方提供的某个在线文件上传至星瀚对应单据附件上
* @param entity 目标单据实体标识
* @param pk 目标数据id
* @param fileUrl 待存入文件的url
* @param fileName 待存入文件名称
* @param fileSize 待存入文件大小
* @param attachKey 附件面板标识
* @throws IOException
*/
public static boolean saveUrlFile2Attch(String entity,Object pk,String fileUrl,String fileName,String fileSize,String attachKey) throws IOException{
List<Map<String, Object>> attachments = new ArrayList<>();
Map<String, Object> attachItem = new HashMap<>();
//文件名称
attachItem.put("name", fileName);
//将url文件转换成输入流
InputStream inputStream = null;
HttpURLConnection conn = null;
String tempUrl = null;
try {
URL url = new URL(fileUrl);
conn = (HttpURLConnection)url.openConnection();
//设置超时间为30秒
conn.setConnectTimeout(30000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
inputStream = conn.getInputStream();
//文件大小
attachItem.put("size", fileSize);
//获取临时文件缓存
TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
//将文件流存入临时文件缓存拷贝完成最后一个参数为缓存有效期600秒
tempUrl = cache.saveAsFullUrl(fileName, inputStream, 600);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
//关闭流
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
//关闭连接
conn.disconnect();
}
//修改时间
attachItem.put("lastModified",System.currentTimeMillis());
//获取appId
MainEntityType dataEntityType = MetadataServiceHelper.getDataEntityType(entity);
String appId = dataEntityType.getAppId();
//将文件缓存中的附件文件上传到正式文件服务器
String actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity,pk,fileName);
//将新文件的物理路径存入map
attachItem.put("url", actUrl);
attachments.add(attachItem);
//维护单据和附件的关系(非文件上传)
AttachmentServiceHelper.upload(entity, pk, attachKey, attachments);
return true;
}
}