How to download a zip file using Java?

I am trying to download a zip file. In my project, I use client side DWR and server side Java. As I saw in the DWR tutorials for downloading data (this is not on their website, they provide it with the dwr.rar package), which they get as a result of the following 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 for getting the value of any element, in this case a file object. // It is specified in the tutorial.

So, I get the zip file using this utility using the code below.

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

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 works without errors. But the output is an empty mail file. I know that I am doing something wrong. I can’t find it.

In fact, I get the zip file as an InputStream.

How do I need to write an InputStream (zip file) to zip.file using java?

, java ZipFile file? , , - .

!!!!! Advance!!!

+3

Source: https://habr.com/ru/post/1774379/


All Articles