Download Single File
Download the file 'ids17d01.snx.Z' from archive directory '/doris/products/sinex_global/ids/' to your local file system.
There are 3 steps to complete before receiving the file:
- Save the code below to a file.
- Compile the code.
- Run the code.
Step 1 is below and Steps 2 and 3 are shown after the code.
Place the code below into a file named CddisHttpsFileDownload.java (or name you select). The lines that begin with "//" are comments and explain what the next line of code does. A comment block begins with "/*" and ends with "*/".
import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
public class CddisHttpsFileDownload {
/* Set location for Earthdata Login */
static String URS = "https://urs.earthdata.nasa.gov";
public static void main(String args[]){
/* Verify command line syntax */
if (args.length < 2){
System.out.println ("Syntax: java FileDownload <resource> <full path
to netrc_file>");
System.exit(0);
}
String resource = args[0].trim();
String username = null;
String password = null;
/* Save the filename from the resource requested as the local file name;
this is one method but it can be done in many ways. */
String localFilename = resource.split("/")[resource.split("/").length-1];
/* Read netrc file and extract username and password; this is one method
but it can be done in many ways. */
try {
BufferedReader br=new BufferedReader(
new FileReader(new File(args[1].trim())));
String line;
while((line=br.readLine())!=null) {
if (line.contains("urs.earthdata.nasa.gov")) {
username = line.split(" ")[3];
password = line.split(" ")[5];
}
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
try {
/* Set up a cookie handle, required */
CookieHandler.setDefault(new CookieManager(null,
CookiePolicy.ACCEPT_ALL));
/* Retrieve a stream for the resource */
InputStream in = getResource(resource, username, password);
/* Read from the URL and write to a local file */
try {
BufferedInputStream inputStream = new BufferedInputStream(in);
FileOutputStream fileOS = new FileOutputStream(localFilename);
byte data[] = new byte[1024];
int byteContent;
while ((byteContent = inputStream.read(data, 0, 1024)) != -1) {
fileOS.write(data, 0, byteContent);
}
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
catch( Exception t) {
System.out.println("ERROR: Failed to retrieve resource");
System.out.println(t.getMessage());
t.printStackTrace();
}
}
/* Return an input stream for a designated resource on a URS-authenticated
remote server.*/
public static InputStream getResource (
String resource, String username, String password) throws Exception {
int redirects = 0;
/* Place an upper limit on the number of redirects we will follow */
while( redirects < 10 ) {
++redirects;
/* Configure a connection to the resource server and submit the
request for our resource. */
URL url = new URL(resource);
HttpsURLConnection connection = null;
if (url.getProtocol().equalsIgnoreCase("https")) {
connection = (HttpsURLConnection) url.openConnection();
}
/* Handle any redirect that goes to http - set it back to an https
request */
else {
connection = (HttpsURLConnection)new URL("https",
url.getAuthority(), url.getFile()).openConnection();
}
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setDoInput(true);
/* If this is the URS server, add in the authentication header. */
if( resource.startsWith(URS) ) {
connection.setDoOutput(true);
connection.setRequestProperty (
"Authorization",
"Basic " + Base64.getEncoder().encodeToString (
(username + ":" + password).getBytes()));
}
/*Execute the request and get the response status code. A return
status code of 200 is good - it means that we have our resource. We
can return the input stream so it can be read (may also want to return
additional header information such as the mime type or size). */
int status = connection.getResponseCode();
if( status == 200 ) {
return connection.getInputStream();
}
/* Any returned status code other than 302 (a redirect) will need
custom handling. A 401 code means that the credentials aren't valid. A
403 code means that the user hasn't authorized the application. */
if( status != 302 ) {
throw new Exception(
"Invalid response from server - status " + status);
}
/* Get the redirection location and continue. This should really have
a null check, just in case. */
resource = connection.getHeaderField("Location");
}
/* If we get to this point, we have exceeded our redirect limit. This is
most likely a configuration problem somewhere on the remote server. */
throw new Exception("Redirection limit exceeded");
}
}
}
Compile the Java code:
> javac CddisHttpsFileDownload.java
Run the code
> java CddisHttpsFileDownload https://cddis.nasa.gov/archive/doris/products/sinex_global/ids/ids17d01.snx.Z