This article has shared the examples of javaweb file packaging and batch download for your reference. The specific content is as follows
// Batch download unresolved job @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(); // Obtain job details based on the job ID AssignmentDetail assignmentDetail = assignmentServiceWS.getAssignmentDetail(assignmentid); generateParameters(assignmentDetail); // The information is incomplete and needs to be filled later. List<AssignmentSubmitList = assignmentServiceWS.getSubmitedAssignmentStudent(assignmentid); // Get all 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); } // Get submission details List<AssignmentSubmits> assignmentSubmits = new ArrayList<AssignmentSubmit>(); for (String a : submitids) { AssignmentSubmit as = assignmentServiceWS.getSubmitAssignment(a); assignmentSubmits.add(as); } // Assign each student who has submitted the assignment a map, userName-->AssignmentSubmit Map<String, AssignmentSubmit> studentSubmitMap = new HashMap<String, AssignmentSubmit>(); for (AssignmentSubmit assignmentSubmit : assignmentSubmits) { String studentID = assignmentSubmit.getUserName(); studentSubmitMap.put(studentID, assignmentSubmit); } // Obtain the student ID of all students in the class according to the class number, and then obtain the detailed list based on the student ID 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> submittedList = new ArrayList<AssignmentSubmit>(); for (AssignmentSubmit as : assignmentStudentList) { if (as.getGradePoint() == null && as.getAssignmentID() != null) submittedList.add(as); } List<File> files = new ArrayList<File>(); File file = new File("d:/css.rar"); if (!file.exists()) { file.createNewFile(); } response.reset(); // response.getWriter() // Create file output stream FileOutputStream fus = new FileOutputStream(file); // In the packaged method, we will use an output stream like ZipOutputStream, so here we convert the output stream ZipOutputStream zipOut = new ZipOutputStream(fous); for (AssignmentSubmit a: submittedList) { for (AttachIDs aa : a.getAttachIDs()) { try { String fileId = aa.getId(); String cloudFileUrl = "http://xxx.xxx.xxx.xxx:8066/ImageService/DownloadFile/"; String fileUrl = announcementService.getAttachmentByFileid(fileId).getUrlUpload(); fileUrl = fileUrl.substring(fileUrl.lastIndexOf("/") + 1); fileUrl = cloudFileUrl + fileUrl; String fileName = announcementService.getAttachmentByFileid(fileId).getName(); // Get the file name of the remote file. // 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); } // Make all accepted files into compressed packages public static HttpServletResponse downloadZip(File file, HttpServletResponse response) { try { // Download the file in the form of a stream. InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // Clear response response response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); // If the output is a file with a Chinese name, you must use the URLEncoder.encode method to process 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; }Blog address! http://oldriver.top/ Technical Manual of Old Drivers
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.