【发布时间】:2014-08-08 11:19:39
【问题描述】:
我正在尝试将图像从 android 上传到 ftp 服务器,但是当我尝试打开我上传的图像时,我看到以下消息而不是图像“图像无法显示,因为它包含错误”
这是我使用的代码
public void uploadImage(String path){
String server = "www.domainname.com";
int port = 21;
String user = "ftp-username";
String pass = "ftp-password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: uploads first file using an InputStream
File firstLocalFile = new File(path);
long fileSize = firstLocalFile.length();
Log.i("File Size",fileSize+"");
String firstRemoteFile = "testfile1.jpg";
InputStream inputStream = new FileInputStream(firstLocalFile);
Log.i("uploading", "Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
Log.i("uploaded", "finished uploading first file");
}
// APPROACH #2: uploads second file using an OutputStream
File secondLocalFile = new File(path);
String secondRemoteFile = "testfile2.jpg";
inputStream = new FileInputStream(secondLocalFile);
Log.i("uploading", "Start uploading second file");
OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
Log.i("uploaded", "finished uploading second file");
}
} catch (IOException ex) {
Log.i("Error", "Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
哪里出错了!!?
提前谢谢..
【问题讨论】:
-
你试图在哪里以及如何显示图像?请给出原始图片和上传图片的文件长度。
-
上传图片后,我使用 ftp 用户名和密码登录到虚拟主机并双击图片,因此它应该显示在浏览器中,但我看到的是错误消息而不是它,但是如果我尝试使用它的 URL 地址查看它,我会看到这个律师-book.com/Images/profiles/faditest.jpg
-
请发一个真实的链接,我们可以点击。不能用ftp客户端吗?请找出长度。
-
图片小于100KB,这是我上传图片的真实链接lawyer-book.com/Images/profiles/faditest.jpg
-
您必须提供两种长度。一份原件,一份上传。以字节为单位!不在 KB 中。每个字节都很重要。