简体   繁体   English

如何使用Java上传zip文件?

[英]How to upload a zip file using Java?

I trying to upload a zip file. 我试图上传一个zip文件。 In my project i am using DWR in the client side and Java in server side. 在我的项目中,我在客户端使用DWR,在服务器端使用Java。 As i have seen in DWR tutorials for uploading data(Its not in their website. They are providing it with dwr.rar bundle) they getting input by the below lines. 正如我在DWR教程中看到的上传数据(它不在他们的网站上。他们提供dwr.rar包)他们通过以下行获得输入。

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

dwr.util.getValue() is a utility to get the value of any element, in this case a file object.//Mentioned in the tutorial. dwr.util.getValue()是一个获取任何元素值的实用程序,在本例中是一个文件对象。在教程中提到。

So, i get a zip file using that utility by the below code. 所以,我通过以下代码使用该实用程序获得了一个zip文件。

Javascript: 使用Javascript:

function uploadZip(){
var file = dwr.util.getValue("uploadFile");
dwr.util.setValue("uploadFile", null);
DataUpload.uploadData(file, function(data){
    if(data != null){
        $("#zipURL").html("<p>Upload Completed!!!</p>");
        $("#zipURL").append("Location: "+data.path2);
    }
});
}

HTML: HTML:

<html>
<head>ZIP Uploader
</head>
<body>
<table>
<tr><td>Select File: </td><td><input type="file" id="uploadFile" /></td>
<tr><td><input type="button" value="Upload" onclick="uploadZip()" /></td></tr>    </table>
<div id="result"><span id="imgURL"></span>
<span id="zipURL"></span></div>
</body>
</html>

The Java Code is: 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;

}

This code runs without error. 此代码运行没有错误。 But the output is a Empty zip file. 但输出是一个空的zip文件。 I know i doing something wrong. 我知道我做错了什么。 I unable to find that. 我找不到。

Actually, i am receiving a zip file as InputStream. 实际上,我收到一个zip文件作为InputStream。

How should i have to write a InputStream(a zip file) to a zip.file using java? 我应该如何使用java 编写一个InputStream(一个zip文件)到zip.file?

What will happen if i set the java method parameter as ZipFile file ? 如果我将java方法参数设置为ZipFile file会发生什么? I didnt tried it, yet because, i am still searching a good tutorial to learn about it. 我没有尝试过,但因为,我仍然在寻找一个很好的教程来了解它。

Any Suggestion or Links would be more appreciative!!!!! 任何建议或链接都​​会更加赞赏!!!!! Thanks in Advance!!! 提前致谢!!!

Here you have 2 examples about creating a ZIP file: 这里有2个关于创建ZIP文件的示例:

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

Here is an example about reading a ZIP file: 这是一个关于读取ZIP文件的示例:

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

I have also implemented the Same kind of backend Code in Java, and I was facing the same Issue of Zip file being made, but its content being empty. 我还在Java中实现了相同类型的后端代码,我遇到了相同的Zip文件问题,但其内容为空。

Later I found that the Request I was making to API, in that the file I was Attaching was not in --data-binary format. 后来我发现我正在向API发出请求,因为我正在附加的文件不是--data-binary格式。 So, I then made the request in this Format. 所以,我接着以这种格式提出了请求。

curl --data-binary @"/mnt/c/checknew.zip" http://localhost/api/upload

I am not sure what request format you are making either in multipart/form-data or Base-64 encoded. 我不确定您在multipart/form-dataBase-64编码中使用的请求格式。

My code worked when I made a Base-64 encoded Request (ie --data-binary ) 当我制作Base-64编码请求(即--data-binary )时,我的代码工作--data-binary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM