gpt4 book ai didi

java - FTPClient 在上传到 Android 上的 FTP 服务器时损坏图像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:02 24 4
gpt4 key购买 nike

我正在尝试从 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();
}

最佳答案

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

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

然后在您的 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)
}

}

关于java - FTPClient 在上传到 Android 上的 FTP 服务器时损坏图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6193485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com