File file = new File(filename); if(!file.exists()){ throw new FileNotFoundException(filename); }
ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length()); BufferedInputStream in = null; try{ in = new BufferedInputStream(new FileInputStream(file)); int buf_size = 1; byte[] buffer = new byte[buf_size]; int len = 0; while(-1 != (len = in.read(buffer,0,buf_size))){ bos.write(buffer,0,len); }
public byte[] lgm(String fileName){ InputStream in = null; byte[] tempbytes = new byte[1]; try {
int byteread = 1; in = new FileInputStream(fileName); //读入多个字节到数组中,byteread为一次读入的字节数 while ((byteread = in.read(tempbytes)) != -1) { System.out.write(tempbytes, 0, byteread); }
File file = new File(filename); if(!file.exists()){ throw new FileNotFoundException(filename); }
ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length()); BufferedInputStream in = null; try{ in = new BufferedInputStream(new FileInputStream(file)); int buf_size = 1; byte[] buffer = new byte[buf_size]; int len = 0; while(-1 != (len = in.read(buffer,0,buf_size))){ bos.write(buffer,0,len); }
再有增强的jai_imageio.read读取图片
.write写图片到一个outputstream
你只要写到ByteArrayOutputStream就是了
为什么以下不行呢?
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private byte[] toByteArray(String filename) throws IOException{
File file = new File(filename);
if(!file.exists()){
throw new FileNotFoundException(filename);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length());
BufferedInputStream in = null;
try{
in = new BufferedInputStream(new FileInputStream(file));
int buf_size = 1;
byte[] buffer = new byte[buf_size];
int len = 0;
while(-1 != (len = in.read(buffer,0,buf_size))){
bos.write(buffer,0,len);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
try{
in.close();
}catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
return bos.toByteArray();
}Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public byte[] lgm(String fileName){
InputStream in = null;
byte[] tempbytes = new byte[1];
try {
int byteread = 1;
in = new FileInputStream(fileName);
//读入多个字节到数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return tempbytes;
}
下面的代码就是用到ByteArrayOutputStream的private byte[] toByteArray(String filename) throws IOException{
File file = new File(filename);
if(!file.exists()){
throw new FileNotFoundException(filename);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length());
BufferedInputStream in = null;
try{
in = new BufferedInputStream(new FileInputStream(file));
int buf_size = 1;
byte[] buffer = new byte[buf_size];
int len = 0;
while(-1 != (len = in.read(buffer,0,buf_size))){
bos.write(buffer,0,len);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
try{
in.close();
}catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
return bos.toByteArray();
}
public byte[] image2Bytes(String imagePath) throws Exception {
BufferedImage bu=ImageIO.read(new File(imagePath));
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
try {
ImageIO.write(bu, "bmp", imageStream);
} catch (Exception e) {
e.printStackTrace();
}
imageStream.flush();
byte[] tagInfo = imageStream.toByteArray();
return tagInfo;
}
我用了以下方法