-
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?
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);
}
-
Re: FTP problem
Can you post a small complete program that compiles and executes for testing?
-
1 Attachment(s)
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.
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);
}
}
-
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.
-
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.
-
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) ;
-
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:
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);
}
}