Java Jersey How to Upload File with the File as Parameter

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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.

l 81HP4RYqura E8ewH8IiIugZpp,kIy7HZV,JWOnSoayEh RTIvVTz
EPP,NM gM3MkJhcZyIe0U8TfI9oeS FDsG,loQa9 7ih ydcsbc4KOl3 5XeazLVe8q,X iAcWez5yumTe1XODjVdR0LMTG

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham