javaweb文件打包批量下载代码
作者:acmjk 发布时间:2022-10-24 17:10:17
标签: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/ 老司机技术手册


猜你喜欢
- 注释介绍@Cacheable@Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存@Cacheable 作用
- Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finc
- 本文实例讲述了C#实现字体旋转的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Colle
- 嗯,就是BASE64,不用多想,本来计划是要跟上一篇字符串压缩一起写的,用来实现将一个文件可以用json或者text等方式进行接口之间的传输
- android仿照qq的顶部栏效果,主要就是利用fragment manager把fragment设置显示内容(1)在activity_ma
- 前言今天给大家带来一个新的控件–轮播图,网上已经有很多这类的博客来讲解如何实现的,那么我的这个有哪些特点呢?或是说有哪些不同呢?满足了轮播图
- 本文实例讲述了Java泛型的使用限制。分享给大家供大家参考,具体如下:一 什么情况下不能使用泛型1 不能使用泛型的形参创建对象。T o=ne
- Druid连接池连接池思想在程序初始化时,提前创建好指定数量的数据库连接对象存储在“池子”中(这个池
- 环境:maven+idea。1. 需要的jar包基本的spring和mybatis依赖包就不说了,在pom文件的build->plug
- 1. 简介直到4g时代,流量依然是宝贵的东西。而移动网络传输中,最占流量的一种载体:图片,成为了我们移动开发者不得不关注的一个问题。我们关注
- 一、基本概念1、进程首先打开任务管理器,查看当前运行的进程:从任务管理器里面可以看到当前所有正在运行的进程。那么究竟什么是进程呢?进程(Pr
- 导入依赖菜单大部分情况下不会出现变化,我们可以将其放入Redis 加快加载速度<dependency><groupId&g
- 1.修改主站点的elasticsearch.yml添加一下行:xpack.security.enabled: true2.生成安全秘钥切到E
- RequestHeaders添加自定义参数在开发过程中有的时候,参数需要绑定到requestHeaders中,而并不是在body中进行传输。
- 前言static静态的,不变的,在某个类中只有一个,不会因实例化对象的不同而不同。static可以修饰类、字段、属性、方法等。如一个方法前加
- 在内核编程中字符串有两种格式ANSI_STRING与UNICODE_STRING,这两种格式是微软推出的安全版本的字符串结构体,也是微软推荐
- 1.定义多态是同一个行为具有多个不同表现形式或形态的能力。多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口
- 像360卫士的波浪球进度的效果,一般最常用的方法就是画线的方式,先绘sin线或贝塞尔曲线,然后从左到右绘制竖线,然后再裁剪圆区域。今天我这用
- 在项目开发中,我们经常会遇到表中的字段名和表对应实体类的属性名称不一定都是完全相同的情况,下面小编给大家演示一下这种情况下的如何解决字段名与
- 很多人都知道:浮点数值不适用于无法接受舍入误差的金融计算中,即:我们常说的丢失精度问题。这是为什么呢?很多人还知道这样一句话:这种舍入误差的