上代码:这首先的是前端代码
点击下载
上控制层代码:
package com.spring.Controller;
import java.io.File;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.spring.util.ImageDownloadUtil;
@Controller
@RequestMapping("/images")
public class ImagesController {
@RequestMapping(value="/upload.do",produces = "text/html; charset=utf-8")
@ResponseBody
public void ImageUpload(HttpServletRequest request,@RequestParam("baseStr")String baseStr,HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
System.out.println("baseStr:"+baseStr);
String path = request.getSession().getServletContext().getRealPath("/images");
long fileName = System.currentTimeMillis();
ImageDownloadUtil.decodeBase64ToImage(baseStr, path, String.valueOf(fileName));
}
}
上服务层代码:
package com.spring.util;
import java.io.File;
import java.io.FileOutputStream;
import cn.hutool.core.codec.Base64Decoder;
public class ImageDownloadUtil {
public static void decodeBase64ToImage(String base64,String path,String fileName) {
try {
System.out.println(path+File.separator+fileName);
FileOutputStream out = new FileOutputStream(path+File.separator+fileName+".png");
byte[] decodeBytes = Base64Decoder.decode(base64);
out.write(decodeBytes);
out.flush();
out.close();
}catch (Exception e) {
// TODO: handle exception
}
}
}
首先呢,我是把canvas通过toDataURL("image/png")转换成base64编码制,然后直接通过Ajax把整个生成的字符串发送给后台,发现生成的图片呢(声明下,后端处理的代码没有任何问题);
后来,我在改的过程中吧toDataURL("image/png")输出下,发现他的格式是:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAATE0lEQVR4Xu3X25FEOxFFQa4PmIAZmI8ZmIAPYAAXonu3HlWlnO+WjpSliBXzxz//+vd//8UfAQJ/KvC3f/3jDzQECPy5wB8C4mkQ+N8CAuJ1EPjfAgLidRD4PwIC4nkQEBBvgEAkICARm0WPCPgP5JFBu2YmICCZm1VvCAjIG3N2y1B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QIEBAQb4AAAQIEIgEBidgsIkCAAAEB8QYIECBAIBIQkIjNIgIECBAQEG+AAAECBCIBAYnYLCJAgAABAfEGCBAgQCASEJCIzSICBAgQEBBvgAABAgQiAQGJ2CwiQIAAAQHxBggQIEAgEhCQiM0iAgQIEBAQb4AAAQIEIgEBidgsIkCAAAEB8QYIECBAIBIQkIjNIgIECBAQEG+AAAECBCIBAYnYLCJAgAABAfEGCBAgQCASEJCIzSICBAgQEBBvgAABAgQiAQGJ2CwiQIAAAQHxBggQIEAgEhCQiM0iAgQIEBAQb4AAAQIEIgEBidgsIkCAAAEB8QYIECBAIBIQkIjNIgIECBAQEG+AAAECBCIBAYnYLCJAgAABAfEGCBAgQCASEJCIzSICBAgQEBBvgAABAgQiAQGJ2CwiQIAAAQHxBggQIEAgEhCQiM0iAgQIEBAQb4AAAQIEIgEBidgsIkCAAAEB8QYIECBAIBIQkIjNIgIECBAQEG+AAAECBCIBAYnYLCJAgAABAfEGCBAgQCASEJCIzSICBAgQEBBvgAABAgQiAQGJ2CwiQIAAAQHxBggQIEAgEhCQiM0iAgQIEBAQb4AAAQIEIgEBidgsIkCAAAEB8QYIECBAIBIQkIjNIgIECBAQEG+AAAECBCIBAYnYLCJAgAABAfEGCBAgQCASEJCIzSICBAgQEBBvgAABAgQiAQGJ2CwiQIAAAQHxBggQIEAgEhCQiM0iAgQIEBAQb4AAAQIEIgEBidgsIkCAAAEB8QYIECBAIBIQkIjNIgIECBAQEG+AAAECBCIBAYnYLCJAgAABAfEGCBAgQCASEJCIzSICBAgQEBBvgAABAgQiAQGJ2CwiQIAAAQHxBggQIEAgEvgPm/EhvhT+F8oAAAAASUVORK5CYII=
看到没,前面有个data:image/png;base64, 。我想啊,后天是处理base64编码的,但是好像前面不是吧,结果我把前面这段截掉了,果然生成的图片就对了。(代码已经给出了)