我试图从HTTP POST接收一个文件,我将其分成单个文件,然后需要在zip文件中返回该文件。我试图返回文件,我已经尝试了所有返回文件,但我无法收到文件...任何想法?
@RequestMapping(value = "/splitFile", headers = ("content-type=multipart/*"), produces ="application/zip", method = RequestMethod.POST)
public void SplitFile(@RequestParam(value = "file", required = true) MultipartFile[] file, HttpServletResponse response ) throws IOException {
for (int i = 0; i < file.length; i++) {
File uplFile = new File(file[i].getOriginalFilename());
uplFile.createNewFile();
FileOutputStream fos = new FileOutputStream(uplFile);
fos.write(file[i].getBytes());
fos.close();
System.out.println("Splitting File: " + uplFile.getName());
uploadHelper Help = new uploadHelper();
ArrayList<String> filestrings = new ArrayList<String>();
filestrings = uplHelp.GetSplitFiles(uplFile);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);
StringBuilder sb = new StringBuilder();
int filenumber = 0;
for (String filedata : filestrings) {
filenumber = filenumber + 1;
sb.append(filedata);
ZipEntry e = new ZipEntry("file_" + filenumber + ".data");
zipOutputStream.putNextEntry(e);
byte[] data = sb.toString().getBytes();
zipOutputStream.write(data, 0, data.length);
zipOutputStream.closeEntry();
}
System.out.println("Splitting File - Split " + filestrings.size() + " files");
if (zipOutputStream != null) {
zipOutputStream.finish();
zipOutputStream.flush();
IOUtils.closeQuietly(zipOutputStream);
}
IOUtils.closeQuietly(bufferedOutputStream);
IOUtils.closeQuietly(out);
InputStream is = new ByteArrayInputStream(out.toByteArray());
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader("Content-Disposition", "attachment; filename=test.zip");
response.setCharacterEncoding("gzip");
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
}
response.setStatus(HttpServletResponse.SC_ACCEPTED);
response.flushBuffer();
}