167 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			Java
		
	
	
	
			
		
		
	
	
			167 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			Java
		
	
	
	
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 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.servicehelper.AttachmentServiceHelper;
 | 
						||
import kd.bos.servicehelper.MetadataServiceHelper;
 | 
						||
import kd.bos.util.FileNameUtils;
 | 
						||
 | 
						||
public class AttachmentFileUtil {
 | 
						||
 | 
						||
    /**将第三方提供的某个在线文件上传至星瀚对应单据附件上
 | 
						||
     * @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 文件目录路径
 | 
						||
     * @throws IOException
 | 
						||
     */
 | 
						||
    public static boolean saveDirFile2Attchment(String appId,String entity,Object pk,String dirpath,String attachKey) throws IOException {
 | 
						||
        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 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()), 600);
 | 
						||
            //修改时间
 | 
						||
            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;
 | 
						||
    }
 | 
						||
}
 |