duanjia3187 2014-05-29 13:13
浏览 51
已采纳

在android中上传图片时出现IOException

Hi Iam trying to upload an image to server. But it is giving me IOException while uploading the image, which means the exception part is always get running. Pls help me how to resolve this problem.

Iam using the below code:

try {
                UpurlString = "http:/xxxxx/xxx/UpImage.php";
                length = fileInputStream.available();               
                //connectHTTP(UpurlString);
                url = new URL(UpurlString);
                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);
                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                Log.e("FilenameCheck",filename);
                dos.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\""
                        + filename + "" + lineEnd);
                dos.writeBytes(lineEnd);
                buffer = new byte[8192];
                bytesRead = 0;
                while ((bytesRead = fileInputStream.read(buffer)) > 0) {
                    dos.write(buffer, 0, bytesRead);
                    sentData += bytesRead;
                }
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);               
                fileInputStream.close();
                dos.flush();
                dos.close();                        
                    inStream = new DataInputStream(conn.getInputStream());
                    while ((strResponse = inStream.readLine()) != null) {
                        Log.e("Response", strResponse);
                        /*if (strResponse.equalsIgnoreCase("The file " + filename
                                + " has been uploaded")) {                          
                        } else {                            
                        }*/
                    }
                    inStream.close();               
            } catch (IOException ioex) {
                Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }
  • 写回答

1条回答 默认 最新

  • doutouhe5343 2014-05-29 13:27
    关注

    There is a / in the url http:/webleads.usawebdept.com/IPhone/leadImage.php after http

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?