想請問一下
我用JS fileReade讀了一些圖片
想直接送回servlet解碼並存於資料庫中
可是一直解碼失敗
實在看不出個端倪
想請求各位大大解惑 QQ
內文的jStr片段為(太長了故只貼個片段..)
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANIAAADcCAYAAAAFk/8yAAAKL2lDQ1BJQ0MgcHJvZmlsZQAAeNqdlndUVNcWh8 9d3qhzTDSGXqTLjCA9C4gHQRRGGYGGMoAwwxNbIioQEQREQFFkKCAAaOhSKyIYiEoqGAPSBBQYjCKqKhkRtZKfHl57 Xl98e939pn73P32XuftS4AJE8fLi8FlgIgmSfgB3o401eFR9Cx/QAGeIABpgAwWempvkHuwUAkLzcXerrICfyL3gwBSPy ZejpT6eD/0/SrFS AADIX8TmbE46S8T5Ik7KFKSK7TMipsYkihlGiZkvSlDEcmKOW Sln30W2VHM7GQeW8TinFPZyWwx94h4e4aQI2LER8QFGVxOpohvi1gzSZjMFfFbcWwyh5kOAIoktgs4rHgRm4iYxA8OdBHxcgBwpLgvOOYLFnCyBOJDuaSkZvO5cfECui5Lj25qbc2ge3IykzgCgaE/k5XI5LPpLinJqUxeNgCLZ/4sGXFt6aIiW5paW1oamhmZflGo/7r4NyXu7SK9CvjcM4jW94ftr/xS6gBgz
public boolean add(List<Photos> photos,String al_no) throws UnsupportedEncodingException{
Base64.Decoder decoder = Base64.getDecoder();
for(Photos p : photos){
p.setAl_no(al_no);
String jStr = p.getSrc().replace("\\s+", "");
jStr = jStr.split(",")[1];
byte[] b = decoder.decode(jStr.getBytes("utf-8"));
p.setPhoto(b);
}
PhotosDAO dao = new PhotosDAO();
boolean result = dao.executeInsert(photos);
return result;
}
補上錯誤碼
七月 30, 2017 12:55:43 上午 org.apache.catalina.core.StandardWrapperValve invoke
嚴重: Servlet.service() for servlet [com.album.controller.PhotosActionCtrl] in context with path [/BA102G4] threw exception
java.lang.IllegalArgumentException: Illegal base64 character 20
at java.util.Base64$Decoder.decode0(Unknown Source)
at java.util.Base64$Decoder.decode(Unknown Source)
String jStr = p.getSrc().replace("\\s+", "");
改成
String jStr = p.getSrc().replaceAll("\\s+","");
試試看