Java中File與byte[]的互轉(zhuǎn)方式
更新時間:2024年05月30日 15:08:53 作者:看你家貓
這篇文章主要介紹了Java中File與byte[]的互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Java File與byte[]互轉(zhuǎn)
1、File 轉(zhuǎn)成 byte[]
public static byte[] getImageStream(String imageUrl,
HttpServletRequest request) {
ServletContext application = request.getSession().getServletContext();
String url = application.getRealPath("/")+imageUrl;
byte[] buffer = null;
File file = new File(url);
FileInputStream fis;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
if(file.exists()) {
file.delete();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
2、byte[] 轉(zhuǎn)成 File
public static void readBin2Image(byte[] byteArray, String targetPath) {
InputStream in = new ByteArrayInputStream(byteArray);
File file = new File(targetPath);
String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
if (!file.exists()) {
new File(path).mkdir();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java byte數(shù)組轉(zhuǎn)換成0和1的二進(jìn)制
可以使用以下代碼將Java中的byte數(shù)組轉(zhuǎn)換為0和1的二進(jìn)制字符串:
byte[] bytes = ...;
StringBuilder binary = new StringBuilder();
for (byte b : bytes) {
int val = b;
for (int i = 0; i < 8; i++) {
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
System.out.println(binary.toStri總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot前端傳遞數(shù)組后端接收兩種常用的方法
這篇文章主要給大家介紹了關(guān)于SpringBoot前端傳遞數(shù)組后端接收兩種常用的方法,文中通過代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-04-04
Java中使用HttpPost發(fā)送form格式的請求實現(xiàn)代碼
在Java中使用HttpPost發(fā)送form格式的請求,可以使用Apache HttpClient庫來實現(xiàn),這篇文章主要介紹了Java中使用HttpPost發(fā)送form格式的請求,本文給大家展示示例代碼,需要的朋友可以參考下2023-08-08
SpringBoot使用Redis對用戶IP進(jìn)行接口限流的項目實踐
本文主要介紹了SpringBoot使用Redis對用戶IP進(jìn)行接口限流,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringBoot?整合數(shù)據(jù)源的具體實踐
本文主要介紹了SpringBoot?整合數(shù)據(jù)源的具體實踐,利用?Spring?Boot?的自動配置和簡化的注解來簡化數(shù)據(jù)源配置工作,從而更專注于應(yīng)用程序的業(yè)務(wù)邏輯開發(fā),感興趣的可以了解一下2023-11-11
通過Java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)操作流程
java相對于其他語言(例如c,c++等)連接數(shù)據(jù)庫要方便得多,那么如何連接呢?下面這篇文章主要給大家介紹了關(guān)于通過Java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)操作流程,需要的朋友可以參考下2023-03-03
MyBatis學(xué)習(xí)教程之開發(fā)Dao的方法教程
這篇文章主要給大家介紹了關(guān)于MyBatis開發(fā)Dao的相關(guān)資料,使用Mybatis開發(fā)Dao,通常有兩個方法,即原始Dao開發(fā)方法和Mapper接口開發(fā)方法。文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-07-07
springtask 的使用方法和 cron 表達(dá)式解析
這篇文章主要介紹了springtask 的使用方法和 cron 表達(dá)式解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10

