FTPClient 在 Android 上上传到 ftp 服务器时损坏图像?

发布于 2024-11-10 18:06:43 字数 1144 浏览 3 评论 0原文

我正在尝试从 Android 手机 (HTC Desire HD) 将图像上传到 FTP 服务器(在我的本地 PC 上)。图像将发送到 FTP 服务器,但它们已损坏。

方法 (ftpClient.storeFile()) 抛出 IOException(错误文件号)

请帮助我。

这是损坏的图像链接:

http://imageshack.us/photo/my -images/820/komikb.jpg/

这是代码:

FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect("192.168.2.14");
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        ftpClient.setSoTimeout(10000);
        ftpClient.enterLocalPassiveMode();
        if(ftpClient.login("Administrator", "xxxx"))
        {
            File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
            FileInputStream fs= new FileInputStream(sFile);
            String fileName = sFile.getName();
            Boolean result = ftpClient.storeFile("/ftpfile.atspace.co.uk/" + fileName, fs);
            String has = "";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I'm trying to upload images to a FTP server (on my local PC) from Android Phone (HTC Desire HD). Images are going to FTP server but they are corrupted.

And the method (ftpClient.storeFile()) throws IOException (Bad File Number)

Please help me.

This is the corrupted image link:

http://imageshack.us/photo/my-images/820/komikb.jpg/

And this is the code:

FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect("192.168.2.14");
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        ftpClient.setSoTimeout(10000);
        ftpClient.enterLocalPassiveMode();
        if(ftpClient.login("Administrator", "xxxx"))
        {
            File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
            FileInputStream fs= new FileInputStream(sFile);
            String fileName = sFile.getName();
            Boolean result = ftpClient.storeFile("/ftpfile.atspace.co.uk/" + fileName, fs);
            String has = "";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-11-17 18:06:43

Apache FTP 客户端在这方面有几个突出的问题。下面是有关如何使用 Ftp4J 通过 java 以编程方式有效处理 ftp 的说明。

下载 Ftp4J: http://www.sauronsoftware.it/projects/ftp4j/download.php< /a>

然后在你的 IDE 中:

import java.io.File;
import java.io.IOException;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

public class FTP4J {

    /**
     * @param args
     * @throws FTPAbortedException 
     * @throws FTPDataTransferException 
     * @throws FTPException 
     * @throws FTPIllegalReplyException 
     * @throws IOException 
     * @throws IllegalStateException 
     */
    public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException {
        FTP4J ftp= new FTP4J();
        ftp.transfer();
    }

    private void transfer() throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException{
        FTPClient client = new FTPClient();
        client.connect("192.168.0.1"); //conect to FTP server (in my case a vsftp on centos 6.4)
        client.login("admn", "admn123");//login to FTP Server
        client.changeDirectory("/usr/share/tomcat/webapps/imgs/"); //tell FTP4J where on the Ftp Server to send your file that you want to upload.
        File fileUpload = new File ("C:\\Users\\ih8w8\\Pictures\\1.jpg"); //point FTP4J to the file you want to upload
        client.upload(fileUpload); //upload it
        client.disconnect(true); //close connection (note: you could also log out first, then disconn if youre not in a test env)
    }

}

Apache FTP Client has several outstanding issues with this. Below are instructions on how to use Ftp4J to effectively handle ftp programatically though java.

Download Ftp4J: http://www.sauronsoftware.it/projects/ftp4j/download.php

Then in your IDE:

import java.io.File;
import java.io.IOException;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

public class FTP4J {

    /**
     * @param args
     * @throws FTPAbortedException 
     * @throws FTPDataTransferException 
     * @throws FTPException 
     * @throws FTPIllegalReplyException 
     * @throws IOException 
     * @throws IllegalStateException 
     */
    public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException {
        FTP4J ftp= new FTP4J();
        ftp.transfer();
    }

    private void transfer() throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException{
        FTPClient client = new FTPClient();
        client.connect("192.168.0.1"); //conect to FTP server (in my case a vsftp on centos 6.4)
        client.login("admn", "admn123");//login to FTP Server
        client.changeDirectory("/usr/share/tomcat/webapps/imgs/"); //tell FTP4J where on the Ftp Server to send your file that you want to upload.
        File fileUpload = new File ("C:\\Users\\ih8w8\\Pictures\\1.jpg"); //point FTP4J to the file you want to upload
        client.upload(fileUpload); //upload it
        client.disconnect(true); //close connection (note: you could also log out first, then disconn if youre not in a test env)
    }

}
神回复 2024-11-17 18:06:43

问题解决了。 FTPClient 类存在“最后一个数据包数据丢失错误”。但这在 3.0.1 23.05.2011 版本中得到了解决。

关于bug的详细解释可以看到:
https://issues.apache.org/jira/browse/NET-409

您可以下载固定版本
https://repository.apache。 org/content/repositories/snapshots/commons-net/commons-net/3.0.1-SNAPSHOT/

Problem is solved. FTPClient class has "last packet data loss bug". But this was solved with 3.0.1 23.05.2011 release.

You can see from detailed explanation about bug:
https://issues.apache.org/jira/browse/NET-409

You can download fixed relea
https://repository.apache.org/content/repositories/snapshots/commons-net/commons-net/3.0.1-SNAPSHOT/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文