当前位置: 移动技术网 > IT编程>开发语言>Java > javaweb文件打包批量下载代码

javaweb文件打包批量下载代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

宋慈湖畔,村民委员会工作职责,盘龙续

本文实例为大家分享了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/ 老司机技术手册

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网