有的時候我們需要將文件通過base64進行編碼解碼來傳遞。
編碼過程:
public static void main(String[] args) {
InputStream inputStream =null;
File file = new File("D:\\index.htm");
try {
inputStream = new FileInputStream(file);
byte [] fileByte = new byte[inputStream.available()];
String fileString = new BASE64Encoder().encode(fileByte);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相反的解碼過程為:
public static void main(String[] args) {
byte [] fileByte = new BASE64Decoder().decodeBuffer(fileStream);
BufferedOutputStream stream = null;
File file = null;
try {
file = new File("D:\\decode.htm");
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(fileByte);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(stream!= null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。