通過form表單上傳圖片時,有時候web容器對文件大小的限制會影響我們上傳。這時,前端頁面可以考慮將圖片轉換成base64串來實現上傳。 圖片與Base64的互轉,其實就是利用了文件流與Base64的互轉。 文件轉換成Base64字元串:讀取文件流,放到byte數組裡,對byte數組進行Base64 ...
通過form表單上傳圖片時,有時候web容器對文件大小的限制會影響我們上傳。這時,前端頁面可以考慮將圖片轉換成base64串來實現上傳。
圖片與Base64的互轉,其實就是利用了文件流與Base64的互轉。
文件轉換成Base64字元串:讀取文件流,放到byte數組裡,對byte數組進行Base64編碼,返回字元串。
Base64串轉換成文件:對Base64串進行解碼,得到byte數組,利用文件輸出流將byte數據寫入到文件。
Talk is cheap, show me the code。直接上代碼:
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; public class ImageBase64Converter { /** * 本地文件(圖片、excel等)轉換成Base64字元串 * * @param imgPath */ public static String convertFileToBase64(String imgPath) { byte[] data = null; // 讀取圖片位元組數組 try { InputStream in = new FileInputStream(imgPath); System.out.println("文件大小(位元組)="+in.available()); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對位元組數組進行Base64編碼,得到Base64編碼的字元串 BASE64Encoder encoder = new BASE64Encoder(); String base64Str = encoder.encode(data); return base64Str; } /** * 將base64字元串,生成文件 */ public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if (!dir.exists() && dir.isDirectory()) {//判斷文件目錄是否存在 dir.mkdirs(); } BASE64Decoder decoder = new BASE64Decoder(); byte[] bfile = decoder.decodeBuffer(fileBase64String); file = new File(filePath + File.separator + fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); return file; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }
test:
public static void main(String[] args) { long start = System.currentTimeMillis(); String imgBase64Str= ImageBase64Converter.convertFileToBase64("C:\\Users\\zhangguozhan\\Pictures\\科技\\liziqi-李子柒爆紅.jpg"); // System.out.println("本地圖片轉換Base64:" + imgBase64Str); System.out.println("Base64字元串length="+imgBase64Str.length()); ImageBase64Converter.convertBase64ToFile(imgBase64Str,"C:\\Users\\zhangguozhan\\Pictures\\科技","test.jpg"); System.out.println("duration:"+(System.currentTimeMillis()-start)); start=System.currentTimeMillis(); String fileBase64Str= ImageBase64Converter.convertFileToBase64("C:\\Users\\zhangguozhan\\Pictures\\科技\\PayOrderList200109075516581.xlsx"); // System.out.println("本地excel轉換Base64:" + fileBase64Str); System.out.println("size="+fileBase64Str.length()); ImageBase64Converter.convertBase64ToFile(fileBase64Str,"C:\\Users\\zhangguozhan\\Pictures\\科技","test.xlsx"); System.out.println("duration:"+(System.currentTimeMillis()-start)); }
執行結果:
文件大小(位元組)=2820811 Base64字元串length=3860058 duration:244 文件大小(位元組)=25506 size=34902 duration:10
提醒一下:獲取文件的大小是用FileInputStream實例的available()方法哦,用File實例的length()返回的是0。
如下圖,測試方法里圖片文件“liziqi-李子柒爆紅.jpg”的大小正是2820811位元組 ÷1024=2755KB ÷1024=2.68M