導航:首頁 > 編程語言 > java實現base64

java實現base64

發布時間:2024-07-03 00:28:29

java中如何用base64解碼圖片,並返回圖片,不保存。

給你發個我以前的工具類吧、
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImageChange {
/**
* 從path這個地址獲取一張圖纖沒臘片然後轉為base64碼
* @param imgName 圖片的名字 如:123.gif(是帶後綴的)
* @param path 123.gif圖片存放的路徑
* @return
* @throws Exception
*/
public static String getImageFromServer(String imgName,String path)throws Exception{
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
File f = new File(path+imgName);
if(!f.exists()){
f.createNewFile();
}
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "gif", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
}

/**
* 將一個base64轉換成圖片保存在 path 文件夾下毀滑 名為imgName.gif
* @param base64String
* @param path 是一個文件夾路徑
* @param imgName 圖片名字(沒有後綴)
* @throws Exception
*/
public static String savePictoServer(String base64String,String path,String imgName)throws Exception{
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream s = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(s);

Date timeCur = new Date();
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
String strYY = fmtYY.format(timeCur);
String strMM = fmtMM.format(timeCur);
String strDD = fmtDD.format(timeCur);

String realPath = path+"/"+strYY+"/"+strMM+"/察芹"+strDD;

File dir=new File(realPath);
if(!dir.exists()){
dir.mkdirs();
}
String fileName=path+"\\"+strYY+"\\"+strMM+"\\"+strDD +"\\"+imgName+".gif";
File w2 = new File(fileName);//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管輸出什麼格式圖片,此處不需改動

return fileName;
}

public static void main(String[] args) throws Exception {
System.out.println(getImageFromServer("001001.gif","d:"));
}
}

② 求java加密源代碼(MD5,base64)

加密什麼加密字元串嗎,我這里有md5的演算法
public final static String MD5(String pwd) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] strTemp = pwd.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}

③ 在Java中如何進行BASE64編碼和解碼

importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;

//將s進行BASE64編碼
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}

//將BASE64編碼的字元串s進行解碼
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}

④ 怎麼用JAVA對一個文件進行base64編碼

JAVA對一個文件進行base64編碼
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;

//將s進行BASE64編碼
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}

//將BASE64編碼的字元串s進行解碼
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}

⑤ javapdf格式的base編碼轉換jpg格式的base64

在格式遲臘。
1、首先,將Java PDF格式的Base64編碼啟滑碼字元串轉換為byte數組。
2、然後,旁閉使用Java的ImageIO類將byte數組轉換為BufferedImage對象。
3、接下來,將BufferedImage對象轉換為JPG格式的Base64編碼字元串。

閱讀全文

與java實現base64相關的資料

熱點內容
php轉化為整數 瀏覽:764
單個程序員做好軟體的關鍵是 瀏覽:244
devc編譯正確但調試不出來 瀏覽:57
單片機應用鍵盤 瀏覽:903
代理伺服器為什麼qq上不去 瀏覽:381
cad命令無法使用 瀏覽:946
previewpdf 瀏覽:186
揚州享車app在哪裡辦公 瀏覽:362
python資料庫操作封裝類 瀏覽:953
如何查找dns伺服器 瀏覽:410
php保留換行 瀏覽:81
遙控器件加密 瀏覽:780
魔獸爭霸命令大全 瀏覽:208
安卓系統為什麼容易中毒 瀏覽:194
模擬機app有什麼用 瀏覽:548
php時間控制項 瀏覽:589
一個文件夾復制的東西日期變了 瀏覽:121
pythonappapi 瀏覽:148
哈哈哈程序員之間的鄙視 瀏覽:271
vbaexe反編譯 瀏覽:357