最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javaweb文件打包批量下載代碼

 更新時(shí)間:2016年06月28日 16:43:05   作者:acmjk  
這篇文章主要為大家詳細(xì)介紹了javaweb文件打包批量下載代碼,批量下載未批改作業(yè),感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了javaweb文件打包批量下載,供大家參考,具體內(nèi)容如下

// 批量下載未批改作業(yè)
 @RequestMapping(value = "/downloadAllHomework", method = RequestMethod.GET)
 public void downloadAllHomework(HttpSession httpSession, HttpServletRequest request, HttpServletResponse response, String assignmentid, int classCode) throws Exception {

  Site site = (Site) httpSession.getAttribute("site");
  String siteid = site.getId();

  // 根據(jù)作業(yè)ID獲取作業(yè)詳細(xì)信息
  AssignmentDetail assignmentDetail = assignmentServiceWS.getAssignmentDetail(assignmentid);
  generateParameters(assignmentDetail);

  // 信息不完整,后面需要填充。
  List<AssignmentSubmit> assignmentSubmitList = assignmentServiceWS.getSubmitedAssignmentStudent(assignmentid);

  // 獲取所有的submitid
  List<String> submitids = new ArrayList<String>();
  for (int i = 0; i < assignmentSubmitList.size(); i++) {
   String submitid = assignmentSubmitList.get(i).getId();
   if (submitid == null || submitid == "")
    continue;
   submitids.add(submitid);
  }
  // 獲取提交詳情
  List<AssignmentSubmit> assignmentSubmits = new ArrayList<AssignmentSubmit>();
  for (String a : submitids) {
   AssignmentSubmit as = assignmentServiceWS.getSubmitAssignment(a);
   assignmentSubmits.add(as);
  }
  // 給每個(gè)已提交作業(yè)的學(xué)生配一個(gè)map,userName-->AssignmentSubmit
  Map<String, AssignmentSubmit> studentSubmitMap = new HashMap<String, AssignmentSubmit>();
  for (AssignmentSubmit assignmentSubmit : assignmentSubmits) {
   String studentID = assignmentSubmit.getUserName();
   studentSubmitMap.put(studentID, assignmentSubmit);
  }
  // 根據(jù)班級(jí)號(hào)獲取該班所有學(xué)生的學(xué)號(hào),再根據(jù)學(xué)號(hào)獲取詳情列表
  List<AssignmentSubmit> assignmentStudentList = new ArrayList<AssignmentSubmit>();

  List<MemberVO> studentList = memberServiceWS.getStudents(siteid, classCode);
  for (MemberVO student : studentList) {

   String userName = student.getId();
   String realName = student.getName();
   AssignmentSubmit assignmentSubmit = new AssignmentSubmit();
   if (studentSubmitMap.get(userName) != null) {
    assignmentSubmit = studentSubmitMap.get(userName);
   }
   assignmentSubmit.setRealName(realName);
   assignmentSubmit.setUserName(userName);
   generateA(assignmentSubmit);
   assignmentStudentList.add(assignmentSubmit);
  }

  List<AssignmentSubmit> submitedList = new ArrayList<AssignmentSubmit>();
  for (AssignmentSubmit as : assignmentStudentList) {
   if (as.getGradePoint() == null && as.getAssignmentID() != null)
    submitedList.add(as);
  }

  List<File> files = new ArrayList<File>();

  File file = new File("d:/css.rar");
  if (!file.exists()) {
   file.createNewFile();
  }
  response.reset();
  // response.getWriter()
  // 創(chuàng)建文件輸出流
  FileOutputStream fous = new FileOutputStream(file);

  // 打包的方法我們會(huì)用到ZipOutputStream這樣一個(gè)輸出流, 所以這里我們把輸出流轉(zhuǎn)換一下

  ZipOutputStream zipOut = new ZipOutputStream(fous);
  for (AssignmentSubmit a : submitedList) {

   for (AttachIDs aa : a.getAttachIDs()) {
    try {
     String fileId = aa.getId();
     String cloudFileUrl = "http://xxx.xxx.xxx.xxx:8066/ImageService/DownloadFile/";
     String fileUrl = announceService.getAttachmentByFileid(fileId).getUrlUpload();
     fileUrl = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
     fileUrl = cloudFileUrl + fileUrl;
     String fileName = announceService.getAttachmentByFileid(fileId).getName(); // 獲取遠(yuǎn)程文件的文件名。
     // response.addHeader("Content-Disposition", "attachment;filename=" +
     // new String(fileName.getBytes("gbk"), "iso-8859-1"));
     // iso-8859-1
     ZipEntry entry = new ZipEntry(new String(fileName.getBytes("gbk"), "iso-8859-1"));
     zipOut.putNextEntry(entry);
     URL urlfile = null;
     HttpURLConnection httpUrl = null;
     urlfile = new URL(fileUrl);
     httpUrl = (HttpURLConnection) urlfile.openConnection();
     httpUrl.connect();
     InputStream downloadFile = httpUrl.getInputStream();
     int len = 0;
     byte[] buf = new byte[1024];
     while ((len = downloadFile.read(buf, 0, 1024)) != -1) {
      zipOut.write(buf, 0, len);
     }
    } catch (JSONException e) {
     e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
    }
   }
  }
  zipOut.close();
  fous.close();
  downloadZip(file, response);
 }
 // 把接受的全部文件打成壓縮包
 public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
  try {
   // 以流的形式下載文件。
   InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
   byte[] buffer = new byte[fis.available()];
   fis.read(buffer);
   fis.close();
   // 清空response
   response.reset();
   OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
   response.setContentType("application/octet-stream");

   // 如果輸出的是中文名的文件,在此處就要用URLEncoder.encode方法進(jìn)行處理
   response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
   toClient.write(buffer);
   toClient.flush();
   toClient.close();
  } catch (IOException ex) {
   ex.printStackTrace();
  } finally {
   try {
    File f = new File(file.getPath());
    f.delete();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return response;

 }

博客地址!http://oldriver.top/ 老司機(jī)技術(shù)手冊(cè)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

麻阳| 科技| 茶陵县| 公安县| 沧源| 罗城| 乃东县| 孝义市| 任丘市| 阿拉善左旗| 汉川市| 东兴市| 沙坪坝区| 墨竹工卡县| 霍州市| 乾安县| 望都县| 沁源县| 玉树县| 耒阳市| 祁门县| 育儿| 荔波县| 南昌市| 玉屏| 溆浦县| 延寿县| 观塘区| 苍梧县| 沛县| 宕昌县| 巴南区| 西乌珠穆沁旗| 满洲里市| 民乐县| 玉林市| 元朗区| 涟水县| 蒲江县| 永和县| 诸城市|