Just before starting to explain you my problem, I would like to share with you the libraries' versions that I use and the server:
javax.ws.rs-api: 2.0.1
jersey-container-servlet: 2.13
jersey-media-multipart: 2.13
jackson: 2.4.3
I also use Apache Tomcat Server version 7.0.55.
So I coded that below:
/**
* On the client side.
*/
public void uploadAFile() {
Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class)
.build();
WebTarget target = null;
try {
target = client
.target("https://blo-bla.rhcloud.com/rest")
.path("v1").path("upload");
} catch (IllegalArgumentException | NullPointerException e) {
LOG_TO_CONSOLE.fatal(e, e);
LOG_TO_FILE.fatal(e, e);
}
Builder builder = target.request(MediaType.TEXT_PLAIN);
builder.header("Authorization",
getValidBasicAuthenticationStrEncrypted());
FormDataMultiPart form = new FormDataMultiPart();
form.field("anotherParam", "Bozo");
String fileName = "/Users/drizzy/Documents/Divx/CaseDepartFolder/sample.avi";
File file = new File(fileName);
form.bodyPart(new FileDataBodyPart("file", file,
MediaType.APPLICATION_OCTET_STREAM_TYPE));
Response response = builder.post(Entity.entity(form,
MediaType.MULTIPART_FORM_DATA_TYPE));
LOG_TO_CONSOLE.debug(response.getStatus());
LOG_TO_CONSOLE.debug(response.readEntity(String.class));
}
/**
* On the server side.
*/
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileDisposition,
@FormDataParam("anotherParam") String str)
throws FileNotFoundException, IOException
{
System.out.println("str: " + str);
final String basePath = "/Users/drizzy/eclipse-workspace/tomcat7jbossews"
+ "/src/main/resources/uploads/";
final String fileName = fileDisposition.getFileName();
System.out.println(new StringBuilder().append("***** fileName ")
.append(fileName)
.toString());
final String filePath = new StringBuilder().append(basePath)
.append(fileName)
.toString();
System.out.println(filePath);
try (OutputStream fileOutputStream = new FileOutputStream(filePath)) {
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, read);
}
}
return "File Upload Successfully !!";
}
Which generates these exceptions at the client side:
Java Heap space error
and
Java binding Exception: Already connected
So my question is to know if somebody could please provide me an example of code with a client using jersey-client V2.13 which uploads a big file from the client to the server? Or even could telling me what is wrong in my code above?
Note: I only want to use jersey-client version V2.13 for handling that problem, so please do not provide me solutions using third party libraries or which do not use jersey-client version V2.13.