Sai Teja Pakalapati
Sai Teja Pakalapati

Reputation: 796

java Spring boot get the file name in the controller when post request is sent via curl

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?

Upvotes: 1

Views: 8005

Answers (2)

x86-64
x86-64

Reputation: 395

Using Multipart might be a better solution when uploading files:

@PostMapping(value="readFile")
public ResponseEntity<?> uploadfile(@RequestParam(value="file") MultipartFile fileContent){

}

Upvotes: -2

Mehraj Malik
Mehraj Malik

Reputation: 15874

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()
}

Upvotes: 6

Related Questions