【问题标题】:Can't write to BufferedOutputStream that is connected to URLConnection无法写入连接到 URLConnection 的 BufferedOutputStream
【发布时间】:2012-02-04 21:46:39
【问题描述】:

我正在尝试使用 URLConnection 将文件上传到 FTP 服务器。没有抛出异常。我似乎能够连接到服务器。如果我“手动”(使用 FTP 客户端)上传文件,我可以毫无问题地下载该文件(使用另一个程序)。

我使用 BufferedOutputStream.write(int)。日志显示我可以读取本地文件(包含“Hej hej”)并访问其中的字节。问题是:没有任何东西写入服务器文件。当我在服务器上读取文件时,它仍然包含预上传内容。该文件未更新。我试过 BufferedOutputStream.write(String.getBytes()) 但这没有帮助。

static public void upload(String string, String file) {
    File file_ = new File(Environment.getExternalStorageDirectory() + "/Android/data/org.sionlinkoping.pingstprayer/files/thanks.txt");
try {
    upload("ftphome.mah.se", user, passwd, "thanks.txt", file_);
}
catch(Exception e) { Log.d("",e.toString()); }
}


static public 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://" );
    // check for authentication else assume its anonymous access.
    if (user != null && password != null)
    {
        sb.append( user );
        sb.append( ':' );
        sb.append( password );
        sb.append( '@' );
    }
    sb.append( ftpServer );
    sb.append( '/' );
    sb.append( fileName );

        BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try
        {
        URL url = new URL( sb.toString() );
        URLConnection urlc = url.openConnection();

            bos = new BufferedOutputStream( urlc.getOutputStream() );
        bis = new BufferedInputStream( new FileInputStream( source ) );

            int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) != -1)
        {
            Log.d("","i:"+i);
            bos.write( i );
        }
        bos.flush();
    }
    finally
    {
        if (bis != null)
            try
            {
                bis.close();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
        if (bos != null)
            try
            {
               bos.close();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
    }
    }
else
{
    System.out.println( "Input not available." );
}
}

日志显示:

01-07 14:00:50.662: DEBUG/(6925): i:239
01-07 14:00:50.662: DEBUG/(6925): i:187
01-07 14:00:50.662: DEBUG/(6925): i:191
01-07 14:00:50.662: DEBUG/(6925): i:72
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.662: DEBUG/(6925): i:32
01-07 14:00:50.662: DEBUG/(6925): i:104
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.672: DEBUG/(6925): i:10

【问题讨论】:

    标签: java android ftp urlconnection


    【解决方案1】:

    问题在于您假设写入 FTP URL 会导致文件被保存到服务器。它不会。结果只是未定义——它不是那样工作的。

    有可用的 FTP 库;你需要使用一个。例如,见here.

    【讨论】:

    • 好的,但是文件已经存在了。我只是想补充一下。还需要图书馆吗?
    • 您需要一个库(如果您是受虐狂,则需要编写一个使用 FTP 协议规范的库)来使用 FTP 做任何事情。
    • 啊哈!这是Java特有的事实吗?我正在开发的这个程序应该是一个 Android 应用程序。我之前在 Objective-C 中为 iPhone 编写了相同的应用程序,然后我似乎不需要任何额外的库,那么?也许 Objective-C 有那个标准的功能或者什么?
    • 顺便说一句,我仍然可以在没有库的情况下读取 FTP 服务器上的文件。
    • "这是 Java 特有的事实吗?"大概吧。没有法律规定不能实施其他平台,以便它们的URLConnection 等价物可以处理 ftp 上传。但无论如何,Java 并不是这样设计的——它是只读的。
    猜你喜欢
    • 2013-12-11
    • 2014-12-02
    • 2011-09-21
    • 1970-01-01
    • 2012-03-14
    • 2023-03-06
    • 1970-01-01
    • 2020-01-11
    • 2019-01-31
    相关资源
    最近更新 更多