2012-06-20 17 views
2

我正嘗試從我的Java代碼將Base64編碼的圖像發佈到網站。我已經在本地測試了編碼和解碼文件,它效果很好!但是,當它到達網站時,我被告知圖像是空白的。Base64編碼在Java中的圖像顯然錯了,我錯過了什麼?

這是我如何POST'ing。如果我使用其他操作而不是上傳,我會得到正確的回覆!

ready = new java.net.URL(url); 
     WebRequest request = new WebRequest(ready, HttpMethod.POST); 
     request.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded"); 

     String requestBody = "action=upload" 
       +"&key=ABCDEFG123456" 
       + "&file=" + encodedFile 
       + "&gen_task_id=" + SQL.getNextID(); 

encodedFile來自下面的代碼:

File file = new File("temp.jpg"); 

    FileInputStream fin = new FileInputStream(file); 

    byte fileContent[] = new byte[(int)file.length()]; 
    fin.read(fileContent); 

    //all chars in encoded are guaranteed to be 7-bit ASCII 
    byte[] encoded = Base64.encodeBase64(fileContent); 
    String encodedFile = new String(encoded); 

說真的,我在做什麼錯?現在我一直在頭撞牆!

+0

你可以使用PUT上傳文件,使內容與參數隔離嗎?也許你正在碰到某種類型的長度限制(並且它是更「restie」解決方案「)? – Sam

+0

這將有助於你嗎?http://stackoverflow.com/questions/1312832/java-image-encoding-in-xml –

+0

哦,你的接收容器是什麼?Tomcat具有開箱即用的默認POST大小限制:maxPostSize =「」,但我想確定。謝謝! – Sam

回答

3

我終於明白了。這是我爲其他任何人解決此問題所做的。

BufferedImage img = ImageIO.read(new File("temp.jpg"));    
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
ImageIO.write(img, "jpg", baos); 
baos.flush(); 
Base64 base = new Base64(false); 
String encodedImage = base.encodeToString(baos.toByteArray()); 
baos.close(); 
encodedImage = java.net.URLEncoder.encode(encodedImage, "ISO-8859-1"); 
request.setRequestBody(encodedImage); 
+0

嗨,你解決方案是正確的,但BufferedImage和ImageIO不支持在android中...我在android中遇到這個問題...你能否給我提供任何解決方案???謝謝... – Rushi

+0

恐怕我已經而不是很長一段時間的Android – David

1

不保證該字節數組緩衝區b將即使數據是可用的完全充滿。以下代碼確保緩衝區已滿。

File file = new File("temp.jpg"); 

FileInputStream fin = new FileInputStream(file); 

byte fileContent[] = new byte[(int)file.length()]; 
int offset = 0; 

while (offset < fileContent.length) { 
    int count = fin.read(fileContent, offset, fileContent.length - offset); 
    offset += count; 
} 

//all chars in encoded are guaranteed to be 7-bit ASCII 
byte[] encoded = Base64.encodeBase64(fileContent); 
String encodedFile = new String(encoded); 

或者,你可以使用ByteArrayOutputStream這樣的:

File file = new File("temp.jpg"); 

FileInputStream fin = new FileInputStream(file); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

byte [] buffer = new byte[1024]; 
int count = 0; 

while ((count = fin.read(buffer)) != -1) { 
    baos.write(buffer, 0, count); 
} 

byte [] fileContent = baos.toByteArray(); 

//all chars in encoded are guaranteed to be 7-bit ASCII 
byte[] encoded = Base64.encodeBase64(fileContent); 
String encodedFile = new String(encoded); 

或者,你可以換你FileInputStream對象在DataInputStream對象是這樣的:

File file = new File("temp.jpg"); 

FileInputStream fin = new FileInputStream(file); 
DataInputStream dis = new DataInputStream(fin); 

byte fileContent[] = new byte[(int)file.length()]; 
dis.readFully(fileContent); 

//all chars in encoded are guaranteed to be 7-bit ASCII 
byte[] encoded = Base64.encodeBase64(fileContent); 
String encodedFile = new String(encoded); 

我敢肯定有有更多的方法來完成這件事。