码迷,mamicode.com
首页 > 其他好文 > 详细

base64

时间:2019-09-28 23:12:35      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:test   public   catch   exists   base64   array   record   nta   oid   

package mytest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.List;


public class baseImage {
public static void main(String[] args) {
List str = printAllFileNames("D:\\Script_Recorder_Cache_Filter");
System.out.println(str.size());
System.out.println(str.get(0));
System.out.println(str.get(1));
System.out.println(str.get(2));
System.out.println(str.get(3));

}
public static List printAllFileNames(String path) {
List<String> str = new ArrayList<>();
List<String> fileList = new ArrayList<String>();
if (null != path) {
File file = new File(path);
if (file.exists()) {
File[] list = file.listFiles();
if(null != list){
for (File child : list) {
String absolutePath = child.getAbsolutePath();

fileList.add(absolutePath);

String str2 = GetImageStr(absolutePath);
str.add(str2);

}
}
}
}
return str;

}

public static String GetImageStr(String imgFilePath) {// 灏嗗浘鐗囨枃浠惰浆鍖栦负瀛楄妭鏁扮粍瀛楃涓诧紝骞跺鍏惰繘琛孊ase64缂栫爜澶勭悊

byte[] data = null;

// 璇诲彇鍥剧墖瀛楄妭鏁扮粍
try {

InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}

// 瀵瑰瓧鑺傛暟缁凚ase64缂栫爜
Encoder encoder = Base64.getEncoder();
String string = encoder.encodeToString(data);

return string;
// 杩斿洖Base64缂栫爜杩囩殑瀛楄妭鏁扮粍瀛楃涓?
}

public static boolean GenerateImage(String imgStr, String imgFilePath) {// 瀵瑰瓧鑺傛暟缁勫瓧绗︿覆杩涜Base64瑙g爜骞剁敓鎴愬浘鐗?
if (imgStr == null) // 鍥惧儚鏁版嵁涓虹┖
return false;
Decoder decoder = Base64.getDecoder();
try {
// Base64瑙g爜
byte[] bytes = decoder.decode(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 璋冩暣寮傚父鏁版嵁
bytes[i] += 256;
}
}
// 鐢熸垚jpeg鍥剧墖
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}

base64

标签:test   public   catch   exists   base64   array   record   nta   oid   

原文地址:https://www.cnblogs.com/senxin/p/1w.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!