I'm trying to get an image from a web page and write to a file, but in the end my photo viewer doesn't recognize the picture file and can't open it (the file is unreadable).
Here is my code:
URL urlpic = new URL("https://static.asset.aparat.com/lp/16107806-6200-m.jpg");//sample pic url HttpURLConnection connectionToPicFile=(HttpURLConnection)urlpic.openConnection(); BufferedReader buffPic=new BufferedReader(new InputStreamReader(connectionToPicFile.getInputStream()));
String pic = "";
String alldatapic = "";
while((pic=buffPic.readLine()) != null)
{
alldatapic += pic;
}
try
{
FileOutputStream fout = new FileOutputStream("D://pic.jpg");//where i want the file to be saved
byte[] b = alldatapic.getBytes();
fout.write(b);
fout.close();
}
catch(Exception ex)
{
System.out.println(ex.toString()+" "+ex.getMessage());
}