欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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);
  }
  // 給每個已提交作業(yè)的學(xué)生配一個map,userName-->AssignmentSubmit
  Map<String, AssignmentSubmit> studentSubmitMap = new HashMap<String, AssignmentSubmit>();
  for (AssignmentSubmit assignmentSubmit : assignmentSubmits) {
   String studentID = assignmentSubmit.getUserName();
   studentSubmitMap.put(studentID, assignmentSubmit);
  }
  // 根據(jù)班級號獲取該班所有學(xué)生的學(xué)號,再根據(jù)學(xué)號獲取詳情列表
  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);

  // 打包的方法我們會用到ZipOutputStream這樣一個輸出流, 所以這里我們把輸出流轉(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ù)手冊

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

相關(guān)文章

  • StringBuilder為什么線程不安全深入講解

    StringBuilder為什么線程不安全深入講解

    這篇文章主要給大家介紹了關(guān)于StringBuilder為什么線程不安全的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用StringBuilder線程具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • java實(shí)現(xiàn)http請求工具類示例

    java實(shí)現(xiàn)http請求工具類示例

    這篇文章主要介紹了java實(shí)現(xiàn)http請求工具類示例,需要的朋友可以參考下
    2014-05-05
  • Java使用FFmpeg處理視頻文件的方法教程

    Java使用FFmpeg處理視頻文件的方法教程

    這篇文章主要給大家介紹了關(guān)于Java使用FFmpeg處理視頻文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Spring?Retry實(shí)現(xiàn)重試機(jī)制的示例詳解

    Spring?Retry實(shí)現(xiàn)重試機(jī)制的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Spring-Retry的用法以及實(shí)現(xiàn)原理是怎么樣的,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下
    2023-07-07
  • java使用CKEditor實(shí)現(xiàn)圖片上傳功能

    java使用CKEditor實(shí)現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了java使用CKEditor實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用

    SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用

    這篇文章主要介紹了SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring使用支付寶掃碼支付

    Spring使用支付寶掃碼支付

    這篇文章主要為大家詳細(xì)介紹了Spring使用支付寶掃碼支付的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • SpringBoot整合七牛云上傳圖片的示例代碼

    SpringBoot整合七牛云上傳圖片的示例代碼

    本文就來介紹了SpringBoot整合七牛云上傳圖片的示例代碼,用戶在前端上傳圖片后,交由后端處理,上傳至七牛云,感興趣的可以了解一下
    2023-10-10
  • Java并發(fā)編程數(shù)據(jù)庫與緩存數(shù)據(jù)一致性方案解析

    Java并發(fā)編程數(shù)據(jù)庫與緩存數(shù)據(jù)一致性方案解析

    這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過程詳解

    springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過程詳解

    這篇文章主要介紹了springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評論