Illegal base64 character

I am trying uploading an image with this javascript code:

var reader = new FileReader();

reader.onloadend = function() {

var bytes = reader.result;

var ext = file.name.split(".").pop();

xhr.send("bytes="+bytes+"&type="+ext);

}

reader.readAsDataURL(file);

where bytes is passed to this java code on the server side:

public Integer upload(String bytes, String type) throws IOException {

byte[] bytes_final = Base64.getDecoder().decode(bytes.split(",")[1]);

BufferedImage src = ImageIO.read(new ByteArrayInputStream(bytes_final));

...

}

but I am getting the error:

java.lang.IllegalArgumentException: Illegal base64 character 20

at java.base/java.util.Base64$Decoder.decode0(Base64.java:743) ~[na:na]

at java.base/java.util.Base64$Decoder.decode(Base64.java:535) ~[na:na]

at java.base/java.util.Base64$Decoder.decode(Base64.java:558) ~[na:na]

at org.loja.model.imagem.ImagemService.upload(ImagemService.java:25) ~[classes/:1.0-SNAPSHOT]

at org.loja.model.imagem.ImagemController.upload(ImagemController.java:25) ~[classes/:1.0-SNAPSHOT]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]

at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]

at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)...

Read More »

By: StackOverFlow - 5 days ago

Related Posts