如何使用Java上传zip文件?

我试图上传一个zip文件。 在我的项目中,我在客户端使用DWR,在服务器端使用Java。 正如我在DWR教程中看到的上传数据(它不在他们的网站上。他们提供dwr.rar包)他们通过以下行获得输入。

var image = dwr.util.getValue('uploadImage'); var file = dwr.util.getValue('uploadFile'); var color = dwr.util.getValue('color'); 

dwr.util.getValue()是一个获取任何元素值的实用程序,在本例中是一个文件对象。在教程中提到。

所以,我通过下面的代码使用该实用程序获得一个zip文件。

使用Javascript:

 function uploadZip(){ var file = dwr.util.getValue("uploadFile"); dwr.util.setValue("uploadFile", null); DataUpload.uploadData(file, function(data){ if(data != null){ $("#zipURL").html("

Upload Completed!!!

"); $("#zipURL").append("Location: "+data.path2); } }); }

HTML:

  ZIP Uploader   
Select File:

Java代码是:

 public class DataUpload { private static String DATA_STORE_LOC = "D:/BeenodData/Trials/"; public Path uploadData(InputStream file) throws IOException{//In the tutorial the //parameters are in type of BufferedImage & String. //They used it for image and text file respectively. //In an another example(out of DWR site) they used InputStream for receiving //image try { byte[] buffer = new byte[1024]; int c; File f2 = new File(DATA_STORE_LOC+dat+".zip"); path.setPath2(DATA_STORE_LOC+dat+".zip"); FileOutputStream fos = new FileOutputStream(f2); c = file.read(); System.out.println(c); while ((c = file.read()) != -1) { fos.write(c); } file.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return path; } 

此代码运行没有错误。 但输出是一个空的zip文件。 我知道我做错了什么。 我找不到。

实际上,我收到一个zip文件作为InputStream。

我应该如何使用java 编写一个InputStream(zip文件)到zip.file?

如果我将java方法参数设置为ZipFile file会发生什么? 我没有尝试过,但因为,我仍然在寻找一个很好的教程来了解它。

任何建议或链接将更加赞赏!!!!! 提前致谢!!!

这里有2个关于创建ZIP文件的示例:

http://www.java2s.com/Tutorial/Java/0180_ 文件/ 0601 _ZipOutputStream.htm

这是一个关于读取ZIP文件的示例:

http://www.kodejava.org/examples/334.html