EPIPE(管道中断),同时上传?(EPIPE (Broken pipe) while upload

2019-08-20 05:29发布

我在我的代码中的问题,但我不知道它在哪里对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)? ,当我尝试成功上传图片上载的,但任何其他文件Ë猫报告(管道中断)为什么!

这是我上传的代码

   @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应用中的目标仍然较新的:)我GOOGLE了我的问题,我无法找到一个解决方案,请帮助!

Answer 1:

“断管”意味着您已写入到已经被同行封闭的连接。

也许你已经超过了上传大小限制。

你还应该注意你的使用available()是无效的。 有一个在关于使用它,你正在使用它的方式的Javadoc具体的警告。 你不需要也无妨:

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

其中buffer是任何合理的尺寸,例如8192个字节。



Answer 2:

我使用过类似的问题HttpURLConnection 。 只需添加:

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

您连接或禁用所有连接:

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

大约从断开的API():

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

因此,Android将重用旧的插座连接。 与代码禁用它上面可以修复它。



文章来源: EPIPE (Broken pipe) while uploading?