【问题标题】:Image uploaded in FTP was 0k with FTPClient使用 FTPClient 上传到 FTP 的图像为 0k
【发布时间】:2018-01-24 09:37:39
【问题描述】:

我在将图像从我的 android 手机上传到 FTP 区域时遇到问题,FTP 工作正常,我尝试了其他一些交互,我没有任何问题。 上传的文件权重0k,名称和文件扩展名正确,没有错误或异常。我做了一个测试,将文件复制到另一个文件夹中并正确复制了文件。

private class FTPUploader extends AsyncTask<URL, Integer, Long> {


    protected Long doInBackground(URL... urls) {
        String data = "/pathimage/filename.jpg";
        File sourceFile = new File(data);
        FTPClient con = new FTPClient();
        try {

            con.connect("ftp.ftpServer.it", 21);

            if (con.login("user", "password")) {
                con.setFileType(FTP.BINARY_FILE_TYPE);
                con.enterLocalPassiveMode();


                if (sourceFile.exists()) {
                    InputStream input = new FileInputStream(sourceFile);


                    boolean result = con.storeFile("/folder/newfilename.jpg", input);
                    input.close();
                    if (result) {
                        System.out.println(result);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (sourceFile.exists()) {
                con.logout();
                con.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }    
}

我错了什么?上传完成后可以管理事件吗?提前感谢您的支持

【问题讨论】:

  • “我尝试了其他一些交互,我没有任何问题” - 还有什么其他交互?您是否使用任何独立的 FTP 客户端成功地将相同的文件上传到 FTP 服务器上的相同文件夹? + 你有服务器端日志吗?
  • 我试图下载一个文件,在 FTP 上读取一个 txt 文件,我没有任何问题,但如果我尝试上传,我只找到带有名称和扩展名的文件,但无效。是的,我可以从 filezilla 上传文件,不,对不起,但我没有服务器端日志,但我可以尝试找到它。今天早上我找到了 ftp4j 的解决方法,我尽快上传了解决方案。

标签: java android file-upload android-asynctask ftp


【解决方案1】:

最后,我找到了适合我的解决方法。我使用库 ftp4j 来管理上传事件,并使用 System.out.println 我可以按照步骤操作并确保正确上传。 我发布以遵循代码。首先需要在libs文件夹中添加库,并在build.gradle(Module:app)中导入

compile files('libs/ftp4j-1.7.2.jar')

接下来我在工作表中导入我需要的内容:

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

并更改 ftp4j 的代码

private class FTPUploader extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... params) {

        FTPClient con = new FTPClient();
        try {
            con.setPassive(true);
            con.setType(FTPClient.TYPE_BINARY);
            con.connect("ftp.server.it");
            con.login("user", "password");
            con.changeDirectory("changeFTPUploadDirectory");
            con.upload(new java.io.File("/path/local/image"), new MyTransferListener());


        } catch (Exception e) {

            e.printStackTrace();
        }

        try {
                con.disconnect(true);
        } catch (IOException | FTPException | FTPIllegalReplyException e) {
            e.printStackTrace();
        }

        return null;
    }


}

但我发现非常有用的是事件管理,如果出现问题,您可以检查上传停止的位置和原因。

public class MyTransferListener implements FTPDataTransferListener {

    public void started() {
        System.out.println("Trasferimento FTP avviato");

    }

    public void transferred(int length) {
        System.out.println("in trasferimento: "+ String.valueOf(length));

    }

    public void completed() {
        System.out.println("Completato");

    }

    public void aborted() {
        System.out.println("Annullato");

    }

    public void failed() {
        System.out.println("Trasferimento fallito");

    }

}

正如文章顶部所述,这不是解决方案,而是理解错误的解决方法。编辑后,具有此更改的代码可以正常工作。无论如何,我都会搜索服务器端日志以获取有关我的问题的更多详细信息。提前谢谢你

文档 -> ftp4j

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 2014-12-07
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多