Results 1 to 7 of 7
Thread: FTP problem
- 03-08-2012, 11:57 AM #1
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
FTP problem
Hello,
This is the code that I am using to upload a file called "test" into a FTP. It's a txt file.
The problem is that it uploads empty files with the same name of "test", and not the file I want.
Can anyone tell me what I am doing wrong? Or suggest a better method?
Java Code:URL url; try { url = new URL("ftp://user:pass@whatever.com/test.txt;type=i"); URLConnection urlc = url.openConnection(); OutputStream os = urlc.getOutputStream(); // To upload }catch (MalformedURLException ex) { System.out.println("URL Exception: " + ex); }catch (IOException ex) { System.out.println("URL IOException: " + ex); }
- 03-08-2012, 01:26 PM #2
Re: FTP problem
Can you post a small complete program that compiles and executes for testing?
- 03-08-2012, 02:44 PM #3
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
Re: FTP problem
There is one button in the program. You can find the programming of that button below.
I have added a .zip file in attachment that contains the program itself.
Needless to say I am not allowed to give you the real account and its username and password, so you'll have to fill that in for another FTP.
The problem seems to be that the program creates an empty file that doesn't really exist and pushes that to the FTP. The actual file that I want to send is in the main folder of the programs folder.
Thx.
Java Code:private void Button_SendActionPerformed(java.awt.event.ActionEvent evt) { String user="...",pass="...",host="whatever.com"; URL url; try { url = new URL("ftp://"+user+":"+pass+"@"+host+"/"+"test.txt;type=i"); URLConnection urlc = url.openConnection(); //InputStream is = urlc.getInputStream(); // To download OutputStream os = urlc.getOutputStream(); // To upload }catch (MalformedURLException ex) { System.out.println("URL Exception: " + ex); }catch (IOException ex) { System.out.println("URL IOException: " + ex); } }
- 03-08-2012, 02:47 PM #4
Re: FTP problem
There is no need for any GUI etc for testing. Can you write a small, simple program that compiles, executes and shows the problem.
- 03-08-2012, 02:51 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: FTP problem
That URL is pointing to a file on the FTP server, so how do you expect to upload a file like that?
You probably ought to look at a package to do the bulk of this for you.
Apache has an FTP client I believe.Please do not ask for code as refusal often offends.
- 03-08-2012, 03:16 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
Re: FTP problem
With OutputStream os = urlc.getOutputStream(); you will get the Outputstream;
Now you need to read the file from local dir. and write it to that Outputstream.
os.write(byte[] b, int off, int len) ;
- 03-09-2012, 11:55 AM #7
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
Re: FTP problem
Thx all for the tips.
I managed to write a program that does the trick just fine(see below).
Small disadvantage is that you can have communication problems due to your firewall.
For anyone who's wondering how I did it:
Java Code:String host="whatever.com"; String user="user"; String pass="pass"; String filename="text.txt"; FTPClient client = new FTPClient(); FileInputStream fis = null; try { client.connect(host); client.login(user,pass); fis = new FileInputStream(filename); client.storeFile(filename, fis); client.logout(); } catch (IOException ex) { System.out.println("Exception FTP: " + ex); }finally { try { if (fis != null) { fis.close(); } client.disconnect(); } catch (IOException e) { System.out.println("Exception FTP_end: " + e); } }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks