为什么我的dos.write(buffer,0,bufferSize)中的管道坏了?

时间:2013-09-24 03:27:49

标签: android dataoutputstream broken-pipe

我在dos.write中写了一个大问题,它给了我一个破管道,我不知道如何解决它。任何人都可以建议处理此类错误的正确方法。我搜索到网络,并说问题是连接,但当我尝试删除我在outputStream中写入文件的代码时,outputStream中的其他写入字节正在工作。请帮帮我..

----这是我的代码----

                FileInputStream stream = new FileInputStream(mFile);

                bytesAvailable = stream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                int bufferlength = buffer.length;

                 // read file and write it into form...
                 bytesRead = stream.read(buffer, 0, bufferSize);

                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary =  "-------------------------acebdf13572468";// random data

                 String paramName = mFile.getName();
                 String param4 = samplefile;

                 Log.i("FileName", paramName);
                 URL url = new URL(urlString);

                     // Open a HTTP connection to the URL
                     conn = (HttpURLConnection) url.openConnection();

                     // Allow Inputs
                     conn.setDoInput(true);
                     // Allow Outputs
                     conn.setDoOutput(true);
                     // Don't use a cached copy.
                     conn.setUseCaches(false);
                     // Use a post method.
                     conn.setRequestMethod("POST");

                     String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP); 
                     conn.setRequestProperty("Authorization", "Basic "+encoded);
                     //conn.setRequestProperty("Connection", "Keep-Alive");
                     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

                     conn.setChunkedStreamingMode(bufferSize);
                     conn.connect();



                     dos = new DataOutputStream( conn.getOutputStream() );

                     dos.writeBytes(twoHyphens + boundary + lineEnd);


                    dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + paramName + "\"" + lineEnd); // filename is the Name of the File to be uploaded
                    dos.writeBytes("Content-Type: " + mimetype + lineEnd);
                    dos.writeBytes(lineEnd);


                        dos.write(buffer, 0, bufferSize);
                        Log.i("Run at: ", "Normal File");
                        var = bufferSize;

                        Log.i("BytesAvailable", String.valueOf(bytesAvailable));
                        Log.i("bufferSize", String.valueOf(bufferSize));
                        Log.i("Bytes Read", String.valueOf(bytesRead));
                        Log.i("buffer", String.valueOf(buffer.length));

                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);


                    // Send parameter #chunks
                    dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes("0" + lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);


                    // Send parameter #name
                    dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(myUUID + lineEnd);


                    Log.i("name: ", paramName);
                    Log.i("filename: ", param4);
                    Log.i("FileSize: ", String.valueOf(bytesAvailable));


                    // send multipart form data necesssary after file data...
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                    // close streams


                     FileUploadresponseCode = String.valueOf(conn.getResponseCode());
                     FileUploadresponseMessage = conn.getResponseMessage();
                     Log.i("DOS: ", String.valueOf(dos.size()));
                     Log.i("Response Code: ", FileUploadresponseCode);
                     Log.i("Response Message: ", FileUploadresponseMessage);

                     is = conn.getInputStream();

                  // retrieve the response from server
                  int ch;

                  StringBuffer b =new StringBuffer();
                  while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
                  String s = b.toString();
                  Log.i("Response",s);
                  if (!FileUploadresponseCode.equals("200")){
                         FileUploadresponseMessage = conn.getResponseMessage();
                         TypeOfError = "Status Code";
                         return false;
                    }

0 个答案:

没有答案