|
|
|
เกิด exception sendto failed: EPIPE (Broken pipe) ของ android ครับ |
|
|
|
|
|
|
|
พอดีผมจะทำการ อัพโหลดรูปภาพ ไปยัง web server ครับ แต่พอกดส่งไป มันเกิด exception นี้อะครับ
06-20 14:24:18.420: W/System.err(16020): java.net.SocketException: sendto failed: EPIPE (Broken pipe)
06-20 14:24:18.430: W/System.err(16020): at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:496)
06-20 14:24:18.430: W/System.err(16020): at libcore.io.IoBridge.sendto(IoBridge.java:465)
06-20 14:24:18.430: W/System.err(16020): at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
06-20 14:24:18.430: W/System.err(16020): at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
06-20 14:24:18.430: W/System.err(16020): at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
06-20 14:24:18.430: W/System.err(16020): at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:231)
06-20 14:24:18.430: W/System.err(16020): at libcore.net.http.RetryableOutputStream.writeToSocket(RetryableOutputStream.java:70)
06-20 14:24:18.430: W/System.err(16020): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:777)
06-20 14:24:18.430: W/System.err(16020): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
06-20 14:24:18.430: W/System.err(16020): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
06-20 14:24:18.430: W/System.err(16020): at com.surinrobot.shop.ProductEditPicActivity$uploadPicAsync.doInBackground(ProductEditPicActivity.java:187)
06-20 14:24:18.430: W/System.err(16020): at com.surinrobot.shop.ProductEditPicActivity$uploadPicAsync.doInBackground(ProductEditPicActivity.java:1)
06-20 14:24:18.430: W/System.err(16020): at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-20 14:24:18.430: W/System.err(16020): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-20 14:24:18.430: W/System.err(16020): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-20 14:24:18.430: W/System.err(16020): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-20 14:24:18.430: W/System.err(16020): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-20 14:24:18.430: W/System.err(16020): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-20 14:24:18.430: W/System.err(16020): at java.lang.Thread.run(Thread.java:856)
06-20 14:24:18.430: W/System.err(16020): Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
06-20 14:24:18.440: W/System.err(16020): at libcore.io.Posix.sendtoBytes(Native Method)
06-20 14:24:18.440: W/System.err(16020): at libcore.io.Posix.sendto(Posix.java:146)
06-20 14:24:18.440: W/System.err(16020): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
06-20 14:24:18.440: W/System.err(16020): at libcore.io.IoBridge.sendto(IoBridge.java:463)
06-20 14:24:18.440: W/System.err(16020): ... 17 more
Code (Android-Java)
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
int resCode = 0;
String resMessage = "";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
toast.setGravity(Gravity.CENTER, 0, 0);
try {
/** Check file on SD Card ***/
File file = new File(imgPath);
if(!file.exists())
{
toast.show();
}
FileInputStream fileInputStream = new FileInputStream(new File(imgPath));
URL url = new URL("http://shop.surinrobot.com/web_service/test_uploadfile_shop.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"filUpload\";filename=\""+ imgPath + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Response Code and Message
resCode = conn.getResponseCode(); exception ตรงนี้
if(resCode == HttpURLConnection.HTTP_OK)
{
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = 0;
while ((read = is.read()) != -1) {
bos.write(read);
}
byte[] result = bos.toByteArray();
bos.close();
resMessage = new String(result);
}
Log.d("resCode=",Integer.toString(resCode));
Log.d("resMessage=",resMessage.toString());
fileInputStream.close();
outputStream.flush();
outputStream.close();
JSONArray jsonAr = new JSONArray(resMessage.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
ลองหาในเว็บนอก ลองแล้วก็ยังไม่หายครับ ไม่รู้จะทำยังไงดี
Tag : Mobile, Android, Tablets, JAVA, Mac, Web Service
|
|
|
|
|
|
Date :
2013-06-20 14:59:16 |
By :
vinsant |
View :
2531 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android Upload Send file to Web Server (Website)
|
|
|
|
|
Date :
2013-06-20 16:34:47 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|