javaweb文件打包批量下载代码

作者:acmjk 时间:2022-10-24 17:10:17 

本文实例为大家分享了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/ 老司机技术手册

标签:javaweb,下载
0
投稿

猜你喜欢

  • Java单元测试工具之JUnit的使用

    2022-09-05 13:20:54
  • Java ThreadLocal类应用实战案例分析

    2022-07-11 19:04:36
  • Android实现数字跳动效果的TextView方法示例

    2023-05-24 16:54:29
  • Android异步下载图片并且缓存图片到本地DEMO详解

    2022-10-27 14:16:17
  • Android开发之对话框案例详解(五种对话框)

    2021-08-07 11:37:47
  • Android开发手册TextInputLayout样式使用示例

    2023-10-16 11:03:12
  • idea打包java可执行jar包的实现步骤

    2022-07-30 02:57:48
  • Android使用gallery和imageSwitch制作可左右循环滑动的图片浏览器

    2021-08-31 22:49:45
  • Java项目有中多个线程如何查找死锁

    2023-01-06 06:17:35
  • java代码执行字符串中的逻辑运算方法

    2023-11-29 12:13:06
  • spring boot中使用@Async实现异步调用任务

    2023-04-25 13:35:18
  • springboot项目启动慢的问题排查方式

    2023-06-19 18:58:40
  • Java数据结构之并查集的实现

    2023-06-29 06:58:32
  • java数据结构之java实现栈

    2023-11-25 05:32:36
  • 轻松实现可扩展自定义的Android滚轮时间选择控件

    2022-06-25 04:21:24
  • mybatis中使用InsertProvider注解报错解决全过程

    2021-06-25 18:36:42
  • 深入了解Java线程池的原理使用及性能优化

    2023-02-17 22:35:31
  • 基于私钥加密公钥解密的RSA算法C#实现方法

    2022-12-01 07:52:37
  • Java面向对象之猜拳游戏

    2022-10-29 21:26:45
  • Android 实现微信登录详解

    2022-07-14 06:34:11
  • asp之家 软件编程 m.aspxhome.com