Skip to content

Instantly share code, notes, and snippets.

@RatanPaul
Forked from kiran-machhewar/Main.java
Created November 23, 2015 06:26
Show Gist options
  • Save RatanPaul/0028eefd5dc408b00c15 to your computer and use it in GitHub Desktop.
Save RatanPaul/0028eefd5dc408b00c15 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
String url = "https://ap1.salesforce.com/services/apexrest/FileDownloadService";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("Authorization", "Bearer <sessionid>");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
con.getInputStream();
InputStream is = con.getInputStream();
OutputStream outstream = new FileOutputStream(new File("myfile.pdf"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
System.out.println("File got created");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment