我已經從html5畫布中檢索到base64數據uri。在我的servlet中,我想解碼數據uri並將其用作輸入流,如下面的「xxx」所示。以下代碼是爲了將HTML5畫布中的圖像發佈到我的Facebook帳戶中。我正在使用restfb。如何使用Image中的Data URI作爲InputStream?
FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("test.jpeg", getClass().getResourceAsStream("xxx")),
Parameter.with("message", "Test"));
我該如何做到這一點?謝謝。
更新靠近但仍然不工作!
在我的jsp:
var d = document.getElementById('img').src;
window.location.href = "upload?src=" + d;
在我的servlet:
String d = req.getParameter("src");
String head = "data:image/jpeg;base64,";
String base64 = d.substring(head.length()-1);
byte[] buf = DatatypeConverter.parseBase64Binary(base64);
ByteArrayInputStream is = new ByteArrayInputStream(buf);
FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("test.jpeg", is),
Parameter.with("message", "Test"));
有沒有在我的編碼任何錯誤,因爲它似乎在servlet中的某處打錯誤。我無法查看它在服務器上運行的錯誤。
好的,我認爲這個問題可能在於查詢字符串從jsp到我的servlet。在將它設置爲查詢字符串之前,我需要先對base64進行編碼嗎?我曾嘗試將我的base64轉換爲使用在線轉換器的圖像,它顯示圖像很好。 – Sky
我設法解決它,確實是我的查詢字符串的問題。我已經創建了一個隱藏的元素,用於在提交給我的servlet之前存儲數據。感謝您的指導,它有幫助!乾杯。 – Sky