Results 1 to 1 of 1
Thread: Java URL Download
- 09-03-2012, 10:45 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 17
- Rep Power
- 0
Java URL Download
Hi!
I currently have 2 very annoying problems when I'm trying to download files through Java.
When I try to download 20 .jpg files from an website (Files with the same name followed by a different number) it only downloads 6-7 files and then stops half-way. Here is my code:
What could the problem be?Java Code:public class FileDownload2 { public static void main(String [] args) { URL url; URLConnection con; DataInputStream dis; FileOutputStream fos; byte[] fileData; String fname = null; for (int n = 1; n < 20; n++){ fname = "picture-" + n + ".jpg"; try { url = new URL("http://randomwebsite.com/content/" + fname); con = url.openConnection(); dis = new DataInputStream(con.getInputStream()); fos = new FileOutputStream(new File("C:\\Users\\UserName\\Desktop\\content\\" + fname)); final byte[] buf = new byte[1024]; fileData = new byte[con.getContentLength()]; for (int num = dis.read(buf); num != -1; num = dis.read(buf)) { fos.write(buf, 0, num); } dis.close(); fos.close(); } catch(MalformedURLException m) { System.out.println(m); } catch(IOException io) { System.out.println(io); } } } }
Also another problem where I try to download only 1 file with the same code (Just edited so it doesn't have the 'for') and the file is 2 mb or more, it stops when downloading. Sometimes when it's only downloaded 100 kb, sometimes when it's downloaded 1 mb. Thank you for your help!
Kris5228
Similar Threads
-
Download java 3d lib
By Dennis in forum Java 2DReplies: 43Last Post: 09-15-2010, 10:05 AM -
java ebooks download
By ramkumar123 in forum Reviews / AdvertisingReplies: 2Last Post: 11-07-2008, 10:20 AM -
how to download using java
By leonard in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 05:36 PM -
Help with download java api
By fernando in forum New To JavaReplies: 1Last Post: 08-06-2007, 02:36 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks