05. april 2006 - 13:27
Der er
2 kommentarer og 1 løsning
Download af fil fra hjemmeside
Hvis man kender navnet på en fil, og også har stien til den, hvordan downloader man så nemmeste filen med java?
Annonceindlæg fra Computerworld
05. april 2006 - 14:10
#1
var egentlig rimelig simpelt System.out.println("Forsøger at downloade: "+ file); byte buf[] = new byte[100000]; OutputStream fileOStream = null; InputStream fileIStream = null; try { fileIStream = new URL( new String( homepage + file ) ).openStream(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } try { fileOStream = new FileOutputStream( basedir + file ); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } boolean finish = false; int TryOut = 0; while (!finish) { int bytesRead = 0; try { bytesRead = fileIStream.read( buf ); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } if ( bytesRead > 0 ) { try { fileOStream.write(buf, 0, bytesRead ); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } else { try { TryOut++; if(TryOut > 4) { finish = true; } Thread.currentThread().sleep( 100 ); } catch ( InterruptedException e ) { System.out.println("Could not sleep"); } } } System.out.println("Denne file er downloaded: "+ file); try { fileOStream.close(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }