------ Solution ---------------------------------------- ----
using FileOutputStream, the following code , but I do not do effectively verified
import java.io.*;
/**
模拟一个jpeg位图到byte[]
*/
class Image2Buff
{
static byte[] image2Bytes(String imgSrc) throws Exception
{
FileInputStream fin = new FileInputStream(new File(imgSrc));
//可能溢出,简单起见就不考虑太多,如果太大就要另外想办法,比如一次传入固定长度byte[]
byte[] bytes = new byte[fin.available()];
//将文件内容写入字节数组,提供测试的case
fin.read(bytes);
fin.close();
return bytes;
}
}
class Buff2Image
{
/**
@param b 传入的byte
@param tagSrc 目标文件名
*/
static void buff2Image(byte[] b,String tagSrc) throws Exception
{
FileOutputStream fout = new FileOutputStream(tagSrc);
//将字节写入文件
fout.write(b);
fout.close();
}
}
//本质上实现了copy 命令
public class ImageBufTest
{
public static void main(String[] args) throws Exception
{
//先模拟一个图形byte[]
byte[] b = Image2Buff.image2Bytes("c:/1.jpg");
//存为文件
Buff2Image.buff2Image(b,"c:/test.jpg");
System.out.println("Hello World!");
}
}
------ For reference only ----------------------------------- ----
but my byte array is obtained from the network, not read directly from a now have Jpg file.
I pass the byte array object after following fout , JPG files obtained can not be displayed when you open the preview ! ! ! !
FileOutputStream fout = new FileOutputStream (tagSrc); / / bytes written to the file fout.write (b);
fout.close ();
------ For reference only ---------------------------------- -----
lacks distinction , since it is a byte [], you can use FileOutputStream
If you do not get the picture , it should be byte [] not read it ? Here to read your forum avatar by URL way
also get on the network , the essence is still the same thing ,
import java.io.*;
import java.net.*;
/**
模拟一个jpeg位图到byte[]
*/
class Image2Buff
{
static byte[] image2Bytes(String imgSrc) throws Exception
{
FileInputStream fin = new FileInputStream(new File(imgSrc));
//可能溢出,简单起见就不考虑太多,如果太大就要另外想办法,比如一次传入固定长度byte[]
byte[] bytes = new byte[fin.available()];
//将文件内容写入字节数组,提供测试的case
fin.read(bytes);
fin.close();
return bytes;
}
//从网上读取图片地址,存入本地
static byte[] image2BytesFromURL(String address) throws Exception
{
URL url = new URL(address);
InputStream is = url.openStream();
byte[] bytes = new byte[is.available()];
//将文件内容写入字节数组,提供测试的case
is.read(bytes);
return bytes;
}
}
class Buff2Image
{
/**
@param b 传入的byte
@param tagSrc 目标文件名
*/
static void buff2Image(byte[] b,String tagSrc) throws Exception
{
FileOutputStream fout = new FileOutputStream(tagSrc);
//将字节写入文件
fout.write(b);
fout.close();
}
}
public class ImageBufTest
{
public static void main(String[] args) throws Exception
{
//先模拟一个图形byte[]
byte[] b = Image2Buff.image2Bytes("c:/1.jpg");
//存为文件
Buff2Image.buff2Image(b,"c:/test.jpg");
//通过url 读取图片得到bytes,复制你的头像到本地
byte[] b2 = Image2Buff.image2BytesFromURL("http://avatar.profile.csdn.net/D/C/3/1_hjx_code.jpg");
Buff2Image.buff2Image(b2,"c:/test2.jpg");
System.out.println("Hello World!");
}
}
------ For reference only ----------------------------------- ----
landlord copy of this document is the same with nothing more than read write process flow streams . From the network constantly reading the image bytes written to the image file contents continuously until the completion of the read and write it wants
------ For reference only --------- ------------------------------
ah , your code does work , but also very standardized.
may really I did not get to the cause of all the bytes of data .
thank you.
------ For reference only ---------------------------------- -----
brother upstairs test code does not know not, tell the landlord Tieshanglai
upstairs code image2Bytes is obviously wrong
you return but only one has a length byte array readable stream that white is an all zero byte array
not the contents of the stream , and then you put the empty array write files , apparently failed .
landlord should do
byte [] buf = new byte [in.available ()]; data stream that is used to keep it in just the first composed entirely of 0
int i = 0;
while ((i = in.read (buf))! = -1) {/ / this time is the flow of the specific content of buf
out.write (buf, 0, i);
}
out.close ();
没有评论:
发表评论