Jersey File Upload-Collection of common programming errors
I tried to upload a file to my jersey server, but there is an error.
Writer output = null;
File file = null;
try {
String text = "Rajesh Kumar";
file = new File("write.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
InputStream is = null;
try {
is = new FileInputStream(file);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FormDataMultiPart part = new FormDataMultiPart().field("file", is, MediaType.TEXT_PLAIN_TYPE);
System.out.println(is);
System.out.println(tenant1.getTenantId());
System.out.println(part);
String response = service.path("rest").path("file").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
The Syso are not null. So the File was written to the inputstream.
Error:
ClientHandlerException: java.io.IOException: ReadError , when I send it to the server.
Server Side:
@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postFileTenant(@FormDataParam("file") InputStream stream) throws IOException {
// save it
return Response.ok(IOUtils.toString(stream)).build();
}