為了方便測(cè)試,我們可以使用一個(gè)圖片編碼網(wǎng)站,將圖片進(jìn)行base64編碼
解密的代碼如下
public static String generateImage(String base64, String path) { ? ?// 解密 ? ?try { ? ? ?String savePath = "/**/imgtest/"; ? ? ?// 圖片分類(lèi)路徑+圖片名+圖片后綴 ? ? ?String imgClassPath = path.concat(UUID.randomUUID().toString()).concat(".jpg"); ? ? ? ?// 去掉base64前綴 data:image/jpeg;base64, ? ? ?base64 = base64.substring(base64.indexOf(",", 1) + 1); ? ? ?// 解密,解密的結(jié)果是一個(gè)byte數(shù)組 ? ? ?Base64.Decoder decoder = Base64.getDecoder(); ? ? ?byte[] imgbytes = decoder.decode(base64); ? ? ?for (int i = 0; i < imgbytes.length; ++i) { ? ? ? ?if (imgbytes[i] < 0) { ? ? ? ? ?imgbytes[i] += 256; ? ? ? } ? ? } ? ? ? ? ? ?// 保存圖片 ? ? ?OutputStream out = new FileOutputStream(savePath.concat(imgClassPath)); ? ? ?out.write(imgbytes); ? ? ?out.flush(); ? ? ?out.close(); ? ? ?// 返回圖片的相對(duì)路徑 = 圖片分類(lèi)路徑+圖片名+圖片后綴 ? ? ?return imgClassPath; ? } catch (IOException e) { ? ? ?return null; ? } }
因?yàn)閳D片的Base64字符串非常大,動(dòng)輒幾百K,所以不能直接使用String base64 = "${該圖片的base64串}"
進(jìn)行測(cè)試,否則編譯器會(huì)報(bào)錯(cuò)Java "constant string too long" compile error"
。這個(gè)錯(cuò)誤的出現(xiàn),是因?yàn)樽址A恐档拈L(zhǎng)度超過(guò)了65534,編譯期檢查沒(méi)通過(guò)。運(yùn)行時(shí)不存在這個(gè)限制,運(yùn)行時(shí)的內(nèi)存限制走的是堆內(nèi)存,跟CPU分配內(nèi)存相關(guān)。
?
解決方法1:如果將字符串預(yù)先存到一個(gè)文件里,使用的時(shí)候再?gòu)奈募镒x出來(lái),就不會(huì)有什么問(wèn)題文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-408476.html
// 從文件中讀取字符串 public static String getFileContent(FileInputStream fis, String encoding) throws IOException { ?try (BufferedReader br = new BufferedReader(new InputStreamReader(fis, encoding))) { ? ?StringBuilder sb = new StringBuilder(); ? ?String line; ? ?while ((line = br.readLine()) != null) { ? ? ?sb.append(line); ? } ? ?return sb.toString(); } } ? ? public static void main(String[] args) throws IOException { ?// 從txt文件中讀取base64字符串 ?FileInputStream fis = new FileInputStream("/Users/valor/workspace/imgtest/bigimg.txt"); ?String base64 = getFileContent(fis, "UTF-8"); ? ?String path = ""; ? ?// 將base64字符串翻譯成圖片 ?String fileName = generateImage(base64, path); ?System.out.println(fileName); }
解決方法2:或者如果我們是處于前后端數(shù)據(jù)交換的環(huán)境中,由于json對(duì)于string的長(zhǎng)度是沒(méi)有限制的,所以可以直接使用@ResponseBody
通過(guò)一個(gè)Bean,去接收這串base64。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-408476.html
// bean @Data @AllArgsConstructor @NoArgsConstructor public class ImgInfo { ? ?Long id; ? ?String base64; } ? // controller @RestController public class String2ImgController { ? ? ?@Autowired ? ?private String2ImgService imgService; ? ? ?@PostMapping("/img/base64") ? ?public String transferImg(@RequestBody ImgInfo imgInfo) { ? ? ? ? ?String base64 = imgInfo.getBase64(); ? ? // System.out.println(base64); ? ? ? ? ? ? ?// 去掉base64前綴 data:image/jpeg;base64, ? ? ? ?base64 = base64.substring(base64.indexOf(",", 1) + 1); ? ? ? ?// 解密,解密的結(jié)果是一個(gè)byte數(shù)組 ? ? ? ?Base64.Decoder decoder = Base64.getDecoder(); ? ? ? ?byte[] imgbytes = decoder.decode(base64); ? ? ? ?for (int i = 0; i < imgbytes.length; ++i) { ? ? ? ? ?if (imgbytes[i] < 0) { ? ? ? ? ? ?imgbytes[i] += 256; ? ? ? ? } ? ? ? } ? ? ? ? ? ?// 對(duì) byte 數(shù)組進(jìn)行你所需要的操作…… ? } }
到了這里,關(guān)于Java - 將base64編碼解碼成圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!