I have my server (SpringBoot), which expects a file to be uploaded, and my client (plain Java), which needs to send a byte[]
array there.
Server endpoint looks like this:
@RequestMapping(value = "/", method = RequestMethod.POST, headers={"content-type=multipart/form-data"})
ResponseEntity<String> postBytes(@ApiParam(value = "Upload bytes.", example = "0") @RequestBody Bytes bytes) {
return ResponseEntity.ok(byteLinkService.postBytes(bytes.getBytes()));
}
Client request looks like this:
//byte[] bytes is my file to be uploaded
URL url = new URL(this.endpoint + "/bytelink/");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Bearer " + token);
con.setRequestProperty("Content-Type", "multipart/form-data");
con.setRequestProperty("Accept", "*");
con.setDoOutput(true);
String jsonInputString = "{\"bytes\": \"" + Arrays.toString(bytes) + "\"}";
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int status = con.getResponseCode();
System.out.println(status);
Right now I am getting this Exception:
WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported]