ftp://user:password@host:port/filePath;type=i. * * @param ftpServer , FTP server address (optional port ':portNumber').

* @param user , Optional user name to login. * @param password , Optional password for user. * @param fileName , Destination file name on FTP server (with optional * preceding relative path, e.g. "myDir/myFile.txt"). * @param source , Source file to upload. * @throws MalformedURLException, IOException on error. */ public void upload( String ftpServer, String user, String password, String fileName, File source ) throws MalformedURLException, IOException { if (ftpServer != null && fileName != null && source != null) { StringBuffer sb = new StringBuffer( "ftp://" ); // check for authentication else assume its anonymous access. if (user != null && password != null) { sb.append( user ); sb.append( ':' ); sb.append( password ); sb.append( '@' ); } sb.append( ftpServer ); sb.append( '/' ); sb.append( fileName ); /* * type ==> a=ASCII mode, i=image (binary) mode, d= file directory * listing */ sb.append( ";type=i" ); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { URL url = new URL( sb.toString() ); URLConnection urlc = url.openConnection(); bos = new BufferedOutputStream( urlc.getOutputStream() ); bis = new BufferedInputStream( new FileInputStream( source ) ); int i; // read byte by byte until end of stream while ((i = bis.read()) != -1) { bos.write( i ); } } finally { if (bis != null) try { bis.close(); } catch (IOException ioe) {

File destination ) throws MalformedURLException. sb.append( user ).append( '/' ). sb. * @param fileName .type=i" ). if (user != null && password != null) { sb. i=image (binary) mode. Name of file to download (with optional preceeding * relative path. try { URL url = new URL( sb.txt). * @param user .close(). * @param password . sb.printStackTrace().println( "Input not available. one/two/three.out. BufferedOutputStream bos = null. } catch (IOException ioe) { ioe. String user. sb. * * @param ftpServer . * @throws MalformedURLException.append( '@' ).append( password ). String password. Destination file to save. sb. } } } else { System.type=i. A FTP URL is generated with the * following syntax: * ftp://user:password@host:port/filePath. IOException { if (ftpServer != null && fileName != null && destination != null) { StringBuffer sb = new StringBuffer( "ftp://" ). Optional user name to login. BufferedInputStream bis = null.append( ':' ). } } /** * Download a file from a FTP server. */ public void download( String ftpServer.toString() ). } if (bos != null) try { bos. } sb. // check for authentication else assume its anonymous access. * @param destination . /* * type ==> a=ASCII mode.append( ftpServer ).g. Optional password for user. e.ioe. IOException on error. FTP server address (optional port ':portNumber'). .printStackTrace(). d= file directory * listing */ sb.append( fileName ). String fileName.append( "." ).

printStackTrace().URLConnection urlc = url. } catch (IOException ioe) { ioe. } } } .printStackTrace().println( "Input not available" ).read()) != -1) { bos. bis = new BufferedInputStream( urlc. } catch (IOException ioe) { ioe.getName() ) ).write( i ). bos = new BufferedOutputStream( new FileOutputStream( destination. } } finally { if (bis != null) try { bis.out. int i.getInputStream() ). } if (bos != null) try { bos. } } } else { System.close(). while ((i = bis.openConnection().close().