我正嘗試從我的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);
說真的,我在做什麼錯?現在我一直在頭撞牆!
你可以使用PUT上傳文件,使內容與參數隔離嗎?也許你正在碰到某種類型的長度限制(並且它是更「restie」解決方案「)? – Sam
這將有助於你嗎?http://stackoverflow.com/questions/1312832/java-image-encoding-in-xml –
哦,你的接收容器是什麼?Tomcat具有開箱即用的默認POST大小限制:maxPostSize =「」,但我想確定。謝謝! –
Sam