服务器之家

服务器之家 > 正文

javaweb文件打包批量下载代码

时间:2020-05-22 10:39     来源/作者:acmjk

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 批量下载未批改作业
 @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/ 老司机技术手册

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

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部