上传时出现EPIPE(Broken pipe)错误?

4

我在代码中遇到问题,但是不知道在哪里,需要查看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应用程序方面仍然比较新:) 我在谷歌上搜索了我的问题,但是没有找到解决方案,请帮忙!

2个回答

8
'Broken pipe' 意味着您向已被对等方关闭的连接写入了数据。
可能您已超过上传大小限制。
您还应该注意,您对 available() 的使用是无效的。Javadoc 中有一个特定的警告,指出不要按照您目前的方式使用它。 无论如何,您都不需要它:
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

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


1
当我删除了(connection.setRequestProperty("Connection", "Keep-Alive"))时,它可以工作,但有时Cat报告连接为空! - Noob
1
HTTP keep-alive 实际上是默认设置。无论如何,很难看出为什么更改它应该有任何影响。我不知道你最后四个词的意思。 - user207421
是的,它在一段时间内工作正常,但之后就停止工作了。 - Sreekanth Karumanaghat
@SreekanthKarumanaghat 那你一定是弄坏了它。这段代码已经运行了17年。 - user207421
@JackKFouani,“猫报告连接 Null”不可能由此代码引起。你先前肯定有错误,可能是连接异常。 - user207421

3

我使用HttpURLConnection时也遇到了类似的问题。只需添加以下代码:

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

针对您的连接或者禁用所有连接:

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

根据disconnect()的API文档:

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

因此,Android将重用旧的套接字连接。使用上述代码禁用它即可解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接