Handling downloads in Java

孤街醉人 提交于 2019-12-01 17:00:26

问题


How would I be able to handle downloads using HttpResponse in Java? I made an HttpGet request to a specific site - the site returns the file to be downloaded. How can I handle this download? InputStream doesn't seem to be able to handle it (or maybe I'm using it the wrong way.)


回答1:


Assuming you're actually talking about HttpClient, Here's an SSCCE:

package com.stackoverflow.q2633002;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Test {

    public static void main(String... args) throws IOException {
        System.out.println("Connecting...");
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://apache.cyberuse.com/httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin.zip");
        HttpResponse response = client.execute(get);

        InputStream input = null;
        OutputStream output = null;
        byte[] buffer = new byte[1024];

        try {
            System.out.println("Downloading file...");
            input = response.getEntity().getContent();
            output = new FileOutputStream("/tmp/httpcomponents-client-4.0.1-bin.zip");
            for (int length; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
            System.out.println("File successfully downloaded!");
        } finally {
            if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
            if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
        }
    }

}

Works fine here. Your problem lies somewhere else.




回答2:


Open a stream and send the file:

try {
    FileInputStream is = new FileInputStream( _backupDirectory + filename );
    OutputStream os = response.getOutputStream();
    byte[] buffer = new byte[65536];
    int numRead;
    while ( ( numRead = is.read( buffer, 0, buffer.length ) ) != -1 ) {
        os.write( buffer, 0, numRead );
    }
    os.close();
    is.close();
}
    catch (FileNotFoundException fnfe) {
    System.out.println( "File " + filename + " not found" );
}



回答3:


In general when you want the browser to show the download dialog box for a file to be downloaded, you should set the incoming inputstream content directly into the response object steam and set the content type of response (HttpServletResponse object) to the relevant file type.

i.e.,

response.setContentType(.. relevant content type)

Content type can be application/pdf for pdf files as an example.

If browser has a plugin to show relevant file in the browser window, file will open and user can save then, otherwise browser will show the download box.



来源:https://stackoverflow.com/questions/2633002/handling-downloads-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!