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; } }
  • 相关阅读:
    【转载】Hibernate---在Hibernate中获取数据方式与缓存使用
    【转载】Hibernate ORM 新特性之 Service(Registry)
    【转载】Hibernate三种状态的区分,以及save,update,saveOrUpdate,merge等的使用
    【转载】Java Cache系列之Cache概述和Simple Cache
    C#进行AutoCAD2014二次开发的注意事项
    WIN10更新后重新激活CAD
    获取子图元
    常用语句
    向命令行发送命令
    DatagridView内容自动换行和换行符换行
  • 原文地址:https://www.cnblogs.com/kisstear/p/5062866.html
Copyright © 2011-2022 走看看