2012-12-09 87 views
-1

我得到異常作爲標題,而發送圖像到Java服務器java.io.utfdataformatexception:字符串太長

下面的代碼:

 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     img.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 

     String imageDataString = new String(Base64.encodeBase64(byteArray)); 
     System.out.println(imageDataString); 

     dataOutputStream.writeUTF(imageDataString); 
     dataOutputStream.flush(); 

img是一個位圖文件。

任何幫助將不勝感激!

+0

它適用於較小的圖像嗎?你如何從服務器恢復圖像的字符串? –

+0

什麼是dataOutputStream的類型? –

+0

圖像沒有被髮送,因爲根據異常解碼字符串太長,但當我發送一些其他編碼字符串像'string =「一些字符串」它得到發送 – Saaram

回答

1

@Sarram跟隨在吹塑鏈路的代碼,我在base64String第i被轉換成文件

吹塑的形式與其他數據一起發送在肥皂請求圖像是代碼的參考

Writing decoded base64 byte array as image file

我使用這個很酷的解碼器import sun.misc.BASE64Decoder; 服務器端可以那樣做

 String filePath = "/destination/temp/file_name.jpg"; 
     File imageFile = new File(filePath); 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(imageFile);//create file 
     } catch (FileNotFoundException e1) { 
      e1.printStackTrace(); 
     } 
     BASE64Decoder decoder = new BASE64Decoder();//create decodeer object 
     byte[] decodedBytes = null; 
     try { 
      decodedBytes = decoder.decodeBuffer(imageFileBase64);//decode base64 string that you are sending from clinet side 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     try { 
      fos.write(decodedBytes);//write the decoded string on file and you have ur image at server side 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      fos.flush(); 
      fos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
+0

您正在檢索來自android模擬器的SD卡的圖像? – Saaram

+0

這裏我發送圖像從客戶端到服務器,所以我需要在Android上進行編碼並在java服務器上解碼它 – Saaram

+0

我在android clinet中從SD卡輸入圖像,然後我將它編碼爲base64字符串和我把這個字符串放到soap調用請求中,在服務器端我解碼它,解碼base 64字符串中的圖像並將其轉換爲文件的代碼在上面 –