导航:首页 > 编程语言 > 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