http://www.js-css.cn/a/jscode/cutpic/2015/0517/1478.html
aa=$('.img-container > img');
bb=aa.cropper('getCroppedCanvas',{"width":60,"height":60});
var imgdata=bb.toDataURL("image/jpeg"); 获取图片流
imgdata.replace('data:image/jpeg;base64,','')
图片写入
后台
public static void decodeBase64ToImage(String base64, String path, String imgName) {
if(base64.indexOf(" ")>-1)
base64 = base64.replaceAll(" ", "+");
BASE64Decoder decoder = new BASE64Decoder();
File folder = new File(path);
if (!folder.exists())
{
folder.mkdirs();
}
try {
FileOutputStream write = new FileOutputStream(new File(path+"/"+ imgName));
byte[] decoderBytes = decoder.decodeBuffer(base64);
write.write(decoderBytes);
write.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
String save_path=UploadUtil.img_head;
String imgName = System.currentTimeMillis() +".png";
String headImg_name=(String)request.getSession().getAttribute("headImg_name");
String imgdata="";
UploadUtil.decodeBase64ToImage(imgdata, save_path, imgName);
图片读取
@RequestMapping("/image")
@ResponseBody
public void getImage(HttpServletRequest request, HttpServletResponse response) throws Exception{
String JPG="image/jpeg;charset=GB2312";
String headImg_name=(String)request.getSession().getAttribute("headImg_name");
String defImg=request.getSession().getServletContext().getRealPath("")+"css\\personalcenter\\profile.png";
String iname=headImg_name;
String filePath = defImg;
if(!StringUtil.isEmpty(iname))
{
// 本地文件路径
filePath = UploadUtil.img_head+"\\"+iname;
}
if(!UploadUtil.isFind(filePath))
{
filePath = defImg;
}
File file = new File(filePath);
// 获取输出流
OutputStream outputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
// 读数据
byte[] data = new byte[fileInputStream.available()];
fileInputStream.read(data);
fileInputStream.close();
// 回写
response.setContentType(JPG);
outputStream.write(data);
outputStream.flush();
outputStream.close();
}