3

我试图上传一个 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 文件作为 InputStream。

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

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

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

4

2 回答 2

2

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

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

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

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

于 2010-11-12T13:30:29.623 回答
0

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

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

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

我不确定您使用multipart/form-dataBase-64编码的请求格式。

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

于 2019-02-15T14:12:56.730 回答