【问题标题】:Transfering text data to a web server using Java使用 Java 将文本数据传输到 Web 服务器
【发布时间】:2011-06-21 18:04:14
【问题描述】:

我正在尝试使用 HttpURLConnection.getOutputStream() 写入我的 Web 服务器上的文本文件。我已经在两台不同的服务器上尝试过,但没有成功。

我添加了一个 FileWriter 来测试 InputStream,并且该文件已在本地目录中正确创建,但 Web 服务器目录中没有显示任何内容,即使关闭了所有密码保护。

任何帮助将不胜感激。

URL url;
    try {
        url = new URL("http://www.myWebsite.com/myFile.txt");

           HttpURLConnection urlConnection = null;

            try {
                urlConnection = (HttpURLConnection) url.openConnection();


                try {
                    urlConnection.setDoOutput(true);
                    urlConnection.setDoInput(true);


                    OutputStream in = new BufferedOutputStream(urlConnection.getOutputStream());

                     InputStream fin1;
                        try {
                            fin1 = new FileInputStream(Environment.getExternalStorageDirectory() + "/fileToRead.txt");
                            FileWriter fWriter = new FileWriter(Environment.getExternalStorageDirectory() + "/fileToWrite.txt");

                            int data = fin1.read();
                            while(data != -1) {

                            fWriter.write(data);
                            in.write(data);
                              data = fin1.read();
                            }



                            fWriter.flush();
                            fWriter.close();

                            fin1.close();

                            in.flush();
                            in.close();

                        } catch (FileNotFoundException e31) {
                            // TODO Auto-generated catch block
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                    }
                   } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                                        }
                    finally {
                     urlConnection.disconnect();
                   }
            } catch (IOException e4) {
                                    // TODO Auto-generated catch block
                e4.printStackTrace();
            }
    } catch (MalformedURLException e4) {
        // TODO Auto-generated catch block
        e4.printStackTrace();
    }

【问题讨论】:

  • 我在您的代码中看不到任何与身份验证相关的内容。你是不是觉得很奇怪?
  • 您在日志中看到什么类型的错误消息?我建议您将日志记录级别设置为 DEBUG 并在运行此代码时观察它们。此外,此代码是否在您尝试将文件写入的同一台服务器上?还有,myFile.txt、fileToRead.txt和fileToWrite.txt有什么区别?

标签: java ftp httpurlconnection outputstream


【解决方案1】:

您必须在 urlConnection 上调用 getInputStream() 以获取输出流以将套接字刷新到远程服务器。

在此处查看讨论:Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?

【讨论】:

    【解决方案2】:

    您正在捕获 IOException(就在 IOException 之后),但没有对它做任何事情。至少打印堆栈跟踪。

    【讨论】:

    • 不应该是评论而不是答案吗?
    【解决方案3】:

    你也可以使用Apache的Http Client http://hc.apache.org/httpcomponents-client-ga/index.html

    比让 URLConnection 工作容易得多。

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      相关资源
      最近更新 更多