为什么没有在Android平台的InputStream我? [英] Why is my InputStream not working in Android?
问题描述
我写的Android图像文件上传我最喜欢的图片库软件,它使用FTP。
我一直在使用Apache的共享网FTP作为我的FTP库的基础上过去堆栈溢出问题启动。像这样:
I'm writing an image file uploader for android for my favorite image gallery software and it uses FTP. I've started using Apache-Commons Net FTP as my ftp library based on past stack overflow questions. Like so:
FTPClient ftp = new FTPClient();
try{
ftp.connect(host);
Log.i(TAG,"we connected");
if(!ftp.login(user,pass)){
ftp.logout();
//TODO: alert user it didn't happen
return;
}
String replyStatus = ftp.getStatus();
Log.i(TAG,replyStatus);
int replyCode = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode))
{
ftp.disconnect();
//TODO: alert user it didn't happen
return;
}
Log.i(TAG,"we logged in");
ftp.changeWorkingDirectory(path);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
for(int i = 0; i < contentUris.size(); i++){
Log.i(TAG,"uploading new file");
Uri stream = (Uri) contentUris.get(i);
//InputStream in = openFileInput(getRealPathFromURI(stream));
InputStream in =this.getContentResolver().openInputStream(stream);
BufferedInputStream buffIn=null;
buffIn=new BufferedInputStream(in);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
boolean Store = ftp.storeFile("test.jpg", buffIn);
Log.i(TAG, "uploaded test");
}
ftp.disconnect();
}
catch(Exception ex){
//do something wise and smart and useful
}
我说我就要记录在日志中看到的,我能够更改目录,当我上传,我得到我的目录test.jpg放在,但0字节大小。
I see in the log that I'm getting logged in, I'm able to change directories, and when I upload I get a test.jpg in my directory, but with 0 bytes size.
怎么办?我不能打开输入流吧?如何正确呢?
What gives? Am I not opening the input stream right? How do I properly do it?
如果我不给足够的细节,让我知道 - 请查看满code,也可在github上
If I'm not giving enough detail, let me know - the full code is also available on github
推荐答案
看起来问题是,你不能让一个活跃的FTP连接,您必须使用被动模式。所以更改顶部这样:)ftpClient.enterLocalPassiveMode(;
Looks like the problem is that you can't make an active ftp connection, you must use passive mode. So change the top to this: ftpClient.enterLocalPassiveMode();
FTPClient ftp = new FTPClient();
try{
ftp.connect(host);
ftp.enterLocalPassiveMode();
Log.i(TAG,"we connected");
if(!ftp.login(user,pass)){
ftp.logout();
//TODO: alert user it didn't happen
return;
}
String replyStatus = ftp.getStatus();
Log.i(TAG,replyStatus);
int replyCode = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode))
{
ftp.disconnect();
//TODO: alert user it didn't happen
return;
}
Log.i(TAG,"we logged in");
ftp.changeWorkingDirectory(path);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
for(int i = 0; i < contentUris.size(); i++){
Log.i(TAG,"uploading new file");
Uri stream = (Uri) contentUris.get(i);
String filePath = getRealPathFromURI(stream);
InputStream in = new FileInputStream(filePath);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
boolean Store = ftp.storeFile("test.jpg", in);
Log.i(TAG, "uploaded test");
}
ftp.disconnect();
}
catch(Exception ex){
//TODO: properly handle exception
//Log.i(TAG,ex);
//TODO:Alert the user this failed
}
这篇关于为什么没有在Android平台的InputStream我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!