简体   繁体   English

为什么我的InputStream无法在Android中使用?

[英]Why is my InputStream not working in Android?

I'm writing an image file uploader for android for my favorite image gallery software and it uses FTP. 我正在为我最喜欢的图片库软件编写一个用于Android的图片文件上传器,它使用FTP。 I've started using Apache-Commons Net FTP as my ftp library based on past stack overflow questions . 基于过去的堆栈溢出问题,我已经开始使用Apache-Commons Net FTP作为ftp库。 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
}

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. 我在日志中看到要登录,可以更改目录,并且在上载时,目录中有一个test.jpg,但大小为0字节。

What gives? 是什么赋予了? Am I not opening the input stream right? 我没有打开输入流吗? How do I properly do it? 我该怎么做呢?

If I'm not giving enough detail, let me know - the full code is also available on github 如果我没有提供足够的细节,请告诉我- 完整的代码也可以在github上找到

Looks like the problem is that you can't make an active ftp connection, you must use passive mode. 看来问题在于您无法建立主动ftp连接,必须使用被动模式。 So change the top to this: ftpClient.enterLocalPassiveMode(); 因此,将顶部更改为: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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-InputStream无法正常工作 - Android - InputStream not working correctly 为什么InputStream停止我的应用程序? - Why InputStream is stopping my application? Java Android使用InputStream和File - Java Android working with InputStream and File 为什么我的 `InputStream` 实现不能与 `com.monitorjbl:xlsx-streamer:2.2.0` 中的 `StreamingReader` 一起工作 - Why my implementation of `InputStream` not working with `StreamingReader` from `com.monitorjbl:xlsx-streamer:2.2.0` PHP URL在浏览器上正常工作,但是当我尝试从我的android应用程序运行它时InputStream为null - PHP URL is working on broswer but when i tried to run it from my android application InputStream is null 为什么我的InputStream保留旧值? - Why does my InputStream hold old values? 为什么我的通知在 Android &gt; 7 上不起作用? - Why my notification not working on Android > 7? 为什么我的Android Intent无法正常工作? - Why is my Android Intent not working? 为什么android HttpURLConnection缓存输入流结果? - Why android HttpURLConnection cache the inputstream results? 尝试在Android Studio中打开输入流时,为什么会收到未处理的错误异常? (代码发布在说明中) - Why do I get the error unhandeled exception when trying to open my inputstream in Android studio? (code posted in description)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM