How to read binary content from URL and save to a binary file
I have a scenario where my program would be receiving a URL string which points to binary data - could be a *.jpg, *.doc, *.pdf, etc. file. I have to read this URL's content and save it to a file on the hard disk.
I have tried using BufferedReader on the URLConnection.getContent() but this way, I am only able to get TEXT content. If I execute that code on binary content, the files created are corrupted.
below is the code:
// *********************************************************************************************
// Start of URL Read and file Write code
// *********************************************************************************************
//URLCon.setRequestMethod("POST");
URLCon.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(URLCon.getInputStream()));
line_list = new ArrayList();
line="";
while((line = in.readLine()) != null) {
line_list.add(line);
}// Save the Content we just read to a file on the server using a FileWriter
strCompletePath = strAttachmentsPath + "/" + strFileName.trim();
BufferedWriter writer = new BufferedWriter(new FileWriter(strCompletePath));
for(int m=0; m<line_list.size(); m++) {
writer.write((String)line_list.get(m));
// Temporarily changed the condition for adding of newLine() below to test JPG files
if(m><line_list.size()-1){
writer.newLine();
}
} // End of for{}... writing to a file using FileWriter
writer.close();
in.close();
// *********************************************************************************************
// End of URL Read and File Write code
// *********************************************************************************************
Any help would be greatly appreciated>

