I have developed a REST service using Jersey which looks like below
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail, #FormDataParam("city") String city){
If I remove city String, then I am able to test file upload test in SoapUI by attaching the file with request.But, I am not able to test file upload along with String using SoapUI.
Related
I got the attached img value as response from the SharePoint server but can not write it on the docx file. The file download API using Postman is giving same response and if I save Postman response to docx file then it is saving perfectly but not from Java side. If I write same response to a docx file using Java, the file is corrupted.
I am using this REST API to download the file from Sharepoint:
SiteURL/_api/web/getfilebyserverrelativeurl('relativeURL/Shared Documents/test.docx')/$value
Most likely server is returning the document (docx) as binary (application/octet-stream). Your code saves the document's string representation:
ResponseEntity<String> response = restTemplate.exchange(fileUrl, HttpMethod.GET, request, String.class);
String responseStrFromSharePoint = response.getBody();
That's why the file could not be decoded by application. Instead save the exact binary (bytes) returned by the server as the following snippet shows:
ResponseEntity<byte[]> response = restTemplate.exchange(fileUrl, HttpMethod.GET, request, byte[].class);
byte[] responseStrFromSharePoint = response.getBody();
Other parts of the code seems fine.
I am trying to upload a file from client to server the client uploads the file to the server using the curl command
client command:
curl -X POST -T pom.xml http://localhost:8070/put --header "origmd5":"7AB4E6F0A4A2D3CBB200DB1677D99AD75"
Now in the controller i.e at the server side the code is as follows
server side:
#PostMapping(value="readFile")
public ResponseEntity<?> uploadfile(#RequestBody String filecontent) throws IllegalStateException, IOException {
System.out.println(filecontent);//prints the content which is inside the file uploaded by client
return null;
}
1.Now the problem statement is how do we get the file name that has been sent by the client the contents of the file can be parsed in the request body but how to get the file name?
#RequestBody String filecontent
2.i am using string like above to parse request body (i.e content of file is stored in String) is this the correct way storing contents of file in string?
You actually need MultipartFile which will give you all the information related to the uploaded file.
uploadfile(#RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename()
}
Using Multipart might be a better solution when uploading files:
#PostMapping(value="readFile")
public ResponseEntity<?> uploadfile(#RequestParam(value="file") MultipartFile fileContent){
}
My payload has the Content-Disposition field. I am trying to upload a bpmn file. So at backend i need to parse the input stream and extract this file name. but i am not able to find a solution of this. I am using jesrey for rest.
I tried with :
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.APPLICATION_JSON)
public Response addWorkflowSchema(#FormDataParam("bpmndata") InputStream uploadedInputStream,
String filename,
#Context HttpServletRequest request)
to get whole payload data. but only if i remove FormDataParam i ma getting request body.
Reuqest Payload :
------WebKitFormBoundary8CCb878TyZksE9go
Content-Disposition: form-data; name="bpmndata"; filename="process.bpmn"
Content-Type: application/octet-stream
------WebKitFormBoundary8CCb878TyZksE9go--
I need to get filename from Content-Disposition. How can i get this.
Just inject the FormDataContentDisposition also, and get the file name from that.
public Response addWorkflowSchema(
#FormDataParam("bpmndata") InputStream in,
#FormDataParam("bpmndata") FormDataContentDisposition fdc) {
String fileName = fdc.getFileName();
}
The InputStream will only be the content of the file part. It won't include the headers, so you don't need to extract anything from it.
I'm implementing a file upload in a web application.
The front-end is written in angularJS and uses the angular-file-upload package (the one that can be found at this link https://github.com/nervgh/angular-file-upload).
The back-end is Java / Jersey Web Services.
My Question is:
The uploaded file contains a WebKitFormBoundary header and footer, like this:
------WebKitFormBoundarylqskdjlqksdjl
Content-Disposition: form-data; name="upload"; filename="foo.bar"
Content-Type: multipart/form-data
Therefore, I'm not sure whether I'm uploading a file or a request. And of course, my back-end application considers that the uploaded files are corrupted and would not display them unless those lines are removed (for now manually).
Bottom line is : how do I get rid of that header and footer in the uploaded file?
Here are some code samples.
Front-End
Once again: angularJS angular-file-upload
item.headers = {
'Content-Disposition': 'attachment; filename="' + item.file.name + '"',
'Content-Type': 'multipart/form-data'
};
Back-End
and Java / Jersey
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("someurl/{fileName}")
public Artifact uploadArtifact(InputStream uploadedStream, #PathParam("fileName") String fileName) throws Exception;
Note
I'm wondering if the Content-Disposition: attachment in my angularJS part could be what's messing it up?
And that it should rather be Content-Disposition: form-data?
Thx in advance!
You need to place #FormDataParam annotation in order to properly handle boundary.
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("someurl/{fileName}")
public Artifact uploadArtifact(
#FormDataParam("file") InputStream uploadedStream,
#FormDataParam("file") FormDataContentDisposition fileDetails,
#PathParam("fileName") String fileName) throws Exception;
I'm using Jersey to upload a file.
This is the rest:
#Path("/MyUpload")
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.TEXT_PLAIN)
public String MyUpload(#Context HttpServletRequest request,
#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileInfo) {
// here I’m handling the input stream
…
return “Ok”;
}
When I send a file with size more than 10KB I get 400 bad request for this method.
Any ideas?
I'm running my app on tomcat7 with linux red hat 6.2.
Thanks..
One problem is that you are fetching both the request multipart params using the same name file. You need to distinguish the two parts using different names for multipart params.
My solution is: change the buggy Jersey libs from version 1.13 (or lower) to the latest version. 1.17 and 1.19 worked for me.