您现在的位置是:首页 > 文章详情

图片地址是BASE64的图片上传(头像上传)

日期:2018-05-12点击:371
 1 package controller;  2  3 import java.io.FileOutputStream;  4 import java.io.IOException;  5 import java.io.OutputStream;  6  7 import javax.servlet.http.HttpServletRequest;  8 import sun.misc.BASE64Decoder;  9 import org.springframework.stereotype.Controller; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.ResponseBody; 12 13 @Controller 14 public class UsersMessageController { 15 private String path; 16 @RequestMapping("insert") 17 public String insert(String file,HttpServletRequest request) throws IOException{ 18 path= request.getSession().getServletContext().getRealPath("image/head"); 19 System.out.println(path); 20 String [] base64s=file.split(","); 21 /*System.out.println("OriginalFilename: " + file.getOriginalFilename()); 22  System.out.println("InputStream: " + file.getInputStream());*/ 23 boolean t=GenerateImage(base64s[1]); 24 return "ceshi"; 25  } 26 public boolean GenerateImage(String imgStr) 27 { //对字节数组字符串进行Base64解码并生成图片  28 if (imgStr == null) //图像数据为空  29 return false; 30 BASE64Decoder decoder = new BASE64Decoder(); 31 try 32  { 33 //Base64解码  34 byte[] b = decoder.decodeBuffer(imgStr); 35 for(int i=0;i<b.length;++i) 36  { 37 if(b[i]<0) 38 {//调整异常数据  39 b[i]+=256; 40  } 41  } 42 //生成jpeg图片  43 String imgFilePath = path+"\\1.jpg";//新生成的图片  44 OutputStream out = new FileOutputStream(imgFilePath); 45 out.write(b); 46 out.flush(); 47 out.close(); 48 return true; 49  } 50 catch (Exception e) 51  { 52 return false; 53  } 54  } 55 }

$('#test')[0].src  获取图片的src

前台上传图片插件实例

链接:https://pan.baidu.com/s/1iGsa08MLlitTbNnYT6yr3w 密码:hqyy

欢迎大家一起说出自己的想法。
原文链接:https://yq.aliyun.com/articles/643543
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章