/** * copy file from inputstream * * @param is * @p合肥信息职业学院aram 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.clo( );out.flush( );out.clo( );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[] timagetobytearray(string filename){ filestream fs = new filest安宁中学ream(filename, filemode.open);int streamlength = (int)fs.l实习期驾驶证扣分新规定ength; byte[] image = new byte[streamlength];fs.read(image, 0, streamlength);fs.clo();return image; }public byte[]timagetobytearray(fileupload fileupload1){ stream stream = fileupload1.postedfile.inputstream;byte[] photo = new byte[fileupload1.postedfile.contentlength];stream.read(photo, 0, fileupload1.postedfile.contentlength);stream.clo();return photo;}
并转换成bytes[]或image图像文件
{ image image; memorystream mymemorystream = new memorystream(mybyte,0, mybyte.length);image = image.fromstream(mymemorystream)适合男生的工作;return image;}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 20:10:45,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c87d6ee41450aea39e302defe20a1040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:java如何实现图片转化为数据流.doc
本文 PDF 下载地址:java如何实现图片转化为数据流.pdf
留言与评论(共有 0 条评论) |