Results 1 to 18 of 18
- 05-20-2012, 01:25 PM #1
Help needed in File download from password protected FTP server.
I tried in two ways to download a file from a FTP server. Both did not work, please help.
FIRST
I tried this...
URL url = new URL("ftp://myuser:mypassword@myhost:myport/myfilePath;type=i");
ERROR i got was "malformed URL".
The problem i think was in "myuser"..
myuser="updator@myexample.in"
Thus there are two '@' character in the URL and so it might have been the error, i figured..
Because of this error i tried another method.
SECOND
This one was using authenticator. (Am not sure how to use Authenticator class, but i read some codes from some site and then coded the following code)
ERROR it showed was invalid username and password.. (But username and password are correct, i can login to the server through browser with the same username and password)
The following was my code using Authenticator class.
Java Code:package testftp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.net.URL; /** * * @author Kuttappu */ public class TestFTP { public static void main(String args[]) throws MalformedURLException, IOException { String urlString = "ftp://myexample.in"; String username = "updator@myexample.in"; //This full string is the username. String password = "mypassword"; Authenticator.setDefault(new MyAuthenticator(username, password)); URL url = new URL(urlString); InputStream content = (InputStream) url.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(content)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } System.out.println("Done."); } static class MyAuthenticator extends Authenticator { private String username, password; public MyAuthenticator(String user, String pass) { username = user; password = pass; } @Override protected PasswordAuthentication getPasswordAuthentication() { System.out.println("Requesting Host : " + getRequestingHost()); System.out.println("Requesting Port : " + getRequestingPort()); System.out.println("Requesting Prompt : " + getRequestingPrompt()); System.out.println("Requesting Protocol: " + getRequestingProtocol()); System.out.println("Requesting Scheme : " + getRequestingScheme()); System.out.println("Requesting Site : " + getRequestingSite()); return new PasswordAuthentication(username, password.toCharArray()); } } }
Can anyone help me? File to be downloaded is in the server myexample.inLast edited by Fubarable; 05-20-2012 at 03:56 PM. Reason: code tags added
- 05-20-2012, 03:51 PM #2
Re: Help needed in File download from password protected FTP server.
It looks like you need to study how to form a valid URL. Find the doc that defines URLs and try to make a valid URL.
If you don't understand my response, don't ignore it, ask a question.
-
Re: Help needed in File download from password protected FTP server.
Moderator edit: color tags removed around code block and replaced with [code] [/code] tags.
- 05-20-2012, 04:01 PM #4
Re: Help needed in File download from password protected FTP server.
@Norm
Isn't that a valid URL?
I found same format in a couple of sites..
Do you have any good links about valid URLs that you can provide me with?
@Fuberable
Thanks, i didn't know about CODE tag.
- 05-20-2012, 04:05 PM #5
Re: Help needed in File download from password protected FTP server.
If you get the error: "malformed URL" the java program does not think it is a valid URL.Isn't that a valid URL?
Write a small test program with a URL, execute it and post the full text of the error message and the program code.If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 08:28 AM #6
Re: Help needed in File download from password protected FTP server.
This is the full ERROR message
May 21, 2012 11:55:01 AM testftpprogram.TestFTPProgram main
SEVERE: null
java.net.MalformedURLException: For input string: "A3Flamez@a3flamez.in"
at java.net.URL.<init>(URL.java:617)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at testftpprogram.TestFTPProgram.download(TestFTPProg ram.java:48)
at testftpprogram.TestFTPProgram.main(TestFTPProgram. java:88)
Caused by: java.lang.NumberFormatException: For input string: "A3Flamez@a3flamez.in"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at java.net.URLStreamHandler.parseURL(URLStreamHandle r.java:217)
at java.net.URL.<init>(URL.java:612)
... 4 more
BUILD SUCCESSFUL (total time: 5 seconds)
This was my code.
Java Code:package testftpprogram; /** * * @author Kuttappu */ import java.io.*; 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=i" ); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { URL url = new URL( sb.toString() ); URLConnection urlc = url.openConnection(); 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("D:\\testFile.ini"); try { tftpp.download("a3flamez.in", "updator@a3flamez.in", "A3Flamez", "vn.ini", 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); } } }
- 05-21-2012, 10:15 AM #7
- 05-21-2012, 12:39 PM #8
Re: Help needed in File download from password protected FTP server.
Print out the fulll URL you are using at line 44. println("sb=" + sb);
Last edited by Norm; 05-21-2012 at 12:44 PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 03:20 PM #9
Re: Help needed in File download from password protected FTP server.
A URL String has to start with a protocol. Yours doesn't.
Protocol FTP is at line 24 na? starts with ftp://Last edited by Kuttappu; 05-21-2012 at 03:23 PM.
- 05-21-2012, 03:21 PM #10
Re: Help needed in File download from password protected FTP server.
Last edited by Kuttappu; 05-21-2012 at 03:27 PM.
- 05-21-2012, 03:22 PM #11
Re: Help needed in File download from password protected FTP server.
Did you print out the full text of the String being passed to the URL constructor?
Please post it so everyone can see where the problem is that the constructor is finding.If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 03:24 PM #12
Re: Help needed in File download from password protected FTP server.
Try using Sockets and do the connection manually.
Have you tried encoding the username so it does not have the @Last edited by Norm; 05-21-2012 at 03:27 PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 07:00 PM #13
- 05-21-2012, 07:02 PM #14
- 05-21-2012, 07:08 PM #15
Re: Help needed in File download from password protected FTP server.
Look at the URLEncoder class.
If you don't understand my response, don't ignore it, ask a question.
- 05-22-2012, 01:07 PM #16
Re: Help needed in File download from password protected FTP server.
Thanks a million Norm..
.gif)
I encoded username and passed it into my download function. I downloaded the file, yay!!! :D
tftpp.download("www.a3flamez.in", URLEncoder.encode("updator@a3flamez.in","ISO-8859-1"), "A3Flamez", "vn.ini", testF);
- 05-22-2012, 01:10 PM #17
Re: Help needed in File download from password protected FTP server.
Glad you got it to work. Now we all know.
If you don't understand my response, don't ignore it, ask a question.
- 05-22-2012, 01:19 PM #18
Similar Threads
-
connect to password protected access database
By Yama12 in forum JDBCReplies: 0Last Post: 09-14-2011, 09:44 AM -
Indexing Password-protected word files?
By pleonastic in forum LuceneReplies: 0Last Post: 02-17-2011, 04:43 PM -
Calling a .Net webservice(Password Protected) from java console application
By charan reddy in forum Advanced JavaReplies: 2Last Post: 03-23-2010, 04:46 AM -
how to download file from ftp server
By santhosh_el in forum NetworkingReplies: 0Last Post: 11-19-2009, 06:01 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks