javaweb文件打包批量下載代碼。本站提示廣大學習愛好者:(javaweb文件打包批量下載代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是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/ 老司機技巧手冊
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。