接口上傳base64編碼圖片


 1 package com.*.util;
 2 
 3 import java.io.FileInputStream;
 4 
 5 
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 import java.util.Date;
11 
12 import Decoder.BASE64Decoder;
13 import Decoder.BASE64Encoder;
14 
15 public class Base64ImgUtils {
16     public static void main(String[] args) {
17         String strImg = GetImageStr();
18         System.out.println(strImg);        
19         GenerateImage(strImg);
20     }
21 
22     /**
23      * 圖片轉化成base64字符串
24      * GetImageStr
25      * 2016年8月31日下午3:37:40   
26      * @param
27      * @return
28      */
29     public static String GetImageStr() {// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
30         String imgFile = "D:/486e407765c21edd9cbffca69717efb1.jpg";// 待處理的圖片
31         InputStream in = null;
32         byte[] data = null;
33         // 讀取圖片字節數組
34         try {
35             in = new FileInputStream(imgFile);
36             data = new byte[in.available()];
37             in.read(data);
38             in.close();
39         } catch (IOException e) {
40             e.printStackTrace();
41         }
42         // 對字節數組Base64編碼
43         BASE64Encoder encoder = new BASE64Encoder();
44         String imghead="data:image/jpg;base64,";//
45         return imghead+encoder.encode(data);// 返回Base64編碼過的字節數組字符串
46     }
47 
48     /**
49      * base64字符串轉化成圖片
50      * GenerateImage
51      * 2016年8月31日下午3:33:12   
52      * @param
53      * @return
54      */
55     public static String GenerateImage(String imgStr) { // 對字節數組字符串進行Base64解碼並生成圖片
56         if (imgStr == null){ // 圖像數據為空
57             return "";
58         }
59         String classPath = new Base64ImgUtils().getClass().getResource("").getPath();
60         String path = classPath.substring(0, classPath.indexOf("WEB-INF"));
61         System.out.println(path);
62         BASE64Decoder decoder = new BASE64Decoder();
63         try {
64             String imghead=imgStr.substring(0,imgStr.indexOf(";")).replace("data:image/", ".");//獲取圖片擴展名
65             imgStr=imgStr.substring(imgStr.indexOf(",")+1);//圖片內容
66                                    
67             // Base64解碼
68             byte[] b = decoder.decodeBuffer(imgStr);
69             for (int i = 0; i < b.length; ++i) {
70                 if (b[i] < 0) {// 調整異常數據
71                     b[i] += 256;
72                 }
73             }
74             // 生成jpeg圖片
75             String filename="upload/"+new Date().getTime()+imghead;//名稱
76             String imgFilePath =path+"/"+filename;// 新生成的圖片
77             OutputStream out = new FileOutputStream(imgFilePath);
78             out.write(b);
79             out.flush();
80             out.close();
81             return filename;
82         } catch (Exception e) {
83             return "";
84         }
85     }
86 
87     
88 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM