package com.togest.emis.modules.resource.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.togest.emis.framework.utils.EncodeUtils;
/**
@author mmk
*
*/
@Controller
public class PsaveController {
@RequestMapping(value="save",method = RequestMethod.POST)
public void save(Map map,HttpServletRequest request,HttpServletResponse response) throws IOException{
String imageData2 = request.getParameter("imageData2");
String file_path = request.getParameter("file_path");
file_path = URLDecoder.decode(file_path , "utf-8");
System.out.println("++++++++++++++++"+file_path);
//String f = new String(request.getParameter("file_path").getBytes("ISO-8859-1"),"UTF-8");
//System.out.println("+++++++++++++++++"+f);
String file_name = request.getParameter("file_name");
String Path = request.getSession().getServletContext().getRealPath("/");
String filePath2 = file_path.substring(0, 34);
String filePath=file_path.replace(filePath2,Path);
filePath =EncodeUtils.isoDecode(filePath);
String fileName=file_name;
File file = new File(filePath);
System.out.println("+++++++"+filePath);
//System.out.println("+++++++++"+EncodeUtils.isoDecode(filePath));
if(imageData2!=null){
try{
FileOutputStream write = new FileOutputStream(file);
byte[] decoderBytes = Base64.decodeBase64(imageData2.replace("data:image/png;base64,",""));
imageData2 = imageData2.replace("base64,","");
write.write(decoderBytes);
write.flush();
write.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
保存控制器代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
href="${pageContext.request.contextPath}/system/css/wColorPicker.css" />
href="${pageContext.request.contextPath}/system/css/wPaint.css" />
<td colspan="2">
<div>
<div id="box"></div>
<input type="button" value="保存" onClick="saveImage2()"
onblur="saveImage2()" /> <input type="button" value="提交"
onblur="saveImage2()" onclick="saveImage2()"> <input
type="button" value="全屏" onclick="full_screen()">
</div> <br />
</td>
</tr>
![图片说明](https://img-ask.csdn.net/upload/201608/01/1470031972_457168.png)