zoukankan      html  css  js  c++  java
  • 上传附件,压缩并加密

        /**
         * 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; } }
  • 相关阅读:
    iOS开发实用干货——强化你的Xcode控制台
    Android App 性能优化实践
    AFNetworking 之于 https 认证
    点击 Run 之后发生了什么?
    happypack 原理解析
    JavaScript 笔记 ( Prototype )
    成立快两年的安卓绿色联盟,现在怎么样了?
    盘点20款主流应用FPS,最Skr帧率测试方法都在这里!
    探寻百度AI3.0背后的技术实践
    流畅购物哪家强?购物类应用“页面过度绘制”情况调查
  • 原文地址:https://www.cnblogs.com/kisstear/p/5062866.html
Copyright © 2011-2022 走看看