Mutiple Files Upload Concept Handling in Rest Controller

multipart file upload

Here, we can upload number of files and we will manage at server side also,

Multiple File Upload concept , we can handle by using MultipartFile Interface.


public interface MutipartFile

MutipartFile is an interface, Commonly Client will send or upload a file , it will be sent to server as in the form of mutipart request.

We will catch that mutipart request by using MutipartFile Concept. This MutipartFile Interface have number of method , those methods are used to get information of that file and if you want to do any copy or moving of a file also we can perform by one of the method called transferTo(“destinationPath”) ;

Methods :

Java.lang.String getName()It will returns the name of the Parameter in mutipart request.
If you uploading demo.txt file , if you call to this method you will get output as file Means gives type of parameter name.
Java.lang.String getOriginalFileName()It will returns the name of a file in client system.
Note: depends on browser this method will return eighter file name or file path.
Java.lang.String getContentType()It will returns Content type of file.
Ex: if you upload a text file , then this method will return content type as application/octet-stream.
if you upload an image then this method will return image/png as content type.
If you upload CSS file , then this method will return text/css.
boolean isEmpty()It will return boolean values . Means when ever uploaded file has doesnt contain any content then this method will return TRUE. Or if no file has choosen in multipart form then also you will get TRUE.
Remaining times it return FALSE.
byte[] getBytes() throws will returns content of file as in the form of byte array.
If file present in mutipart request then it will return array of bytes,
if file is not there in request then it will return empty array .
Throws IOException while accessing files . getInputStream() throws will returns inputstream to read the content of file From source.
Developer responsible to close stream.
Void transferTo( destinationFilePath) throws, java.lang.IllegalStateExceptionIt will transfers file into destination path.
If file already present in that destination folder , first it will delete that one and replace with new one.
In case of reading or writing file we will get
if file already deleted or moved from that place that time if you transefer file into another folder then we will get java.lang.IllegalStateException.

Rest Controller:

Below you can find how to receive mutiple files and move it to perticular location.

   * Multiple Files Upload 
   * @param req
   * @param files
   * @return
   * @throws IOException 
    @RequestMapping(value="/multipleFilesUpload" , method=RequestMethod.POST, 
  	consumes="multipart/form-data", produces="application/json")  
	public ResponseEntity<?> mutipleFileUpload(HttpServletRequest req, 
      @RequestParam(value="file" , required = false) MultipartFile[] files) throws IOException{
  for (MultipartFile file : files) {
	 File f= new File(getPath()+createFolderInDesc("/appFiles/GSTC/mutipleUpload/"),file.getOriginalFilename());
	   try {
		    file.transferTo(f); //Transfer or Saving in local memory 
		} catch (IllegalStateException e) {
		} catch (IOException e) {

		return null;

multipart form data

Write a comment