java 二进制 base64编码_java 按字节读写二进制文件(Base64编码解码)
最近在做項目時遇到這樣一個需求:依次讀取本地文件夾里所有文件的內容,轉為JSON,發送到ActiveMQ的消息隊列, 然后從MQ的消息隊列上獲取文件的信息,依次寫到本地。常見的文件類型,比如.txt 和.png等文件的讀寫并不難。但是,我剛才所提到的需求,如果用常規的方法去讀寫,比如按字節讀取文件內容,轉為字符串,再轉為JSON發送到MQ的隊列,然后從MQ獲取原文件信息寫到文件,就會發現寫出來的文件和文件不一樣。我的解決方法是:按字節讀取原文件,轉為字節數組,將字節數組轉為Base64編碼格式的字符串,然后轉為JOSN發送到MQ,然后從MQ獲取到JOSN,將文件內容通過Base64解碼為字符數組,寫文件到某個路徑。以下為代碼:
測試類 Test.class
/**
* @Desc: 測試類
* @Date: 2016/7/1
* @Version: 1.0
* @Author: lzy
*/
public class Test {
public static void main(String[] args){
String fileName="JUBE99EGRR311800"; //文件名:測試文件是沒有后綴名的二進制
String fileReadPath="d:/read/"; //文件所在文件夾
String filePath=fileReadPath+fileName; //文件路徑
//工具類
FileUtil fileUtil=new FileUtil();
//按字節讀取文件內容,并轉換為Base64編碼字符串
String fileJsonStr=fileUtil.fileToJson(fileName,filePath);
//文件內容Base64解碼,按字節寫文件
fileUtil.writeFile(fileJsonStr,"d:/write/");
}
}123456789101112131415161718192021222324
FileUtil.class 文件轉換工具類
/**
* @Desc: 文件內容轉換工具類
* @Date: 2016/7/1
* @Version: 1.0
* @Author: lzy
*/
public class FileUtil {
/**
* 文件內容轉為 Base64 編碼的 JSON
* @param fileName 文件名
* @param filePath 文件路徑
* @return
*/
public String fileToJson(String fileName,String filePath){
String fileContentJson=""; //文件內容轉JSON
try {
if(null!=filePath && !filePath.equals("")){
if(null!=fileName && !fileName.equals("")){
//將文件內容轉為字節數組
byte[] fileByte=toByteArray(filePath);
//將字節數組轉為Base64編碼
String fileContent=ByteToBase64(fileByte);
FileEntity fileEntity=new FileEntity();
fileEntity.setFileName(fileName);
fileEntity.setFileContent(fileContent);
//實體轉JSON
fileContentJson = FileEntitytoJSON(fileEntity);
}
}
}catch (Exception e){
Log.error("fileToJson error",e);
}
return fileContentJson;
}
/**
* 將文件轉成字節數組
* @param filePath
* @return
*/
public byte[] toByteArray(String filePath){
ByteArrayOutputStream bos=null;
BufferedInputStream in = null;
try {
File f = new File(filePath);
if(f.exists()){
in = new BufferedInputStream(new FileInputStream(f));
bos = new ByteArrayOutputStream((int) f.length());
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
//return bos.toByteArray();
}
} catch (IOException e) {
Log.error("toByteArray() Exception", e);
} finally {
try {
in.close();
bos.close();
} catch (IOException e) {
Log.error("toByteArray() Exception",e);
}
}
return bos.toByteArray();
}
/**
* Base64加密
* @param b
* @return
*/
public String ByteToBase64(byte[] b) {
String str="";
if(null!=b){
BASE64Encoder encoder = new BASE64Encoder();
str=encoder.encode(b);
}
return str;
}
/**
* Base64解密
* @param str
* @return
*/
public byte[] Base64toByte(String str) {
byte[] b = new byte[0];
BASE64Decoder decoder = new BASE64Decoder();
try {
if(null!=str && !str.equals("")){
b = decoder.decodeBuffer(str);
}
} catch (IOException e) {
Log.error("Base64toByte() Exception",e);
}
return b;
}
/**
* 實體轉JSON
* @param fileEntity
* @return
*/
public String FileEntitytoJSON(FileEntity fileEntity) {
String str="";
if(null!=fileEntity){
JSONObject json = JSONObject.fromObject(fileEntity);
str = json.toString();
}
return str;
}
/**
* JSON轉實體
* @param msg
* @return
*/
public FileEntity JSONtoFileEntity(String msg) {
FileEntity fileEntity=new FileEntity();
if(null!=msg && !msg.equals("")){
JSONObject obj = new JSONObject().fromObject(msg);
fileEntity = (FileEntity) JSONObject.toBean(obj, FileEntity.class);
}
return fileEntity;
}
/**
* 寫文件
* @param fileJson json
* @param filePath 文件路徑
*/
public void writeFile(String fileJson,String filePath){
FileOutputStream fos=null;
try{
if(null!=fileJson && !fileJson.equals("")){
if(null!=filePath && !filePath.equals("")){
//json轉為文件實體
FileEntity fileEntity=JSONtoFileEntity(fileJson);
//Base64文件內容解碼
byte[] b=Base64toByte(fileEntity.getFileContent());
File file=new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
fos = new FileOutputStream(filePath + File.separator +fileEntity.getFileName());
fos.write(b);
}
}
}catch (Exception e){
Log.error("write Exception:",e);
}finally {
try {
fos.close();
} catch (IOException e) {
Log.error("fos.close() Exception:", e);
}
}
}
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
FileEntity.class 文件實體類
/**
* @Desc:文件信息實體
* @Date: 2016/6/29
* @Version: 1.0
* @Author: lzy
*/
public class FileEntity {
private String fileName; //文件名
private String fileContent; //文件內容
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileContent() {
return fileContent;
}
public void setFileContent(String fileContent) {
this.fileContent = fileContent;
}
}
總結
以上是生活随笔為你收集整理的java 二进制 base64编码_java 按字节读写二进制文件(Base64编码解码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汽修技工学校学费一年多少钱?
- 下一篇: 外地车,有进京证,限号是24小时内走不了