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; } }
  • 相关阅读:
    Silverlight 4版本升级到4.0.60531.0
    Silverlight实例教程 理解Navigation导航框架Page类
    微软官方在线培训课程汇总2011版
    分享Silverlight/WPF/Windows Phone一周学习导读(07月11日07月17日)
    Linux内核简介
    Brief Summary of IaaS, PaaS, SaaS
    On Amazon EC2's Underlying Architecture
    Linux进程管理(2)
    一个诡异的时间问题追查[转]
    查看一个进程打开了哪些文件的命令
  • 原文地址:https://www.cnblogs.com/kisstear/p/5062866.html
Copyright © 2011-2022 走看看