Java Jersey How to Upload File with the File as Parameter

Multi tool use


Java Jersey How to Upload File with the File as Parameter
I have a method in the resource class, uploadFile. It calls a method that takes in a file as a parameter and then split the files into parts before writing said parts to disk.
I would like to know how to write a client test for it, assuming it's possible to upload a file without using @FormDataParam InputStream. Because I don't think I need it in this case.
I've seen plenty of examples for uploading files with Jersey that takes in InputStream and FormDataContentDisposition as @FormDataParam but I'm not sure if that's mandatory.
@POST
@Path("/uploadfile/{file}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@PathParam("file") File file, @FormDataParam("file") FormDataContentDisposition fileMetaData) throws IOException {
FileSplit.splitFile(file); //Write file to disk
String output = "File successfully uploaded";
return Response.status(200).entity(output).build();
}
//Method to write file to disk
public static void splitFile(File f) throws IOException {
int partCounter = 1;
int sizeOfFiles = (int) (f.length() / 3);
byte buffer = new byte[sizeOfFiles];
String fileName = f.getName();
try (FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis)) {
int bytesAmount = 0;
while ((bytesAmount = fis.read(buffer)) != -1) {
String filePartName = String.format("%s.%03d", fileName, partCounter++);
File newFile = new File("D:\", filePartName);
try (FileOutputStream out = new FileOutputStream(newFile)) {
out.write(buffer, 0, bytesAmount);
}
}
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.