I'm getting this error (550 the filename, directory name, or volume label syntax is incorrect. )
I think the url is correct (obviously not though). Any thoughts?
Here is the url:
STOR /images/report/6F81CB22-3D04-4BA3-AC3F-3D34663449E0**9.png
Here is the invocation method:
private void uploadImageToFtp(String location, String imageName) throws Exception{
File imageFile = new File(location);
System.out.println("Start");
FTPUploader ftpUploader = new FTPUploader("ftp.xxx.com", "user", "password");
ftpUploader.uploadFile(imageFile, imageName, "/images/report/");
imageFile.delete();
ftpUploader.disconnect();
System.out.println("Done");
}
Here is the
ftp class:
package server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPUploader {
FTPClient ftp = null;
public FTPUploader(String host, String user, String pwd) throws Exception{
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
public void uploadFile(File file, String fileName, String hostDir)
throws Exception {
try {
InputStream input = new FileInputStream(file);
this.ftp.storeFile(hostDir + fileName, input);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void disconnect(){
if (this.ftp.isConnected()) {
try {
this.ftp.logout();
this.ftp.disconnect();
} catch (IOException f) {
// do nothing as file is already saved to server
f.printStackTrace();
}
}
}
}
If the FTP server is running Windows, the '*' characters are the problem. Windows file names may not have asterisks.
Try changing working directory first before uploading
ftp.changeWorkingDirectory(DESTPATH);
It worked for me.
Related
i am developing an utility which will upload files to ftp server. The code is running right. But now i want to save file name to sql database at server side. Please help me with this. below is the code for file upload. It is running right.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPUploader {
FTPClient ftp = null;
public FTPUploader(String host, String user, String pwd) throws Exception{
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
public void uploadFile(String localFileFullName, String fileName, String hostDir)
throws Exception {
try(InputStream input = new FileInputStream(new File(localFileFullName))){
String ss=hostDir+"/swaps";
this.ftp.makeDirectory(ss);
boolean changeWorkingDirectory = ftp.changeWorkingDirectory(ss);
//this.ftp.storeFile(ss + fileName, input);
if(changeWorkingDirectory)
this.ftp.storeFile(fileName, input);
}
}
public void disconnect(){
if (this.ftp.isConnected()) {
try {
this.ftp.logout();
this.ftp.disconnect();
} catch (IOException f) {
// do nothing as file is already saved to server
}
}
}
public static void main(String[] args) throws Exception {
System.out.println("Start");
FTPUploader ftpUploader = new FTPUploader("www.web.com", "swaswa", "swaswa");
//FTP server path is relative. So if FTP account HOME directory is "/home/pankaj/public_html/" and you need to upload
// files to "/home/pankaj/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"
ftpUploader.uploadFile("D:\\o.jpg", "qq.jpg", "");
///netsh advfirewall set global StatefulFTP disable
ftpUploader.disconnect();
System.out.println("Done");
}
}
Hello Every one i am working one project where i need to upload file on my ftp server with my java standalone application
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class Ftpdemo {
public static void main(String args[]) {
// get an ftpClient object
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(300);
FileInputStream inputStream = null;
try {
// pass directory path on server to connect
ftpClient.connect("ftp.mydomain.in");
// pass username and password, returned true if authentication is
// successful
boolean login = ftpClient.login("myusername", "mypassword");
if (login) {
System.out.println("Connection established...");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
inputStream = new FileInputStream("/home/simmant/Desktop/mypic.png");
boolean uploaded = ftpClient.storeFile("user_screens/test3.png",inputStream);
if (uploaded) {
System.out.println("File uploaded successfully !");
} else {
System.out.println("Error in uploading file !");
}
// logout the user, returned true if logout successfully
boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} else {
System.out.println("Connection fail...");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
its working fine for the data which is 1 or 2 kb but when i try to upload the file which is of 50 kb and 100 kb then it not working fine. The image uploaded on the server is blank .
As far as I can see, there is nothing wrong with the code. As you're saying 1 or 2 kb of data uploading works fine. So, In my opinion, there is problem with your internet. Might be it's too slow to upload a file size of 50 kb or more.
There is noting wrong with your code only issue with the file or internet speed.Code is working fine here and Also there is recommended that NOT TO USE FTP DETAILS DIRECTLY please avoid this stuff from application you have better option to use web service for your server related stuff.
Good Luck
It seems you had not made connection Instances properly.
Please refer my working code:
public class FTPUploader {
FTPClient ftp = null;
public FTPUploader() throws Exception {
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(new FileOutputStream("c:\\ftp.log"))));
int reply;
ftp.connect(Constant.FTP_HOST);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception(Constant.FTP_SERVER_EXCEPTION);
}
ftp.login(CommonConstant.FTP_USERNAME, Constant.FTP_PASSWORD);
ftp.setFileType(FTP.TELNET_TEXT_FORMAT);
ftp.enterLocalPassiveMode();
}
public boolean uploadFile(File localFileFullName, String fileName) throws Exception {
InputStream input = new FileInputStream(localFileFullName);
boolean reply = this.ftp.storeFile("'" + fileName +"'", input);
disconnect();
return reply;
}
public void disconnect() {
if (this.ftp.isConnected()) {
try {
this.ftp.logout();
this.ftp.disconnect();
} catch (IOException e) {
System.out.println(this.getClass()+ e);
}
}
}
public static void main(String[] args) throws Exception {
System.out.println("Start");
FTPUploader ftpUploader = new FTPUploader();
System.out.println(ftpUploader.uploadFile(new File("C:\\test.txt"), "Destinate_DIR"));
System.out.println("Done");
}
}
All the best ...!!!
I am trying to retrieve a file from ftp server but I get error as below. Would you please help me
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpTest_V1 {
public static void main(String[] args) {
String server = "192.168.200.8";
int port = 21;
String user = "Test_user";
String pass = "123456**";
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
showServerReply(ftpClient);
ftpClient.enterLocalPassiveMode();
int replyCode = ftpClient.getReplyCode();
System.out.println(replyCode);
//isPositiveCompletion
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println(FTPReply.isPositiveCompletion(replyCode));
System.out.println(FTPReply.isNegativeTransient(replyCode));
System.out.println("Connect failed");
return;
}
boolean success = ftpClient.login(user, pass);
//System.out.println(success);
showServerReply(ftpClient);
if (!success) {
System.out.println("Could not login to the server");
return;
}
// Lists files and directories
FTPFile[] files1 = ftpClient.listFiles("TEST2");
//printFileDetails(files1);
// uses simpler methods
String[] files2 = ftpClient.listNames("TEST2");
printNames(files2);
String[] files = files2;
for (String aFile: files) {
String filename = aFile;
fos = new FileOutputStream(filename);
// Download file from FTP server
ftpClient.retrieveFile("C://test//FTP_TEST//GET" + filename, >fos);
}
}
catch (IOException ex) {
System.out.println("An Error Occured"+ex.getMessage());
System.out.println("Warning! Something wrong happened");
ex.printStackTrace();
} finally {
// logs out and disconnects from server
try {
if (fos != null) {
fos.close();
}
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static void printNames(String files[]) {
if (files != null && files.length > 0) {
int i =0;
for (String aFile: files) {
i++;
}
System.out.println("Number of files = "+i);
for (String aFile: files) {
System.out.println(aFile);
}
}
}
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER : " + aReply);
}
}
}
}
Here is my Output :
SERVER : 220-FileZilla Server version 0.9.43 beta
SERVER : 220 Hello FTP SERVER
220
SERVER : 230 Logged on
number of files = 2
TEST2/testfile.csv
TEST2/tttt.csv
An Error OccuredTEST2\testfile.csv (The system cannot find the path specified)
Warning! Something wrong happened
java.io.FileNotFoundException: TEST2\testfile.csv (The system cannot find the path
specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at FtpTest_V1.main(FtpTest_V1.java:74)
The slash should be backwards like this: "C:\ \test\ \FTP_TEST\ \GET" (without the middle space, I put it that way because the html parser in this page change it to only one slash)
intead of:
ftpClient.retrieveFile("C://test//FTP_TEST//GET" + filename, >fos);
Or even better:
"C:" + File.separatorChar + "test" + File.separatorChar + "FTP_TEST" + File.separatorChar + "GET"
Hello I have solved the problem as changing the code. try catch block is as below
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String[] files = ftpClient.listNames("TEST_FILE");
for (String aFile: files) {
String remoteFile1 = aFile;
File downloadFile1 = new File("C:/test/FTP_TEST
/GET/"+remoteFile1.replace("TEST_FILE", ""));
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
}
Hi I'm trying to get my program to download a text document from my FTP server.
This is the code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
public class NetTestFTP {
public static void main(String[] args) {
FTPClient client = new FTPClient();
OutputStream outStream;
try {
client.connect("82.44.221.198");
client.login("User", "Pass");
String remoteFile = "/hello.txt";
outStream = new FileOutputStream("hello.txt");
client.retrieveFile(remoteFile, outStream);
} catch (IOException ioe) {
System.out.println("Error communicating with FTP server.");
} finally {
try {
client.disconnect();
} catch (IOException e) {
System.out.println("Problem disconnecting from FTP server");
}
}
}
}
I need to send an email using SSL (SMTPS) and authentification. In apache Commons Net however there seems to be either AuthenticatingSMTPClient (no SSL, though it extends SMTPSClient?) or SMTPSClient (no authentication?), I need a combination of both (SSL + authentication). Anyone knows how I can do this? Thanks!
I know it is too late to reply to this but for future reference for others, AuthenticatingSMTPClient does provide ssl + authentication as it is extending SMTPSClient. Below is the sample code, one has to do modifications where I have commented with numerals (e.g. //1).
Code:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;
public final class SMTPMail
{
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException
{
String sender, recipient, subject, filename, server;
List<String> ccList = new ArrayList<String>();
FileReader fileReader = null;
Writer writer;
SimpleSMTPHeader header;
AuthenticatingSMTPClient client;
server = "<smtp server>"; // 1
try
{
sender = "<your user name>"; // 2
recipient = "<recipient>"; // 3
subject = "<mail subject>"; // 4
header = new SimpleSMTPHeader(sender, recipient, subject);
filename = "hello.txt"; //This will be the body of your mail //5
try
{
fileReader = new FileReader(filename);
}
catch (FileNotFoundException e)
{
System.err.println("File not found. " + e.getMessage());
}
client = new AuthenticatingSMTPClient("TLS", false);
client.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out), true));
client.connect(server);
if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
{
client.disconnect();
System.err.println("SMTP server refused connection.");
System.exit(1);
}
client.login("hostname.testing.smtp.api"); //6
if(client.execTLS())
{
if(client.auth(AUTH_METHOD.LOGIN, "<your user name>", "<your password>")) //7
{
client.setSender(sender);
client.addRecipient(recipient);
for (String recpt : ccList) {
client.addRecipient(recpt);
}
writer = client.sendMessageData();
if (writer != null)
{
writer.write(header.toString());
Util.copyReader(fileReader, writer);
writer.close();
client.completePendingCommand();
}
if (fileReader != null ) {
fileReader.close();
}
}
}
client.logout();
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
}