导航:首页 > 编程语言 > 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相关的资料

热点内容
反编译exe脚本 浏览:460
源码文件夹怎么编译到固件中 浏览:910
ERp打印服务器错误怎么弄 浏览:111
蚌端口u盘加密软件有哪些 浏览:178
前端如何认证服务器 浏览:554
linux切换db2用户命令 浏览:306
相片如何用电解压 浏览:905
硕士程序员去学校当老师 浏览:120
pythonstr提取到字典 浏览:818
程序员那么可爱有人看上陆漓了 浏览:876
php正则提取图片 浏览:103
pythonlinuxdjango 浏览:562
php中文返回乱码 浏览:89
宿舍装的电信怎么加密 浏览:745
为什么压缩文件解压后变少了 浏览:426
现在安卓充电器普遍是什么型号 浏览:714
9日均线36均线主图指标源码 浏览:349
程序员阿里文化完整版 浏览:98
早间新闻在哪个app上面可以看 浏览:954
工作啦app注册的信息怎么删去 浏览:378