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


i'm designing small application in spring mvc framework. have html page user can upload multiple files.

here 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 code i'm allowing user upload 2 files.

also have bean called mcqitem.java:

public class mcqitem {        	private string option1;  	private string option2;      private byte[] img1;  	private byte[] img2;    //with getter , setters  }

in controller have designed method pass data (option1 , option 2) bean , there model , save them in db

but: don't know how save files. prefer save them in file.

can tell me how can save uploaded files?

so here have done after going through 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 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;  		  	}  	
problem along files have form save in db well.

but giving me error: "the current request not multipart request"


Comments