这篇文章主要介绍了java怎么实现图片转化为数据流的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java怎么实现图片转化为数据流文章都会有所收获,下面我们一起来看看吧。
/**
* Copy file from inputStream
*
* @param is
* @param f2
* @throws Exception
*/
public static void copyFileFromInputStream( InputStream is, File f2 ) throws Exception {
int length = 2097152;
FileOutputStream out = new FileOutputStream( f2 );
byte[] buffer = new byte[length];
while (true) {
int ins = is.read( buffer );
if ( ins == -1 ) {
is.close( );
out.flush( );
out.close( );
break;
}
out.write( buffer , 0 , ins );
}
}
String image = "XXX.jpg";
File imageFile= new File(System.getProperty("java.io.tmpdir"), image); //System.getProperty("java.io.tmpdir")是获取操作系统缓存的临时目录
copyFileFromInputStream(XXXX.class.getResourceAsStream("images/" + image),imageFile);
// 系统会读取XXX.class路径中images文件夹下的xxx.jpg文件,将其转换为数据流
在学习期间,把开发过程经常用到的一些代码段做个备份,下边代码内容是
应该能对各朋友也有用处
public byte[] SetImageToByteArray(string fileName)
{ FileStream fs = new FileStream(fileName, FileMode.Open);
int streamLength = (int)fs.Length; byte[] image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image; }
public byte[]
SetImageToByteArray(FileUpload FileUpload1)
{ Stream stream = FileUpload1.PostedFile.InputStream;
byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
stream.Close();
return photo;
}
并转换成bytes[]或Image图像文件
{ Image image; MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length);
image = Image.FromStream(mymemorystream);
return image;
}
关于“java怎么实现图片转化为数据流”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“java怎么实现图片转化为数据流”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。