Search code examples
javaspringspring-mvcjavabeans

How can i upload and retrieve files (image,audio and video) in Spring MVC


I'm designing a small application in Spring MVC framework. I have a HTML page where user can upload multiple files.

Here is my HTML file:

<div class="form-group">
<label class="control-label col-sm-4" for="option1">Option 1:</label>
<div class="col-sm-4">
<form:input type="text" path="option1" class="form-control"/>
</div>
<div class="col-sm-4">
	<form:input type="file" path="img1" class="form-control" name="img1"/>
</div>
</div>
							
<div class="form-group">
<label class="control-label col-sm-4" for="option2">Option 2:</label>
<div class="col-sm-4">
<form:input type="text" path="option2" class="form-control"/>
</div>
<div class="col-sm-4">
<form:input type="file" path="img2" class="form-control" name="img2"/>
</div>
</div>

based on this code I'm allowing the user to upload 2 files.

Also i have a bean called McqItem.java:

public class McqItem {
  
  	private String option1;
	private String option2;
    private byte[] img1;
	private byte[] img2;

//with their getter and setters
}

In my controller i have designed a method where i pass all the data (option1 and option 2) to the bean and from there to the model and save them in my DB

BUT: I don't know how to save my files. prefer to save them in a file.

Can someone tell me how can I save the uploaded files?


Solution

  • so here is what i have done after going through that link

    Controller:

    @RequestMapping(value="/questionType/MCQ.do",method = RequestMethod.POST)
    	public ModelAndView saveMCQuestion(@RequestParam("option1") String option1,@RequestParam("option2") String option2 ,@RequestParam("img1") MultipartFile img1,@RequestParam("img2") MultipartFile img2,@ModelAttribute McqItem mcqItem, HttpServletRequest request)throws IOException{
    		ModelAndView modelAndView = new ModelAndView();
    		QuizItem quizitem=(QuizItem)request.getSession().getAttribute("quizItem");
    		mcqItem.setQuiz_id(String.valueOf(quizitem.getId()));
    		QuizItem qType=(QuizItem)request.getSession().getAttribute("qTypeItem");
    		mcqItem.setQType(qType.getItemType());
    		
    //begin the uploading section
    
    		byte[] img1File=null;
    		byte[] img2File=null;
    		if(!img1.isEmpty() && !img2.isEmpty()){
    		try{
    			img1File= img1.getBytes();
    			img2File=img2.getBytes();
    			
    			BufferedOutputStream stream= 
    					new BufferedOutputStream(new FileOutputStream(new File(option1)));
    			stream.write(img1File);
    			stream.write(img2File);
    			stream.close();
    			System.out.println("Successful Upload");
    		}catch(Exception e){
    			return null;
    		}	}
    		
    		
    //end Uploading section
    	
    		projectDAO.saveQuestion(mcqItem);
    		modelAndView.addObject("qtypeitem", new QuizItem());
    		modelAndView.setViewName("project/qType");
    		
    		return modelAndView;
    		
    	}
    	
    Basically my problem is that along my files i have a form to save in db as well.

    But its giving me this error: "The current request is not a multipart request"