1.路径
<img src="balabala/123.jpg">
2.img标签指向servlet方法
<img src="/ImgServlet?imgId=00001" alt="picture">
主要流程为:获取id—>查询数据库得到图片路径—>重置response—>得到输出流—>设置contenttype—>读取文件流—>输入缓存区—>输出缓存区—>缓冲字节数
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String imgId = request.getParameter("imgId");
String imagePath = getImagePathFromDB(imgId);
response.reset();
OutputStream output = response.getOutputStream();
response.setContentType(GIF);
ServletContext context = getServletContext();
InputStream imageIn = context.getResourceAsStream(imagePath);
BufferedInputStream bis = new BufferedInputStream(imageIn);
BufferedOutputStream bos = new BufferedOutputStream(output);
byte data[] = new byte[4096];
int size = 0;
size = bis.read(data);
while (size != -1) {
bos.write(data, 0, size);
size = bis.read(data);
}
bis.close();
bos.flush();
bos.close();
output.close();
}
3.二进制流显示图片(不存到本地)
将图片转换成数据流,然后将数据流一对一将传进转化为数组的MemoryStream中,通过image控件显示图片
string path = "https://img3.doubanio.com/mpic/s8896281.jpg";
Uri url = new Uri(path);
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
Bitmap myImage = new Bitmap(webResponse.GetResponseStream());
MemoryStream ms = new MemoryStream();
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var p = new pictureUrl
{
pictureUrl1 = ms.ToArray()
};
db.pictureUrl.InsertOnSubmit(p);
db.SubmitChanges();
var pictures = from picture in db.pictureUrl select picture;
pictureUrl myPicture = pictures.First();
MemoryStream mymemorystream = new MemoryStream(myPicture.pictureUrl1.ToArray());
pictureBox1.Image = Image.FromStream(mymemorystream);
4.图片转化为base64字符串
将图片转化为字节字符串数组,并对其进行编码处理,再解码生成图片
扫描二维码关注公众号,回复:
4233879 查看本文章
![](/qrcode.jpg)
http://blog.sina.com.cn/s/blog_6400e5c50101qtr3.html
https://blog.csdn.net/hanchao5272/article/details/79261312