2011-08-16 52 views
1

嗨朋友我面臨一個問題,當我上傳圖像的響應來無效的文件,所以請幫助解決這個問題。問題(無效的文件響應)在Android上的圖像上傳

public void executeMultipartPost() throws Exception { 

       try { 

        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        MybitmapImage.compress(Bitmap.CompressFormat.JPEG, 75, bos); 
        byte[] data = bos.toByteArray(); 
        HttpClient httpClient = new DefaultHttpClient(); 
        HttpPost postRequest = new HttpPost("http://myurl.net/uploadphoto.aspx"); 
        ByteArrayBody bab = new ByteArrayBody(data,"5.png"); 

      //  File file= new File("/sdcard/mickey.jpg"); 

       //  FileBody bin = new FileBody(file,"image/jpg"); 

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
       // reqEntity.addPart("5.jpg",bin); 

        reqEntity.addPart("5.png", bab); 



        postRequest.setEntity(reqEntity); 
        System.out.println("Method"+postRequest.getMethod()); 
        System.out.println("tostring"+postRequest.toString()); 

        HttpResponse response = httpClient.execute(postRequest); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
        String sResponse; 
        StringBuilder s = new StringBuilder(); 
        while ((sResponse = reader.readLine()) != null) { 
         s = s.append(sResponse); 
        } 

        System.out.println("Response: " + s); 

       } catch (Exception e) { 

        // handle exception here 
        System.out.println("Error"+e); 
        Log.e(e.getClass().getName(), e.getMessage()); 

       } 

      } 

在迴應無效Flie來。所以請儘量解決我的問題。 非常感謝

+0

請添加堆棧跟蹤和錯誤消息的問題。 – Zds

+0

有沒有錯誤發生,但在響應是這樣的:08-16 13:02:06.345:INFO/System.out(661):迴應:無效的文件 –

+0

可以上傳圖像到你的網絡服務器? –

回答

0

要添加圖像,你可以試試(你有評論代碼) -

File fileToUpload = new File(filePath); 
FileBody fileBody = new FileBody(fileToUpload, "image/jpeg"); 
reqEntity.addPart("file", fileBody); 

而不是添加ByteArrayBody

0

要登錄你的錯誤,請嘗試在catch

catch (Exception e) { 

    // handle exception here 
    e.printStackTrace(); 

}