如何使用 Java 上传 zip 文件?

发布于 2024-10-02 03:32:24 字数 2558 浏览 0 评论 0原文

我正在尝试上传 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("<p>Upload Completed!!!</p>");
        $("#zipURL").append("Location: "+data.path2);
    }
});
}

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>

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 文件 输入流。

我应该如何 输入流(zip 文件)到 zip.file 使用java?

如果我设置java会发生什么 方法参数作为 ZipFile 文件?我 没试过,但因为,我是 仍在寻找一个好的教程 了解一下。

任何建议或链接将更加感激!!!! 提前致谢!!!

I trying to upload a zip file. In my project i am using DWR in the client side and Java in server side. 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.

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.

So, i get a zip file using that utility by the below code.

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>
<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:

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. I know i doing something wrong. I unable to find that.

Actually, i am receiving a zip file as
InputStream.

How should i have to write a
InputStream(a zip file) to a zip.file
using java?

What will happen if i set the java
method parameter as 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!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2024-10-09 03:32:24

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

http://www.java2s。 com/Tutorial/Java/0180_File/0601_ZipOutputStream.htm

以下是读取 ZIP 文件的示例:

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

Here you have 2 examples about creating a ZIP file:

http://www.java2s.com/Tutorial/Java/0180_File/0601_ZipOutputStream.htm

Here is an example about reading a ZIP file:

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

水晶透心 2024-10-09 03:32:24

我还在 Java 中实现了相同类型的后端代码,并且我面临着制作 Zip 文件的相同问题,但其内容为空。

后来我发现我向 API 发出的请求,因为我附加的文件不是 --data-binary 格式。所以,我随后以这种格式提出了请求。

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

我不确定您在 multipart/form-dataBase-64 编码中发出什么请求格式。

当我发出 Base-64 编码请求(即 --data-binary)时,我的代码有效

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.

Later I found that the Request I was making to API, in that the file I was Attaching was not in --data-binary format. 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.

My code worked when I made a Base-64 encoded Request (i.e --data-binary)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文