无法将zip文件从Android上传到服务器sendto失败:epipe(断管)

时间:2018-02-06 05:49:01

标签: java android

我正在创建一个Android应用程序,用于将zip文件从本地文件系统上传到服务器。以下是我的文件上传代码。

 protected Object doInBackground(Object[] objects) {
        try {
    String boundary = "===" + System.currentTimeMillis() + "===";
                String LINE_FEED = "\r\n";
                String charset;
                String fieldName = "uploadFile";
                URL url = new URL("http://hridhaanirs.com/REST-TEST/upload");
                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                httpConn.setUseCaches(false);
                httpConn.setDoOutput(true); // indicates POST method
                httpConn.setDoInput(true);
                httpConn.setRequestProperty( "Accept-Encoding", "" );
                httpConn.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary=" + boundary);
                httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
                httpConn.setRequestProperty("Test", "Bonjour");
                OutputStream outputStream = httpConn.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"),
                        true);
                outputStream = httpConn.getOutputStream();
                File file=new File((String)objects[0]);
                String fileName = file.getName();
                writer.append("--" + boundary).append(LINE_FEED);
                writer.append("Content-Disposition: form-data; name=\"" + fieldName
                        + "\"; filename=\"" + fileName + "\"")
                        .append(LINE_FEED);
                writer.append(
                        "Content-Type: "
                                + URLConnection.guessContentTypeFromName(fileName))
                        .append(LINE_FEED);
                writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);

                writer.append(LINE_FEED);
                writer.flush();

                FileInputStream inputStream = new FileInputStream(file);
                FileReader fileReader=new FileReader(file);
                int buff;
                while((buff=fileReader.read())!=-1){
                    outputStream.write(buff);
                }
                outputStream.flush();
                inputStream.close();

                writer.append(LINE_FEED);
                writer.flush();
                List<String> response = new ArrayList<>();
                writer.append(LINE_FEED).flush();
                writer.append("--" + boundary + "--").append(LINE_FEED);
                writer.close();

                int status = httpConn.getResponseCode();
                if (status == HttpURLConnection.HTTP_OK) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        response.add(line);
                    }
                    reader.close();
                    httpConn.disconnect();
                }
                return response;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

在上面的代码执行调试时我看到了以下异常。

02-06 11:13:36.863 32495-32598/com.tech.mds.mdsconnect I/System.out: [OkHttp] sendRequest>>
02-06 11:13:41.886 32495-32598/com.tech.mds.mdsconnect W/System.err: java.net.SocketException: sendto failed: EPIPE (Broken pipe)
02-06 11:13:41.913 32495-32598/com.tech.mds.mdsconnect W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:586)
02-06 11:13:41.935 32495-32598/com.tech.mds.mdsconnect W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:555)
02-06 11:13:41.957 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:513)
02-06 11:13:41.978 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:42)
02-06 11:13:41.998 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:271)
02-06 11:13:42.014 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okio.Okio$1.write(Okio.java:70)
02-06 11:13:42.038 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)
02-06 11:13:42.055 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okio.RealBufferedSink.write(RealBufferedSink.java:44)
02-06 11:13:42.076 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.RetryableSink.writeToSocket(RetryableSink.java:77)
02-06 11:13:42.098 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.HttpConnection.writeRequestBody(HttpConnection.java:240)
02-06 11:13:42.119 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.HttpTransport.writeRequestBody(HttpTransport.java:77)
02-06 11:13:42.138 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:631)
02-06 11:13:42.160 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:398)
02-06 11:13:42.182 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:342)
02-06 11:13:42.205 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:520)
02-06 11:13:42.223 32495-32598/com.tech.mds.mdsconnect W/System.err:     at com.tech.mds.mdsconnect.FileUpload.doInBackground(FileUpload.java:88)
02-06 11:13:42.240 32495-32598/com.tech.mds.mdsconnect W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
02-06 11:13:42.258 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-06 11:13:42.275 32495-32598/com.tech.mds.mdsconnect W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-06 11:13:42.293 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-06 11:13:42.308 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-06 11:13:42.316 32495-32598/com.tech.mds.mdsconnect W/System.err:     at java.lang.Thread.run(Thread.java:818)
02-06 11:13:42.334 32495-32598/com.tech.mds.mdsconnect W/System.err: Caused by: android.system.ErrnoException: sendto failed: EPIPE (Broken pipe)
02-06 11:13:42.403 32495-32598/com.tech.mds.mdsconnect W/System.err:     at libcore.io.Posix.sendtoBytes(Native Method)
02-06 11:13:42.413 32495-32598/com.tech.mds.mdsconnect W/System.err:     at libcore.io.Posix.sendto(Posix.java:206)
02-06 11:13:42.423 32495-32598/com.tech.mds.mdsconnect W/System.err:     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
02-06 11:13:42.433 32495-32598/com.tech.mds.mdsconnect W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:553)
02-06 11:13:42.438 32495-32598/com.tech.mds.mdsconnect W/System.err:    ... 20 more

ZIP文件包含几个图像和一个.txt文件。如果我上传.txt文件,那么它可以成功运行。 而且我在JAVA SE环境中使用了相同的代码并成功上传了相同的zip文件。所以肯定这似乎是一个环境问题。

2 个答案:

答案 0 :(得分:0)

  1. 服务器已关闭连接,原因是您的手工实现的分块协议不正确或者您已超出上传限制。

  2. 抛弃手工实施的分块协议并使用HttpURLConnection.setChunkedStreamingMode()

答案 1 :(得分:0)

我修改了@greenapps建议的阅读代码,一切正常。似乎一次读取一个字节使得请求非常慢,因此连接终止。使用缓冲区一次读取特定的字节数并写入输出流。