javaweb文件打包批量下载代码
本文实例为大家分享了javaweb文件打包批量下载,供大家参考,具体内容如下
//批量下载未批改作业 @RequestMapping(value="/downloadAllHomework",method=RequestMethod.GET) publicvoiddownloadAllHomework(HttpSessionhttpSession,HttpServletRequestrequest,HttpServletResponseresponse,Stringassignmentid,intclassCode)throwsException{ Sitesite=(Site)httpSession.getAttribute("site"); Stringsiteid=site.getId(); //根据作业ID获取作业详细信息 AssignmentDetailassignmentDetail=assignmentServiceWS.getAssignmentDetail(assignmentid); generateParameters(assignmentDetail); //信息不完整,后面需要填充。 List<AssignmentSubmit>assignmentSubmitList=assignmentServiceWS.getSubmitedAssignmentStudent(assignmentid); //获取所有的submitid List<String>submitids=newArrayList<String>(); for(inti=0;i<assignmentSubmitList.size();i++){ Stringsubmitid=assignmentSubmitList.get(i).getId(); if(submitid==null||submitid=="") continue; submitids.add(submitid); } //获取提交详情 List<AssignmentSubmit>assignmentSubmits=newArrayList<AssignmentSubmit>(); for(Stringa:submitids){ AssignmentSubmitas=assignmentServiceWS.getSubmitAssignment(a); assignmentSubmits.add(as); } //给每个已提交作业的学生配一个map,userName-->AssignmentSubmit Map<String,AssignmentSubmit>studentSubmitMap=newHashMap<String,AssignmentSubmit>(); for(AssignmentSubmitassignmentSubmit:assignmentSubmits){ StringstudentID=assignmentSubmit.getUserName(); studentSubmitMap.put(studentID,assignmentSubmit); } //根据班级号获取该班所有学生的学号,再根据学号获取详情列表 List<AssignmentSubmit>assignmentStudentList=newArrayList<AssignmentSubmit>(); List<MemberVO>studentList=memberServiceWS.getStudents(siteid,classCode); for(MemberVOstudent:studentList){ StringuserName=student.getId(); StringrealName=student.getName(); AssignmentSubmitassignmentSubmit=newAssignmentSubmit(); if(studentSubmitMap.get(userName)!=null){ assignmentSubmit=studentSubmitMap.get(userName); } assignmentSubmit.setRealName(realName); assignmentSubmit.setUserName(userName); generateA(assignmentSubmit); assignmentStudentList.add(assignmentSubmit); } List<AssignmentSubmit>submitedList=newArrayList<AssignmentSubmit>(); for(AssignmentSubmitas:assignmentStudentList){ if(as.getGradePoint()==null&&as.getAssignmentID()!=null) submitedList.add(as); } List<File>files=newArrayList<File>(); Filefile=newFile("d:/css.rar"); if(!file.exists()){ file.createNewFile(); } response.reset(); //response.getWriter() //创建文件输出流 FileOutputStreamfous=newFileOutputStream(file); //打包的方法我们会用到ZipOutputStream这样一个输出流,所以这里我们把输出流转换一下 ZipOutputStreamzipOut=newZipOutputStream(fous); for(AssignmentSubmita:submitedList){ for(AttachIDsaa:a.getAttachIDs()){ try{ StringfileId=aa.getId(); StringcloudFileUrl="http://xxx.xxx.xxx.xxx:8066/ImageService/DownloadFile/"; StringfileUrl=announceService.getAttachmentByFileid(fileId).getUrlUpload(); fileUrl=fileUrl.substring(fileUrl.lastIndexOf("/")+1); fileUrl=cloudFileUrl+fileUrl; StringfileName=announceService.getAttachmentByFileid(fileId).getName();//获取远程文件的文件名。 //response.addHeader("Content-Disposition","attachment;filename="+ //newString(fileName.getBytes("gbk"),"iso-8859-1")); //iso-8859-1 ZipEntryentry=newZipEntry(newString(fileName.getBytes("gbk"),"iso-8859-1")); zipOut.putNextEntry(entry); URLurlfile=null; HttpURLConnectionhttpUrl=null; urlfile=newURL(fileUrl); httpUrl=(HttpURLConnection)urlfile.openConnection(); httpUrl.connect(); InputStreamdownloadFile=httpUrl.getInputStream(); intlen=0; byte[]buf=newbyte[1024]; while((len=downloadFile.read(buf,0,1024))!=-1){ zipOut.write(buf,0,len); } }catch(JSONExceptione){ e.printStackTrace(); }catch(UnsupportedEncodingExceptione){ e.printStackTrace(); } } } zipOut.close(); fous.close(); downloadZip(file,response); } //把接受的全部文件打成压缩包 publicstaticHttpServletResponsedownloadZip(Filefile,HttpServletResponseresponse){ try{ //以流的形式下载文件。 InputStreamfis=newBufferedInputStream(newFileInputStream(file.getPath())); byte[]buffer=newbyte[fis.available()]; fis.read(buffer); fis.close(); //清空response response.reset(); OutputStreamtoClient=newBufferedOutputStream(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(IOExceptionex){ ex.printStackTrace(); }finally{ try{ Filef=newFile(file.getPath()); f.delete(); }catch(Exceptione){ e.printStackTrace(); } } returnresponse; }
博客地址!http://oldriver.top/老司机技术手册
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。