I am trying to download pdf from a url using java. I am using the following code for the same. I am able to download the pdf.
But the problem is that I am not able to read the pdf using the adobe reader because the content type of the url is application/pdf ; charset= utf-8.
Although for all the urls whose content type is not the same as above, I am able to download as well as read the pdf.
public class PdfDownloading
{
public static void DownloadPDFFromURL(String link)
{
try
{
URL url = new URL(link);
InputStream in = url.openStream();
FileOutputStream fos = new FileOutputStream(new File("E:\\KOLDump\\charles_hicks\\pdfs\\"+"hello"+".pdf"));
int length = -1;
byte[] buffer = new byte[50000];// buffer for portion of data from connection
while ((length = in.read(buffer)) > -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{
DownloadPDFFromURL("http://www.amjmed.com/article/S0002-9343(06)00676-0/pdf");
}
}
The url from which I am not able to download the pdf is given in the code itself.