我的表单内有好几个文件上传控件,每个控件里有多个文件。在后台用了MultipartFile 接受后 我想把每个框的文件压缩成一个zip,并且直接生成base64位的zip字符串,因为我需要在webservice中传输,目前的情况是生成文件没有问题,但是在转base64位时好像会破坏这个压缩文件,导致解码后打不开。如果先生成一个文件,再通过文件流的形式转base64位是没有问题的。我想问下这个有办法解决吗?
代码部分:
/**
文件压缩zip
*/
public static String ZipFiles(MultipartFile[] srcfile,HttpServletRequest request) {
byte[] buf = new byte[1024];
ZipOutputStream out = null;
String compressedStr = null;
ByteArrayOutputStream bout = null;
try {
bout= new ByteArrayOutputStream();
out = new ZipOutputStream(bout);
for (int i = 0; i < srcfile.length; i++) {
System.out.print(srcfile[i].getOriginalFilename());
InputStream in = srcfile[i].getInputStream();
System.out.print(in);
out.putNextEntry(new ZipEntry(srcfile[i].getOriginalFilename()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.closeEntry();
}
byte[] compressed = bout.toByteArray();
compressedStr = encoder.encodeBuffer(compressed);
out.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return compressedStr;
}public static void decoderBase64File(String base64Code)
throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream("d://copytest.zip");
out.write(buffer);
out.close(); }
这样生成的文件缺失报 不可预料的压缩文件末端。。如果先生成一个文件再用文件流的方式生成base64位是可以的,但是我不想中间有这么一步或者大家有什么别的方式可以解决?
代码部分:
/**
文件压缩zip
*/
public static String ZipFiles(MultipartFile[] srcfile,HttpServletRequest request) {
byte[] buf = new byte[1024];
ZipOutputStream out = null;
String compressedStr = null;
ByteArrayOutputStream bout = null;
try {
bout= new ByteArrayOutputStream();
out = new ZipOutputStream(bout);
for (int i = 0; i < srcfile.length; i++) {
System.out.print(srcfile[i].getOriginalFilename());
InputStream in = srcfile[i].getInputStream();
System.out.print(in);
out.putNextEntry(new ZipEntry(srcfile[i].getOriginalFilename()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.closeEntry();
}
byte[] compressed = bout.toByteArray();
compressedStr = encoder.encodeBuffer(compressed);
out.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return compressedStr;
}public static void decoderBase64File(String base64Code)
throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream("d://copytest.zip");
out.write(buffer);
out.close(); }
这样生成的文件缺失报 不可预料的压缩文件末端。。如果先生成一个文件再用文件流的方式生成base64位是可以的,但是我不想中间有这么一步或者大家有什么别的方式可以解决?
解决方案 »
- 谁能给几个jsp网站的源码和数据库给我。明天面试,应届生没项目经验。想拿几个站去忽悠HR~
- 如何实现URL后面跟用户名就跳转到用户的首页?
- java关于url的疑惑
- 100分求一个最简单的Struts小程序。
- struts配置问题--有些莫名奇妙
- 初学者,jbuilder关于jsp的问题!急,在线等!!!,大家帮忙啊,谢谢了!!
- rss是什么东西啊?
- 在eclipse下写jsp怎么不出现动态提醒?
- 帮个忙,为什么网址后面附加的变量不能显示中文!
- 在jsp中如何实现与asp中的Request.ServerVariables("SCRIPT_NAME")同等的功能?
- springboot 2集成mybatis遇到错误
- dom4j 解析XML 非法反射 求大佬解决
or
byte[] compressed = bout.toByteArray();
compressedStr = new Base64Encoder().encodeToString(compressed);
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
ZipInputStream zs = new ZipInputStream(new ByteArrayInputStream(buffer));
zs.getNextEntry();
...