android - 上传文件到FTP服务器

标签 android

我正在尝试使用以下代码将文件上传到 FTP 服务器:

private void upload( String ftpServer, String user, String password,
            String fileName, File source ) throws MalformedURLException,
            IOException
    {
         if (ftpServer != null && fileName != null && source != null){
            StringBuffer sb = new StringBuffer( "ftp://" );

            if (user != null && password != null){
               sb.append( user );
               sb.append( ':' );
               sb.append( password );
               sb.append( '@' );
            }

            sb.append( ftpServer );
            sb.append( '/' );
            sb.append( fileName );
            sb.append( ";type=i" );

            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try
            {
               URL url = new URL( sb.toString() );
               URLConnection urlc = url.openConnection();
                   urlc.setDoOutput(true);
               bos = new BufferedOutputStream( urlc.getOutputStream() );
               bis = new BufferedInputStream( new FileInputStream( source ) );
               int i;
               while((i = bis.read()) != -1){
                      bos.write( i );
               }

            }
            finally
            {
               if (bis != null)
                  try
                  {
                     bis.close();
                  }
                  catch (IOException ioe)
                  {
                     ioe.printStackTrace();
                  }
               if (bos != null)
                  try
                  {
                     bos.close();
                  }
                  catch (IOException ioe)
                  {
                     ioe.printStackTrace();
                  }
            }
         }
         else{
            Log.e("Tag", "Input not available." );
         }
    }

但是文件并没有在服务器上结束。

最佳答案

我不确定您是否可以通过这种方式与 FTP 服务器通信。 我推荐使用 apache ftp 客户端。

下载地址 http://commons.apache.org/net/download_net.cgi

文档位于 http://commons.apache.org/net/api/org/apache/commons/net/ftp/package-summary.html

关于android - 上传文件到FTP服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871850/

相关文章:

android - 如何在 Firebase 中查找平均 session 持续时间?

android - Google Drive Sqlite db 文件从 android 应用程序上传

android - 无法关闭自定义 DialogFragment Android

java - 如何获取内部和外部存储中的可用空间

android - adb 深层链接触发器不适用于某些参数

android - 样式 Action 模式

java - C服务器未接收java/android客户端消息

java - 如何将首选项值获取到静态字符串变量中?

android - 在我的 Activity 中放置带有 sin/cos 波的图表

android - ListView 和适配器