最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to upload a zip file using Java? - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question asked Nov 11, 2010 at 19:15 user405398user405398 2
  • can you please explain to me why i still the fakepath even after using dwr.util.getValue("file"). – Genjuro Commented Jul 18, 2012 at 14:09
  • 1 Sorry, i am not able to understand you? What is fakepath means in terms of DWR. – user405398 Commented Jul 23, 2012 at 14:22
Add a ment  | 

2 Answers 2

Reset to default 1

Here you have 2 examples about creating a ZIP file:

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

Here is an example about reading a ZIP file:

http://www.kodejava/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.

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)

发布评论

评论列表(0)

  1. 暂无评论