Results 1 to 4 of 4
  1. #1
    rocko is offline Member
    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0

    Post Help needed in File download from password protected FTP server

    I have also tried above code with few changes but I got error. Code and error show below.

    Java Code:
    package practice;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class TestFTPProgram {
    	/**
    	 * @param args
    	 *            the command line arguments
    	 */
    
    	public void download(String ftpServer, String user, String password,
    			String fileName, File destination) throws MalformedURLException,
    			IOException {
    		StringBuffer sb = new StringBuffer("ftp://");
    		// check for authentication else assume its anonymous access.
    
    		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=a");
    		BufferedInputStream bis = null;
    		BufferedOutputStream bos = null;
    		try {
    			URL url = new URL(sb.toString());
    			URLConnection urlc = url.openConnection();
    			System.out.println(url);
    			bis = new BufferedInputStream(urlc.getInputStream());
    			bos = new BufferedOutputStream(new FileOutputStream(
    					destination.getName()));
    
    			int i;
    			while ((i = bis.read()) != -1) {
    				bos.write(i);
    			}
    		} finally {
    			if (bis != null)
    				try {
    					bis.close();
    				} catch (IOException ioe) {
    				}
    			if (bos != null)
    				try {
    					bos.close();
    				} catch (IOException ioe) {
    				}
    		}
    
    	}
    
    	public static void main(String[] args) {
    		// TODO code application logic here
    		TestFTPProgram tftpp = new TestFTPProgram();
    		File testF = new File("C:\\Users\\works\\Desktop\\hre");
    		try {
    			tftpp.download("miyahara_7", "work", "work",
    					"C:\\Users\\work\\Desktop\\outtime", testF);
    		} catch (MalformedURLException ex) {
    			
    			Logger.getLogger(TestFTPProgram.class.getName()).log(Level.SEVERE,
    					null, ex);
    		} catch (IOException ex) {
    			
    			Logger.getLogger(TestFTPProgram.class.getName()).log(Level.SEVERE,
    					null, ex);
    		}
    
    	}
    
    }
    error:
    Java Code:
    Jun 16, 2014 6:21:33 PM practice.TestFTPProgram main
    SEVERE: null
    java.net.ConnectException: Connection refused: connect
    	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at sun.net.ftp.impl.FtpClient.doConnect(Unknown Source)
    	at sun.net.ftp.impl.FtpClient.tryConnect(Unknown Source)
    	at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
    	at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
    	at sun.net.[url]www.protocol.ftp.FtpURLConnection.connect(Unknown[/url] Source)
    	at sun.net.[url]www.protocol.ftp.FtpURLConnection.getInputStream(Unknown[/url] Source)
    	at practice.TestFTPProgram.download(TestFTPProgram.java:43)
    	at practice.TestFTPProgram.main(TestFTPProgram.java:71)

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: Help needed in File download from password protected FTP server.

    Don't hijack someone else's thread, create your own.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Help needed in File download from password protected FTP server

    Moved to own thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: Help needed in File download from password protected FTP server

    A simple case of exception researching 101: copy/paste "java.net.ConnectException: Connection refused: connect" into google and see what you get. It should not be very hard to imagine why based on the error alone.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 01-15-2014, 08:27 AM
  2. Password protected zip files and Java
    By profman in forum New To Java
    Replies: 2
    Last Post: 09-14-2012, 01:28 PM
  3. Replies: 17
    Last Post: 05-22-2012, 02:19 PM
  4. Replies: 0
    Last Post: 09-14-2011, 10:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •