Created
August 15, 2012 13:10
-
-
Save dwilliamson/3359976 to your computer and use it in GitHub Desktop.
Manifest download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String DownloadRemoteFile(String source, String filename, String dest, boolean log_download) | |
{ | |
// Locate in the user's home directory | |
String remote_filename = source + filename; | |
String local_filename = m_HomeDirectory + "/" + dest + filename; | |
if (log_download) | |
m_Client.Log("Downloading: " + local_filename); | |
// Open a stream to the source file | |
InputStream input_stream; | |
try | |
{ | |
URL resource = new URL(remote_filename); | |
input_stream = resource.openStream(); | |
} | |
catch (Throwable e) | |
{ | |
m_Client.Log(" Failed to open input stream: " + e.getMessage()); | |
return null; | |
} | |
try | |
{ | |
// Create the destination file, ensuring its directories are created | |
File file = new File(local_filename); | |
file.getParentFile().mkdirs(); | |
FileOutputStream output_stream = new FileOutputStream(file); | |
// Copy the file locally | |
byte[] array = new byte[1024*100]; | |
for (int i = input_stream.read(array); i != -1; i = input_stream.read(array)) | |
output_stream.write(array, 0, i); | |
// Close the streams | |
output_stream.close(); | |
input_stream.close(); | |
if (log_download) | |
m_Client.Log(" Done."); | |
return file.getAbsolutePath(); | |
} | |
catch (Throwable e) | |
{ | |
m_Client.Log(" Failed to open file: " + e.getMessage()); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment