图片上传前端的几种方法

 

 

 

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字符串

将图片转化为字节字符串数组,并对其进行编码处理,再解码生成图片

 

 

http://blog.sina.com.cn/s/blog_6400e5c50101qtr3.html

https://blog.csdn.net/hanchao5272/article/details/79261312

http://www.cnblogs.com/JsonZhangAA/p/5568575.html

http://www.cnblogs.com/JsonZhangAA/p/5574685.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值