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