【问题标题】:EPIPE (Broken pipe) while uploading?上传时EPIPE(断管)?
【发布时间】:2013-03-30 00:50:26
【问题描述】:

我的代码有问题,但我不知道 E 日志报告在哪里

04-08 05:47:46.745: E/Upload Server(20080): Starting  : /storage/sdcard1/Music/Piano (my favourites)/11 Tchaikovsky - The Music Lovers.mp3
04-08 05:47:47.136: E/Upload Server(20080): Connection Error : sendto failed: EPIPE (Broken pipe)

什么是(EPIPE)? ,当我尝试上传图像时,它的上传成功,但任何其他文件 E Cat 报告(断管)为什么!

这是我的上传代码

   @Override
    protected String doInBackground(String... urls) {


    String upLoadServerUri = "http://test.com/test.php";
    String fileName = this.file_path;
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize =  1*1024*1024;
    File sourceFile = new File(fileName);
    int sentBytes = 0;
    long fileSize = sourceFile.length();
    connection = null;


    try
    {


    FileInputStream fileInputStream = new FileInputStream(sourceFile);

    Log.e("Upload Server ", "Starting  : "+ fileName );

    URL url = new URL(upLoadServerUri);
    connection = (HttpURLConnection) url.openConnection();


    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setChunkedStreamingMode(1024);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);
    outputStream = new DataOutputStream(connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"file[]\";filename=\""+ fileName + "\"" + lineEnd);

       outputStream.writeBytes(lineEnd);
       bytesAvailable = fileInputStream.available();
       bufferSize = Math.min(bytesAvailable, maxBufferSize);
       buffer = new byte[bufferSize];



bytesRead = fileInputStream.read(buffer, 0, bufferSize);

   while (bytesRead > 0)
   {

   if(isCancelled()){

   break;
   }

   sentBytes += bytesRead;

   double percentDone = (sentBytes * 1.0) / fileSize * 100;
   publishProgress((int)percentDone);

   outputStream.write(buffer, 0, bufferSize);

   bytesAvailable = fileInputStream.available();

   bufferSize = Math.min(bytesAvailable,     maxBufferSize);
   bytesRead = fileInputStream.read(buffer, 0,      bufferSize);
   }

   if(isCancelled()){

    fileInputStream.close();
   outputStream.flush();
   outputStream.close();
   Log.e("Upload Server ", "upload Canceled " );
   return "canceled";
   }

   outputStream.writeBytes(lineEnd);
   outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
   int serverResponseCode       = connection.getResponseCode();
    fileInputStream.close();
   outputStream.flush();
   outputStream.close();

   if(serverResponseCode == 200)
   {
   Scanner s;
   s = new Scanner(connection.getInputStream());
   s.useDelimiter("\\Z");
   final String response = s.next();
   Log.e("Upload Server ", "Message : " + response);
   return response;
   }else
   {
   Log.e("Upload Server ", "Server Code Error : " + serverResponseCode );
   return "faild";
   }
   }  catch (final Exception e) {

   Log.e("Upload Server ", "Error : " +  e.getMessage() );
   }

   return "falid";
}

请注意在 android 应用中瞄准仍然较新 :) 我用谷歌搜索了我的问题,我找不到解决方案,请帮忙!

【问题讨论】:

    标签: java android upload httpurlconnection


    【解决方案1】:

    我在使用HttpURLConnection 时遇到了类似的问题。只需添加:

    conn.setRequestProperty("connection", "close"); // disables Keep Alive
    

    到您的连接或为所有连接禁用它:

    System.setProperty("http.keepAlive", "false");
    

    来自关于disconnect()的API:

    释放此连接,以便其资源可以重复使用或关闭。 与其他 Java 实现不同,这不一定会关闭可重用的套接字连接。您可以通过在发出任何 HTTP 请求之前将 http.keepAlive 系统属性设置为 false 来禁用所有连接重用。

    因此,Android 将重用旧的套接字连接。使用上面的代码禁用它,您可以修复它。

    【讨论】:

      【解决方案2】:

      'Broken pipe' 表示您已写入已被对等方关闭的连接。

      您可能已超出上传大小限制。

      您还应该注意,您对available() 的使用是无效的。 Javadoc 中有一个关于 not 以您使用它的方式使用它的特定警告。反正你不需要它:

      while ((count = in.read(buffer)) > 0)
      {
        out.write(buffer, 0, count);
      }
      

      其中buffer 是任何合理的大小,例如8192 字节。

      【讨论】:

      • 当我删除 (connection.setRequestProperty("Connection", "Keep-Alive") ) 时它工作但有时 Cat 报告连接 Null !
      • HTTP keep-alive 实际上是默认的。无论如何,很难理解为什么改变它会产生任何影响。我不知道你最后四个字是什么意思。
      • 是的,它工作了一段时间,但之后停止工作。
      • @SreekanthKarumanaghat 那你一定把它弄坏了。这段代码已经工作了 17 年。
      • @JackKFouani 'Cat 报告连接 Null' 不可能由此代码引起。您遇到了先前的错误,可能是连接异常。
      猜你喜欢
      • 2014-02-09
      • 2013-01-26
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多