Skip to content

Instantly share code, notes, and snippets.

@alessandro-aglietti
Created March 8, 2015 12:32
Show Gist options
  • Save alessandro-aglietti/ae1cd5703a56f1bc21b2 to your computer and use it in GitHub Desktop.
Save alessandro-aglietti/ae1cd5703a56f1bc21b2 to your computer and use it in GitHub Desktop.
package so;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://stackoverflow.com/questions/28926139/java-save-inputstream-to-file-takes-hours");
CloseableHttpResponse response;
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
FileOutputStream fos = new FileOutputStream(new File("out/28926139.html"));
InputStream is = response.getEntity().getContent();
IOUtils.copy(is, fos);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(fos);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment