javaweb文件打包批量下載代碼
本文實(shí)例為大家分享了javaweb文件打包批量下載,供大家參考,具體內(nèi)容如下
// 批量下載未批改作業(yè) @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(); // 根據(jù)作業(yè)ID獲取作業(yè)詳細(xì)信息 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); } // 給每個已提交作業(yè)的學(xué)生配一個map,userName-->AssignmentSubmit Map<String, AssignmentSubmit> studentSubmitMap = new HashMap<String, AssignmentSubmit>(); for (AssignmentSubmit assignmentSubmit : assignmentSubmits) { String studentID = assignmentSubmit.getUserName(); studentSubmitMap.put(studentID, assignmentSubmit); } // 根據(jù)班級號獲取該班所有學(xué)生的學(xué)號,再根據(jù)學(xué)號獲取詳情列表 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() // 創(chuàng)建文件輸出流 FileOutputStream fous = new FileOutputStream(file); // 打包的方法我們會用到ZipOutputStream這樣一個輸出流, 所以這里我們把輸出流轉(zhuǎn)換一下 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(); // 獲取遠(yuǎn)程文件的文件名。 // 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方法進(jìn)行處理 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/ 老司機(jī)技術(shù)手冊
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)批量下載(打包成zip)的實(shí)現(xiàn)
- Java OSS批量下載并壓縮為ZIP代碼實(shí)例
- java實(shí)現(xiàn)批量下載 多文件打包成zip格式下載
- java后臺批量下載文件并壓縮成zip下載的方法
- JAVA SFTP文件上傳、下載及批量下載實(shí)例
- Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包
- Java實(shí)現(xiàn)創(chuàng)建Zip壓縮包并寫入文件
- Java實(shí)現(xiàn)導(dǎo)出ZIP壓縮包的方法
- Java后臺實(shí)現(xiàn)瀏覽器一鍵導(dǎo)出下載zip壓縮包
- java批量下載生成zip壓縮包的思路詳解
相關(guān)文章
Spring?Retry實(shí)現(xiàn)重試機(jī)制的示例詳解
這篇文章主要為大家詳細(xì)介紹了Spring-Retry的用法以及實(shí)現(xiàn)原理是怎么樣的,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下2023-07-07java使用CKEditor實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了java使用CKEditor實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用
這篇文章主要介紹了SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java并發(fā)編程數(shù)據(jù)庫與緩存數(shù)據(jù)一致性方案解析
這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過程詳解
這篇文章主要介紹了springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09