導航:首頁 > 編程語言 > 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相關的資料

熱點內容
繼電器連接單片機 瀏覽:14
寧明天西到崇左源碼 瀏覽:657
10g文件夾誤刪能恢復嗎 瀏覽:132
dos命令服務啟動 瀏覽:724
aws雲伺服器付費 瀏覽:171
水滴桿文件夾圖片 瀏覽:26
java百戰程序員25講 瀏覽:383
java斑馬 瀏覽:998
名片開發源碼 瀏覽:333
如何買便宜的雲伺服器 瀏覽:774
linux的命令提示符 瀏覽:979
機器人焊接手柄編程入門教程 瀏覽:653
方舟怎麼進入之前進去的伺服器 瀏覽:810
家有萌寶解壓 瀏覽:929
linuxc一站式學習pdf 瀏覽:20
奇思妙想pdf 瀏覽:992
租用裸金屬伺服器需要什麼資質 瀏覽:636
程序員熬夜習慣 瀏覽:203
劍網三文件夾越來越大怎麼辦 瀏覽:413
提示沒有ads編譯器 瀏覽:375