1、将File、FileInputStream 转换为byte数组:
File file = new File("file.txt");
InputStream input = new FileInputStream(file);
byte[] byt = new byte[input.available()];
input.read(byt);
2、将byte数组转换为InputStream:
byte[] byt = new byte[1024];
InputStream input = new ByteArrayInputStream(byt);
3、将byte数组转换为File:
File file = new File('');
OutputStream output = new FileOutputStream(file);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
bufferedOutput.write(byt);
-
public static byte[] getStreamBytes(InputStream is) throws Exception {
-
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream();
-
byte[]
buffer
=
new
byte[1024];
-
int
len
=
0
;
-
while ((
len
=
is
.read(buffer)) != -1) {
-
baos.write(buffer, 0, len);
-
}
-
byte[]
b
=
baos
.toByteArray();
-
is.close();
-
baos.close();
-
return b;
-
}
-
default byte[] readFileBytes(InputStream is){
-
byte[]
data
=
null
;
-
try {
-
if(is.available()==0){//严谨起见,一定要加上这个判断,不要返回data[]长度为0的数组指针
-
return data;
-
}
-
data
=
new
byte[is.available()];
-
is.read(data);
-
is.close();
-
return data;
-
} catch (IOException e) {
-
LogCore.BASE.error("readFileBytes, err", e);
-
return data;
-
}
-
}
-
public
FileInputStream
byte
To
File
(
byte
[]
byte
s, String
file
Name) {
File
file
= new
File
(
file
Name);
InputStream
是抽象类,只是
InputStream
读取的是字节,使用的参数是
byte
数组
(
byte
[])
读取文件的输入流:
FileInputStream
是节点流需要直接
和
指定文件关联。
方法摘要:
available()
返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
close()
关闭此输入流并释放...
package com.openailab.oascloud.
file
.util;
import com.openailab.oascloud.
file
.common.consts.BootstrapConst;
import java.io.*;
import java.nio.Mapped
Byte
Buffer;
im...
public
FileInputStream
byte
To
File
(
byte
[]
byte
s, String
file
Name) {
File
file
= new
File
(
file
Name);
FileInputStream
fileInputStream
= null;
try {
1、将
File
、
FileInputStream
转
换为
byte
数组
:
File
file
= new
File
("test.txt");
InputStream
input = new
FileInputStream
(
file
);
byte
[] byt = new
byte
[input.available()];
input.read(byt);
2、将
byte
数组
转
换为Input...
File
file
= new
File
("test.txt");
InputStream
input = new
FileInputStream
(
file
);
byte
[]
byte
s = new
byte
[input.available()];
input.read(
byte
s);
byte
[]
转
换为
InputStream
byte
[]
byte
s = new
byte
[1024];
InputStream
input = new
同样的用springmvc获取的图片文件,有两种格式
Byte
Array
InputStream
FileInputStream
先记一下 ,自己猜想应该
和
图片的存储方式有关?还是格式?文件编码?
用split截取.需要用split("[.]")
可以使用
Byte
ArrayOutputStream来将
InputStream
转
换成
byte
数组
。具体步骤如下:
1. 创建一个
Byte
ArrayOutputStream对象。
2. 创建一个
byte
数组
作为缓冲区。
3. 使用while循环读取
InputStream
中的数据,每次读取数据后将数据写入
Byte
ArrayOutputStream中。
4. 读取完毕后,将
Byte
ArrayOutputStream中的数据
转
换成
byte
数组
即可。
以下是示例代码:
```java
public static
byte
[]
inputStream
To
Byte
Array(
InputStream
inputStream
) throws IOException {
Byte
ArrayOutputStream
byte
ArrayOutputStream = new
Byte
ArrayOutputStream();
byte
[] buffer = new
byte
[4096];
int
byte
sRead;
while ((
byte
sRead =
inputStream
.read(buffer)) != -1) {
byte
ArrayOutputStream.write(buffer, 0,
byte
sRead);
return
byte
ArrayOutputStream.to
Byte
Array();
使用时只需要传入需要
转
换的
InputStream
对象即可,例如:
```java
InputStream
inputStream
= new
FileInputStream
("
file
.txt");
byte
[]
byte
s =
inputStream
To
Byte
Array(
inputStream
);
Springcloud+Nacos+Seata: NettyClientChannelManager : 0304 register RM failed或者0101 can not connect
Ghost-Unison:
Docker - 在容器中搭建运行FTP服务器(vsftpd)
Cadence_D:
Docker - 在容器中搭建运行FTP服务器(vsftpd)
Hubeoo: