Skip to content

Instantly share code, notes, and snippets.

@lefloh
Last active August 29, 2015 14:24
Show Gist options
  • Save lefloh/c9bc852596c7f1a8b71e to your computer and use it in GitHub Desktop.
Save lefloh/c9bc852596c7f1a8b71e to your computer and use it in GitHub Desktop.
RESTesy resource guessing the encoding
@POST
@Path("/form")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadForm(MultipartFormDataInput input) throws IOException {
String charset = input.getFormDataMap().get("_charset_").get(0).getBodyAsString();
InputPart file = input.getFormDataMap().get("file").get(0);
InputStream inputStream = file.getBody(InputStream.class, null);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset));
String line;
StringBuilder content = new StringBuilder();
while ((line = br.readLine()) != null) {
content.append(line);
}
return Response.ok(content).header("Content-Type", MediaType.TEXT_PLAIN_TYPE.withCharset(charset)).build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment