JAVA上傳批量下載文件 – JAVA編程語言程序開發技術文章

筆記記錄在此。

上傳:目錄結構:論述題ID/批次ID/省份ID/文件

[java]
String basePath = request().getContextPath(); 
                basePath = request().getScheme() + "://" + request().getServerName() + ":" + request().getServerPort() + basePath + "/"; 
                String realPath = request().getSession().getServletContext().getRealPath(""); // 獲取服務器真實路徑地址  
                String root = "upload"; 
                String article = "article"; 
                String dir = ServletActionContext.getServletContext().getRealPath("/" + root); 
                dir = dir.replaceAll("\\\\", "/"); 
                File f0 = new File(dir);// 根目錄upload  
                if (!f0.exists()) { 
                    f0.mkdir(); 
                } 
                dir = dir + "/" + article;// 論述題目錄article  
                File f1 = new File(dir); 
                if (!f1.exists()) { 
                    f1.mkdir(); 
                } 
                dir = dir + "/" + peExamBatch.getId();// 當前批次目錄  
                File f2 = new File(dir); 
                if (!f2.exists()) { 
                    f2.mkdir(); 
                } 
                String code = bzzStudent.getPeEnterprise().getEnumConstByFlagSheng().getId(); 
                dir = dir + "/" + code;// 省份目錄  
                File f3 = new File(dir); 
                if (!f3.exists()) { 
                    f3.mkdir(); 
                } 
                dir = dir + "/" + bzzStudent.getRegNo() + prefix; 
                if (getUpload() != null && !"".equals(getUpload().getPath())) { 
                    FileInputStream fis = new FileInputStream(getUpload()); 
                    // 其實這裡可以判斷articlePath文件是否存在,如果存在直接存放而省去取批次,省以及學號等的數據庫操作.安全起見  
                    FileOutputStream fos = new FileOutputStream(dir); 
                    int i = 0; 
                    byte[] buf = new byte[1024]; 
                    while ((i = fis.read(buf)) != -1) { 
                        fos.write(buf, 0, i); 
                    } 
                    fos.flush(); 
                    fos.close(); 
                    String fullpath = root + "/" + article + "/" + peExamBatch.getId() + "/" + code;// upload/article/當前批次ID/企業所在省CODE  
                    String photoURL = basePath + fullpath + "/" + bzzStudent.getRegNo() + prefix;// 文件名為  
                    // 學號+後綴  
                    peBzzExamScore.setArticlePath(photoURL); 
                    this.getGeneralService().save(peBzzExamScore); 
                    request().setAttribute("js", "alert('您的論述題已成功上傳!');window.location='/entity/workspaceStudent/bzzstudent_aboutArticle.action';"); 
                } 

String basePath = request().getContextPath();
    basePath = request().getScheme() + "://" + request().getServerName() + ":" + request().getServerPort() + basePath + "/";
    String realPath = request().getSession().getServletContext().getRealPath(""); // 獲取服務器真實路徑地址
    String root = "upload";
    String article = "article";
    String dir = ServletActionContext.getServletContext().getRealPath("/" + root);
    dir = dir.replaceAll("\\\\", "/");
    File f0 = new File(dir);// 根目錄upload
    if (!f0.exists()) {
     f0.mkdir();
    }
    dir = dir + "/" + article;// 論述題目錄article
    File f1 = new File(dir);
    if (!f1.exists()) {
     f1.mkdir();
    }
    dir = dir + "/" + peExamBatch.getId();// 當前批次目錄
    File f2 = new File(dir);
    if (!f2.exists()) {
     f2.mkdir();
    }
    String code = bzzStudent.getPeEnterprise().getEnumConstByFlagSheng().getId();
    dir = dir + "/" + code;// 省份目錄
    File f3 = new File(dir);
    if (!f3.exists()) {
     f3.mkdir();
    }
    dir = dir + "/" + bzzStudent.getRegNo() + prefix;
    if (getUpload() != null && !"".equals(getUpload().getPath())) {
     FileInputStream fis = new FileInputStream(getUpload());
     // 其實這裡可以判斷articlePath文件是否存在,如果存在直接存放而省去取批次,省以及學號等的數據庫操作.安全起見
     FileOutputStream fos = new FileOutputStream(dir);
     int i = 0;
     byte[] buf = new byte[1024];
     while ((i = fis.read(buf)) != -1) {
      fos.write(buf, 0, i);
     }
     fos.flush();
     fos.close();
     String fullpath = root + "/" + article + "/" + peExamBatch.getId() + "/" + code;// upload/article/當前批次ID/企業所在省CODE
     String photoURL = basePath + fullpath + "/" + bzzStudent.getRegNo() + prefix;// 文件名為
     // 學號+後綴
     peBzzExamScore.setArticlePath(photoURL);
     this.getGeneralService().save(peBzzExamScore);
     request().setAttribute("js", "alert('您的論述題已成功上傳!');window.location='/entity/workspaceStudent/bzzstudent_aboutArticle.action';");
    }
下載:

 

[java]
String root = "upload"; 
        String article = "article"; 
        String m = "alert('當前%s下還沒有論述題');"; 
        String dir = ServletActionContext.getServletContext().getRealPath("/" + root); 
        dir = dir.replaceAll("\\\\", "/"); 
        String upload = dir;// 記錄下upload目錄  
        File f0 = new File(dir);// 根目錄upload  
        if (!f0.exists()) { 
            request().setAttribute("js", String.format(m, "根目錄")); 
            return "message"; 
        } 
        dir = dir + "/" + article;// 論述題目錄article  
        File f1 = new File(dir); 
        if (!f1.exists()) { 
            request().setAttribute("js", String.format(m, "論述題目錄")); 
            return "message"; 
        } 
        dir = dir + "/" + peBzzExamBatch.getId();// 當前批次目錄  
        File f2 = new File(dir); 
        if (!f2.exists()) { 
            request().setAttribute("js", String.format(m, "批次")); 
            return "message"; 
        } 
        String zip = dir + "/zip";// 臨時目錄用於存放壓縮後文件目錄  
        String srctemp = dir + "/srctemp";// 臨時目錄用於存放要壓縮文件的目錄  
        String provid = request().getParameter("prov"); 
        if (StringUtils.isEmpty(provid)) { 
            request().setAttribute("js", "alert('請先選擇省份!');"); 
            return "message"; 
        } 
        dir = dir + "/" + provid;// 省份  
        File f3 = new File(dir); 
        if (!f3.exists()) { 
            request().setAttribute("js", String.format(m, "省份")); 
            return "message"; 
        } 
        File f4 = new File(zip); 
        if (!f4.exists()) { 
            f4.mkdir(); 
        } 
        String enterpriseid = request().getParameter("enterprise"); 
        FileManage filemanage = FileManageFactory.creat(); 
        try { 
            if (StringUtils.isNotEmpty(enterpriseid) && !"-1".equals(enterpriseid)) {// 如果選擇瞭一級工作單位  
                DetachedCriteria score = DetachedCriteria.forClass(PeBzzExamScore.class); 
                DetachedCriteria student = score.createCriteria("peBzzStudent", "peBzzStudent"); 
                DetachedCriteria pe = student.createCriteria("peEnterprise", "peEnterprise"); 
                student.add(Restrictions.or(Restrictions.eq("peBzzStudent.peEnterprise.id", enterpriseid), Restrictions.eq("peEnterprise.peEnterprise.id", enterpriseid))); 
                score.createCriteria("enumConstByFlagExamScoreStatus", "enumConstByFlagExamScoreStatus"); 
                score.add(Restrictions.eq("enumConstByFlagExamScoreStatus.id", "402880a9aaadc115061dadfcf26b0003"));// 已審核  
                String enterpriseid2 = request().getParameter("enterprise2"); 
                if (StringUtils.isNotEmpty(enterpriseid2) && !"-1".equals(enterpriseid2)) {// 如果選擇瞭二級工作單位  
                    pe.add(Restrictions.eq("peEnterprise.id", enterpriseid2)); 
                } 
                List<PeBzzExamScore> scores = this.getGeneralService().getDetachList(score); 
                if (scores != null && scores.size() == 0) { 
                    request().setAttribute("js", "alert('當前條件下沒有論述題');"); 
                    return "message"; 
                } 
                File f5 = new File(srctemp); 
                if (!f5.exists()) { 
                    f5.mkdir(); 
                } 
                dir = srctemp; 
                for (PeBzzExamScore s : scores) { 
                    String path = s.getArticlePath(); 
                    if (StringUtils.isNotEmpty(path)) { 
                        int index = path.indexOf("/article"); 
                        if (index > 0) { 
                            String str = path.substring(index); 
                            filemanage.copyFile(upload + str, srctemp);// 復制文件  
                        } 
                    } 
                } 
            } 
            String targetFile = zip + "/" + provid + ".zip"; 
            if (filemanage.isExist(targetFile)) { 
                filemanage.delete(targetFile); 
            } 
            if (!filemanage.getSize(dir).equals("0B")) { 
                filemanage.zip(dir, targetFile);// 壓縮至指定文件夾  
                String basePath = request().getContextPath(); 
                basePath = request().getScheme() + "://" + request().getServerName() + ":" + request().getServerPort() + basePath + "/"; 
                int index = targetFile.indexOf("upload/"); 
                if (index > 0) { 
                    String str = targetFile.substring(index); 
                    // request().setAttribute("js",  
                    // String.format("window.open('%s');", basePath+str));  
                    request().setAttribute("msg", "下載頁面"); 
                    request().setAttribute("js", String.format("window.location.href='%s';", basePath + str)); 
                    return "message"; 
                } 
            } else { 
                request().setAttribute("js", "alert('當前條件下沒有論述題');"); 
                return "message"; 
            } 
        } catch (Exception e) { 
            request().setAttribute("msg", "導出失敗!"); 
            e.printStackTrace(); 
        } 
        return "message"; 

String root = "upload";
  String article = "article";
  String m = "alert('當前%s下還沒有論述題');";
  String dir = ServletActionContext.getServletContext().getRealPath("/" + root);
  dir = dir.replaceAll("\\\\", "/");
  String upload = dir;// 記錄下upload目錄
  File f0 = new File(dir);// 根目錄upload
  if (!f0.exists()) {
   request().setAttribute("js", String.format(m, "根目錄"));
   return "message";
  }
  dir = dir + "/" + article;// 論述題目錄article
  File f1 = new File(dir);
  if (!f1.exists()) {
   request().setAttribute("js", String.format(m, "論述題目錄"));
   return "message";
  }
  dir = dir + "/" + peBzzExamBatch.getId();// 當前批次目錄
  File f2 = new File(dir);
  if (!f2.exists()) {
   request().setAttribute("js", String.format(m, "批次"));
   return "message";
  }
  String zip = dir + "/zip";// 臨時目錄用於存放壓縮後文件目錄
  String srctemp = dir + "/srctemp";// 臨時目錄用於存放要壓縮文件的目錄
  String provid = request().getParameter("prov");
  if (StringUtils.isEmpty(provid)) {
   request().setAttribute("js", "alert('請先選擇省份!');");
   return "message";
  }
  dir = dir + "/" + provid;// 省份
  File f3 = new File(dir);
  if (!f3.exists()) {
   request().setAttribute("js", String.format(m, "省份"));
   return "message";
  }
  File f4 = new File(zip);
  if (!f4.exists()) {
   f4.mkdir();
  }
  String enterpriseid = request().getParameter("enterprise");
  FileManage filemanage = FileManageFactory.creat();
  try {
   if (StringUtils.isNotEmpty(enterpriseid) && !"-1".equals(enterpriseid)) {// 如果選擇瞭一級工作單位
    DetachedCriteria score = DetachedCriteria.forClass(PeBzzExamScore.class);
    DetachedCriteria student = score.createCriteria("peBzzStudent", "peBzzStudent");
    DetachedCriteria pe = student.createCriteria("peEnterprise", "peEnterprise");
    student.add(Restrictions.or(Restrictions.eq("peBzzStudent.peEnterprise.id", enterpriseid), Restrictions.eq("peEnterprise.peEnterprise.id", enterpriseid)));
    score.createCriteria("enumConstByFlagExamScoreStatus", "enumConstByFlagExamScoreStatus");
    score.add(Restrictions.eq("enumConstByFlagExamScoreStatus.id", "402880a9aaadc115061dadfcf26b0003"));// 已審核
    String enterpriseid2 = request().getParameter("enterprise2");
    if (StringUtils.isNotEmpty(enterpriseid2) && !"-1".equals(enterpriseid2)) {// 如果選擇瞭二級工作單位
     pe.add(Restrictions.eq("peEnterprise.id", enterpriseid2));
    }
    List<PeBzzExamScore> scores = this.getGeneralService().getDetachList(score);
    if (scores != null && scores.size() == 0) {
     request().setAttribute("js", "alert('當前條件下沒有論述題');");
     return "message";
    }
    File f5 = new File(srctemp);
    if (!f5.exists()) {
     f5.mkdir();
    }
    dir = srctemp;
    for (PeBzzExamScore s : scores) {
     String path = s.getArticlePath();
     if (StringUtils.isNotEmpty(path)) {
      int index = path.indexOf("/article");
      if (index > 0) {
       String str = path.substring(index);
       filemanage.copyFile(upload + str, srctemp);// 復制文件
      }
     }
    }
   }
   String targetFile = zip + "/" + provid + ".zip";
   if (filemanage.isExist(targetFile)) {
    filemanage.delete(targetFile);
   }
   if (!filemanage.getSize(dir).equals("0B")) {
    filemanage.zip(dir, targetFile);// 壓縮至指定文件夾
    String basePath = request().getContextPath();
    basePath = request().getScheme() + "://" + request().getServerName() + ":" + request().getServerPort() + basePath + "/";
    int index = targetFile.indexOf("upload/");
    if (index > 0) {
     String str = targetFile.substring(index);
     // request().setAttribute("js",
     // String.format("window.open('%s');", basePath+str));
     request().setAttribute("msg", "下載頁面");
     request().setAttribute("js", String.format("window.location.href='%s';", basePath + str));
     return "message";
    }
   } else {
    request().setAttribute("js", "alert('當前條件下沒有論述題');");
    return "message";
   }
  } catch (Exception e) {
   request().setAttribute("msg", "導出失敗!");
   e.printStackTrace();
  }
  return "message";

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *