javaweb文件打包批量下载代码

 更新时间:2016年06月28日 16:43:05   作者:acmjk  
这篇文章主要为大家详细介绍了javaweb文件打包批量下载代码,批量下载未批改作业,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了javaweb文件打包批量下载,供大家参考,具体内容如下

// 批量下载未批改作业
 @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();

  // 根据作业ID获取作业详细信息
  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);
  }
  // 给每个已提交作业的学生配一个map,userName-->AssignmentSubmit
  Map<String, AssignmentSubmit> studentSubmitMap = new HashMap<String, AssignmentSubmit>();
  for (AssignmentSubmit assignmentSubmit : assignmentSubmits) {
   String studentID = assignmentSubmit.getUserName();
   studentSubmitMap.put(studentID, assignmentSubmit);
  }
  // 根据班级号获取该班所有学生的学号,再根据学号获取详情列表
  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()
  // 创建文件输出流
  FileOutputStream fous = new FileOutputStream(file);

  // 打包的方法我们会用到ZipOutputStream这样一个输出流, 所以这里我们把输出流转换一下

  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(); // 获取远程文件的文件名。
     // 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方法进行处理
   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/ 老司机技术手册

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • StringBuilder为什么线程不安全深入讲解

    StringBuilder为什么线程不安全深入讲解

    这篇文章主要给大家介绍了关于StringBuilder为什么线程不安全的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用StringBuilder线程具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • java实现http请求工具类示例

    java实现http请求工具类示例

    这篇文章主要介绍了java实现http请求工具类示例,需要的朋友可以参考下
    2014-05-05
  • Java使用FFmpeg处理视频文件的方法教程

    Java使用FFmpeg处理视频文件的方法教程

    这篇文章主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Spring Retry实现重试机制的示例详解

    Spring Retry实现重试机制的示例详解

    这篇文章主要为大家详细介绍了Spring-Retry的用法以及实现原理是怎么样的,文中的示例代码讲解详细,具有一定的参考价值,需要的可以了解一下
    2023-07-07
  • java使用CKEditor实现图片上传功能

    java使用CKEditor实现图片上传功能

    这篇文章主要为大家详细介绍了java使用CKEditor实现图片上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • SpringCloud让微服务实现指定程序调用

    SpringCloud让微服务实现指定程序调用

    这篇文章主要介绍了SpringCloud让微服务实现指定程序调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Spring使用支付宝扫码支付

    Spring使用支付宝扫码支付

    这篇文章主要为大家详细介绍了Spring使用支付宝扫码支付的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • SpringBoot整合七牛云上传图片的示例代码

    SpringBoot整合七牛云上传图片的示例代码

    本文就来介绍了SpringBoot整合七牛云上传图片的示例代码,用户在前端上传图片后,交由后端处理,上传至七牛云,感兴趣的可以了解一下
    2023-10-10
  • Java并发编程数据库与缓存数据一致性方案解析

    Java并发编程数据库与缓存数据一致性方案解析

    这篇文章主要为大家介绍了Java并发编程中数据库与缓存数据一致性解决方案,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • springboot 使用poi进行数据的导出过程详解

    springboot 使用poi进行数据的导出过程详解

    这篇文章主要介绍了springboot 使用poi进行数据的导出过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09

最新评论