import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
public class FTPUpload{
public static boolean uploadfile(String server,String username,String Password,String source_file_path,String dest_dir){
FTPClient ftp=new FTPClient();
try {
int reply;
ftp.connect(server);
ftp.login(username, Password);
System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
return false;
}
System.out.println("FTP server connected.");
InputStream input= new FileInputStream(source_file_path);
ftp.storeFile(dest_dir, input);
System.out.println( ftp.getReplyString() );
input.close();
ftp.logout();
} catch(Exception e) {
System.out.println("err");
e.printStackTrace();
return false;
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(Exception ioe) {
}
}
}
return true;
}
public static void main(String[] args) {
FTPUpload upload = new FTPUpload();
try {
upload.uploadfile("192.168.0.210","muruganp","vm4snk","/home/media/Desktop/FTP Upload/data.doc","/fileserver/filesbackup/Emac/");
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用上面的代码在服务器位置192.168.0.210上传名为“data.doc”的文件。 我的服务器的目标位置是fileserver / filesbackup / Emac /.
但我最终收到错误“553无法创建文件”,尽管服务器已成功连接。我怀疑我以错误的方式提供目标格式。请让我知道要解决这个问题需要做些什么?
答案 0 :(得分:7)
问题是您尝试将文件上传到目录。您应该指定目标文件名,而不是目标目录。
在另一个FTP客户端中尝试相同时,它是否有效?
<强> [更新] 强>
以下是一些(未经测试,因为我没有FTP服务器)代码,可以更好地以更短的形式进行错误处理。
package so3972768;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
public class FtpUpload {
private static void check(FTPClient ftp, String cmd, boolean succeeded) throws IOException {
if (!succeeded) {
throw new IOException("FTP error: " + ftp.getReplyString());
}
}
private static String today() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
public void uploadfile(String server, String username, String Password, String sourcePath, String destDir) throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect(server);
try {
check(ftp, "login", ftp.login(username, Password));
System.out.println("Connected to " + server + ".");
InputStream input = new FileInputStream(sourcePath);
try {
String destination = destDir;
if (destination.endsWith("/")) {
destination += today() + "-" + new File(sourcePath).getName();
}
check(ftp, "store", ftp.storeFile(destination, input));
System.out.println("Stored " + sourcePath + " to " + destination + ".");
} finally {
input.close();
}
check(ftp, "logout", ftp.logout());
} finally {
ftp.disconnect();
}
}
public static void main(String[] args) throws IOException {
FtpUpload upload = new FtpUpload();
upload.uploadfile("192.168.0.210", "muruganp", "vm4snk", "/home/media/Desktop/FTP Upload/data.doc", "/fileserver/filesbackup/Emac/");
}
}