@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();
AssignmentDetail assignmentDetail = assignmentServiceWS.getAssignmentDetail(assignmentid);
generateParameters(assignmentDetail);
List<AssignmentSubmit> assignmentSubmitList = assignmentServiceWS.getSubmitedAssignmentStudent(assignmentid);
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<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();
FileOutputStream fous =
new FileOutputStream(file);
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();
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.reset();
OutputStream toClient =
new BufferedOutputStream(response.getOutputStream());
response.setContentType(
"application/octet-stream");
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;
}