上传附件,压缩并加密

    /**
     * Method onUpload$uploadAttach 上传附件
     * @param e
     * @param uploaded 
     * @param page 
     */
    public static void uploadAttach(UploadEvent e, Page page, Hlayout uploaded){
        //一个随机的字符串,临时的合同编号,上传附件的存储路径都会用到
        String temporary_key = System.currentTimeMillis() + "";
        String path = System.getProperty("purchase_attachment_path") + temporary_key + "/";
        File f0 = new File(path);
        if(!f0.exists()){
            f0.mkdirs();
        }
        File file = new File(path + e.getMedia().getName().trim().replaceAll(" ", "_"));
        if(e.getMedia().isBinary()){
            InputStream ins = e.getMedia().getStreamData();
            try {
                OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                while ((bytesRead = ins.read(buffer, 0, 1024)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.close();
                ins.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }else{
            try {
                file.createNewFile();
                Files.copy(file, e.getMedia().getReaderData(), "GBK");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        
        String filename = RandomUtil.randomString(16);
        
        Pack p = new Pack();
        String pswd = p.pack(file.getParent() + "/" + filename, file.getAbsolutePath(), filename);
        if(null != pswd){
            Document d = new Document(e.getMedia().getName().trim().replaceAll(" ", "_") + ".rar", "application/x-zip-compressed", 
                    e.getMedia().getFormat(), file.getParent() + "/" + filename + ".rar", pswd);
            d.setPage(page);
            d.setParent(uploaded);
            d.setMacroURI("/zk/document.zul");
            d.afterCompose();
        }
        else{
            Messagebox.show(ErrorMessage.ERROR_c00001, "操作失败", Messagebox.OK, Messagebox.ERROR);
            return;        
        }
    }
    public String pack(String path, String path2, String filename){
        密钥 = RandomUtil.randomString(9);
        
        String command = "/root/rar/rar a -df -ep -hp" + 密钥 + " " + path
            + ".rar " + path2;
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
        //C盘下面需要有winrar文件 command
= "c:/WinRAR/Rar a -ep -df -hp" + 密钥 + " " + path + ".rar " + path2; } System.out.println(command); InputStream ins = null; Process process = null; try { process = Runtime.getRuntime().exec(command); ins = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader( ins)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } return 密钥; } catch (IOException e) { Logger logger = LoggerFactory.getLogger(this.getClass()); logger.error(e.getMessage()); e.printStackTrace(); return null; } }
原文地址:https://www.cnblogs.com/kisstear/p/5062866.html