Skip to content

Instantly share code, notes, and snippets.

@joesavak
Created December 6, 2010 20:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joesavak/730902 to your computer and use it in GitHub Desktop.
Get files over HTTP
for (i = 0; i < _numFiles; i++) {
try {
//get the next file name
fileName = _fileList[i].substring(_fileList[i].lastIndexOf("/")+1);
//associate file (URL) with the httpMethod
HttpMethod getXMLFile = new GetMethod(
_fileList[i]);
//go get the file
_filehttp.executeMethod(getXMLFile );
//header info is useful (modified d/ts & size) -
//i check it in my code to init downstream processing - this just grabs it
Header[] respHeader = getXMLFile.getResponseHeaders();
//gotta have something to write to
InputStream inFileStream = getXMLFile.getResponseBodyAsStream();
//_xmldest is a config-file set directory read upstream to this
File outFile = new File(_xmldest + fileName);
//create file
FileOutputStream outFileStream = new FileOutputStream(outFile);
//set buffer size
byte[] buffer = new byte[1024];
k = 0;
//write to file
while ((k = inFileStream.read(buffer)) > 0 ) {
outFileStream.write(buffer,0,k);
}
//remember to close!
inFileStream.close();
outFileStream.close();
} catch (Exception e) {
e.printStackTrace();
error++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment