Java FTP 550错误

时间:2013-10-27 02:38:55

标签: java ftp

我收到此错误(550文件名,目录名或卷标语法不正确。) 我认为网址是正确的(显然不是)。有什么想法吗?

这是网址:

STOR /images/report/6F81CB22-3D04-4BA3-AC3F-3D34663449E0**9.png

这是调用方法:

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();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果FTP服务器正在运行Windows,则“*”字符是问题所在。 Windows文件名may not have asterisks

答案 1 :(得分:0)

上传前先尝试更改工作目录 ftp.changeWorkingDirectory(DESTPATH);

它对我有用。