业务需要将ftp文件压缩加密并且转存,先读取ftp源文件再写入临时目录,然后在临时文件中加密压缩,再通过ftp上传,清除临时目录中的文件
java
public String getPdfByFtpUrl(RecTypeDictStorageEntity storageEntity,ViewArchiveDocumentsEntity viewArchiveDocuments) {
        ByteArrayOutputStream fileData=new ByteArrayOutputStream();
        FtpUtil ftpUtil = new FtpUtil();
        String ftpConfig = "ftp://USER:PWD@IP:PORT";
        ftpConfig = ftpConfig.replace("IP", storageEntity.getFtpIp())
                .replace("USER", storageEntity.getFtpUser())
                .replace("PWD", storageEntity.getFtpPwd())
                .replace("PORT", storageEntity.getFtpPort());
        ftpUtil.getConn(ftpConfig);
        String remoteFile = viewArchiveDocuments.getDocfile().substring(0, viewArchiveDocuments.getDocfile().lastIndexOf("/") + 1);
        String fileName = viewArchiveDocuments.getDocfile().substring(viewArchiveDocuments.getDocfile().lastIndexOf("/") + 1);
        try {
            boolean result = ftpUtil.fileExist(remoteFile, fileName);
            if (result) {
                fileData = ftpUtil.fileGet(remoteFile, fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ftpUtil.ftpDisconnect();
        }
        if (fileData!=null) {
            try {
                byte[] fileArray=fileData.toByteArray();
                ByteArrayInputStream inputStream = new ByteArrayInputStream(fileArray);
                return Base64.getEncoder().encodeToString(IOUtils.toByteArray(inputStream));
            } catch (Exception ex) {
                logger.info(ex.getMessage());
                return "";
            }
        }
        logger.info("找不到文件,获取不了文件信息");
        return "";
    }
java
 String path= null;
        try {
            path = AutoCollectionJob.class.getResource("/pdf/").toURI().getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        System.out.println("======================="+path+"路径");
        Map<String,String> map=new HashMap<>();
        String fileName = MessageFormat.format("{0}_{1}_{2}.pdf",viewArchiveDocuments.getEpisodeid(),viewArchiveDocuments.getDocdesc(),DateUtil.format(new Date(),"yyyy_MM_dd_HH_mm_ss"));
        String filePath=path+"\\"+fileName;
        map.put("fileName",fileName);
        map.put("path",path);
        map.put("filePath",filePath);
        FileUtils.base64ToFile(filePath,base64);
        return map;
将base64转存在target-classes目录的pdf文件夹中,因为打包会把resources目录的文件打包到这边
java public String zipPDF(Map<String,String> map){
        String pdfFile=map.get("filePath");
        String pdfName=map.get("fileName");
        String path= null;
        try {
            path = AutoCollectionJob.class.getResource("/zip/").toURI().getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        System.out.println(path);
        String zipPath=path+pdfName.split("pdf")[0]+"zip";
        System.out.println(zipPath);
        ZipUtil.encrypt_zip(pdfFile,zipPath,"mars");
        return zipPath;
    }
    public String loadZip(String zipName){
        String base64=Base64Util.convertFileToBase64(zipName);
        return base64;
    }
加密工具类中的方法
java
    public static void encrypt_zip(String src_file, String dst_file, String encode) {
        File file = new File(src_file);
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);//压缩方式
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//加密方式
        parameters.setPassword(encode.toCharArray());//设置密码
        try {
            ZipFile zipFile = new ZipFile(dst_file);
            zipFile.setFileNameCharset("gbk");
            zipFile.addFile(file, parameters);
        } catch (net.lingala.zip4j.exception.ZipException e) {
            e.printStackTrace();
        }
    }
读取pdf文件,加密到zip目录
java
public UpLoadResponse upload(String base64Str, RecTypeDictStorageEntity recTypeDictStorage, ViewArchiveDocumentsEntity viewArchiveDocuments){
        UpLoadResponse upLoadResponse=new UpLoadResponse();
        //base转输入流
        ByteArrayInputStream stream = null;
        try {
            byte[] bytes = Base64.getMimeDecoder().decode(base64Str);
            stream = new ByteArrayInputStream(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        InputStream inputStream=stream;
        FtpUtil ftpUtil = new FtpUtil();
        String ftpConfig = "ftp://USER:PWD@IP:PORT";
        ftpConfig = ftpConfig.replace("IP", recTypeDictStorage.getFtpIp())
                .replace("USER", recTypeDictStorage.getFtpUser())
                .replace("PWD", recTypeDictStorage.getFtpPwd())
                .replace("PORT", recTypeDictStorage.getFtpPort());
        try {
            logger.info("上传首页 ftp配置:" + ftpConfig);
            ftpUtil.getConn(ftpConfig);
            String remoteFile = ftpUtil.getMedDocPath(viewArchiveDocuments.getPatientid(), viewArchiveDocuments.getEpisodeid());
            String fileName = MessageFormat.format("{0}_{1}_{2}.as",viewArchiveDocuments.getEpisodeid(),viewArchiveDocuments.getDocdesc(),DateUtil.format(new Date(),"yyyy_MM_dd_HH_mm_ss"));
            boolean result = ftpUtil.fileUpload(remoteFile,fileName, inputStream);
            upLoadResponse.setResult(result);
            upLoadResponse.setFileName(fileName);
            upLoadResponse.setRemoteFile(remoteFile);
            return upLoadResponse;
        } catch (Exception e) {
            logger.info(e.getMessage());
        } finally {
            ftpUtil.ftpDisconnect();
        }
        upLoadResponse.setResult(false);
        return upLoadResponse;
    }
上传ftp的时候设置私有的加密格式
java
public boolean deleteZipAndPdf() throws IOException {
        String zipPath= null;
        String pdfPath= null;
        try {
            zipPath = AutoCollectionJob.class.getResource("/zip/").toURI().getPath();
            pdfPath = AutoCollectionJob.class.getResource("/pdf/").toURI().getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        if (StrUtil.isBlank(zipPath)||StrUtil.isBlank(pdfPath)){
            return false;
        }
        File dirZip = new File(zipPath);
        File dirPdf = new File(pdfPath);
        if (dirZip.isDirectory()) {
            // 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错
            org.apache.commons.io.FileUtils.cleanDirectory(dirZip);
        }
        if (dirPdf.isDirectory()){
            org.apache.commons.io.FileUtils.cleanDirectory(dirPdf);
        }
        return true;
    }
删除zip和pdf文件夹下的文件,文件夹保留
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!