java.lang.IllegalArgumentException: Last unit does not have enough valid bits
at java.util.Base64$Decoder.decode0(Base64.java:734) ~[na:1.8.0_191]
at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_191]
at java.util.Base64$Decoder.decode(Base64.java:549) ~[na:1.8.0_191]
javax.imageio.IIOException: Invalid JPEG file structure: two SOF markers
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method) ~[na:1.8.0_191]
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1247) ~[na:1.8.0_191]
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1050) ~[na:1.8.0_191]
java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit
at java.util.Base64$Decoder.decode0(Base64.java:704) ~[na:1.8.0_191]
at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_191]
at java.util.Base64$Decoder.decode(Base64.java:549) ~[na:1.8.0_191]
java.lang.IllegalArgumentException: Illegal base64 character 3a
Illegal Argument Exception: Illegal base64 character 3a when decoding String value using Base64.getDecode()
刚开始改成这个 引发了一系列问题
byte[] bytes = Base64.getMimeDecoder().decode(authInfo);
System.out.println(Arrays.toString(bytes));
前端代码
function(cropper,result){
console.log(result)
// Bootstrap's Modal
$('#getCroppedCanvasModal').find('.modal-body').html(result);
$("#getCroppedCanvasModal").modal("show");
var uploadedImageType = 'image/jpeg';
var uploadedImageName = 'cropped.jpg';
var dataURL =result.toDataURL(uploadedImageType);
console.log(dataURL)
// 转化为BASE64位
var file = encodeURIComponent(dataURL)
console.log(file)
//if (!download.disabled) {
// download.download = uploadedImageName;
// download.href = result.toDataURL(uploadedImageType);
//}
$.ajax({
url : ‘/cropper/upload',
type : 'POST',
data : "file=" + file,
async : true,
success : function(data) {
console.log(data)
}
});
});
}
错误原因:
没有进行这一步转码
var file = encodeURIComponent(dataURL)
后端代码:
@ResponseBody
@PostMapping("/cropper/upload")
public FileUploadModel cropper(@RequestParam("file") String file, HttpServletRequest request) throws Exception {
Decoder decoder = Base64.getDecoder();
// 去掉base64编码的头部 如:"data:image/jpeg;base64," 如果不去,转换的图片不可以查看
file = file.substring(file.indexOf(",")+1);
// 解码
byte[] imgByte = decoder.decode(file);
FileUploadModel fileUploadModel = new FileUploadModel();
String fileId = UUID.randomUUID().toString();
String date = CalendarUtil.toStrings(new Date(), CalendarUtil.yyyyMMdd_sprit);
String filePath = File.separator + date + File.separator + fileId + "_" + getFileName();
fileUploadModel.setFileId(fileId);
fileUploadModel.setFilePath(filePath);
fileUploadModel.setFileType("image");
File uploadFile = new File(appConfig.getImageUploadDir(), filePath);
File parentFile = uploadFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(uploadFile); // 输出文件路径
IOUtils.write(imgByte, out);
IOUtils.closeQuietly(out);
// 图片对象
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(uploadFile));
// 宽度
int width = bufferedImage.getWidth();
// 高度
int height = bufferedImage.getHeight();
fileUploadModel.setWidth(width);
fileUploadModel.setHeight(height);
fileUploadModel.setThumbnailUrl(appConfig.getImageUploadServer() + filePath);
fileUploadModel.setUrl(appConfig.getImageUploadServer() + filePath);
fileUploadModel.setVersionInfo(new FileUploadVersionInfo(fileId, "v1"));
return fileUploadModel;
}
文章评论