1

I need to upload a zip file to the URL using POST or another option. I followed the below method and successfully I have uploaded the file.

String diskFilePath="/tmp/test.zip;
    String urlStr="https://<ip>/nfc/file.zip";

    HttpsURLConnection conn = (HttpsURLConnection) new URL(urlStr).openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setChunkedStreamingMode(CHUCK_LEN);
    conn.setRequestMethod(put? "PUT" : "POST"); // Use a post method to write the file.
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Length", Long.toString(new File(diskFilePath).length()));

    int i=0;
    while(i<1)
    {
        continue;
    }

    BufferedOutputStream bos = new BufferedOutputStream(conn.getOutputStream());

    BufferedInputStream diskis = new BufferedInputStream(new FileInputStream(diskFilePath));
    int bytesAvailable = diskis.available();
    int bufferSize = Math.min(bytesAvailable, CHUCK_LEN);
    byte[] buffer = new byte[bufferSize];

    long totalBytesWritten = 0;
    while (true) 
    {
        int bytesRead = diskis.read(buffer, 0, bufferSize);
        if (bytesRead == -1) 
        {
            //System.out.println("Total bytes written: " + totalBytesWritten);
            break;
        }

        totalBytesWritten += bytesRead;
        bos.write(buffer, 0, bufferSize);
        bos.flush();
    //  System.out.println("Total bytes written: " + totalBytesWritten);
        int progressPercent = (int) (((bytesAlreadyWritten + totalBytesWritten) * 100) / totalBytes);

    }

Now my zip file is in a remote location and I need to upload the zip file without downloading to the local machine.

I need to pass this url “https:///file/test.zip” instead of “/tmp/test.zip”

For example, I execute the Program on machine A and the file to be uploaded is present in Machine B. Webserver is deployed in the machine B and url is exposed to download the zip file. Now I need to pass this ZIP file URL location to upload instead of the downloading the zip file to machine A and then pass to the upload URL.

Thanks, Kalai

0

1 Answer 1

2

Not 100% what you mean by "without downloading to the local machine."

Here's how to avoid downloading the file into a temporary local file and then uploading that. The basic approach is to read from one URLConnection (instead of the local file) and write to another URLConnection (like you do already). Start by doing the request to sourceString, so

HttpsURLConnection source = (HttpsURLConnection) new URL("https://machine.B/path/to/file.zip").openConnection();

then keep everything until youset the Content-Length and replace that with

conn.setRequestProperty("Content-Length", source.getContentLength());

and then all you have to do is use

InputStream is = source.getInputStream();

instead of your diskis.

PS: I don't get the purpose of the .available logic, why not just use CHUNK_LEN for the buffer size? PPS: also the while(i<0) loop can be removed ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. It worked.. Instead of HttpsURLConnection i used URLConnection. URL url=new URL("machine.B/path/to/file.zip"); URLConnection source=url.openConnection();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.