0

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?

Maryam Moeini
  • 29
  • 1
  • 5

2 Answers2

1

You can use multi part file upload to upload and save files.

     byte[] bytes = file.getBytes();
            BufferedOutputStream stream =
                    new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();

This sample from spring boot is a very good exampe

https://spring.io/guides/gs/uploading-files/

Paul John
  • 1,626
  • 1
  • 13
  • 15
  • Hi paul, thanks for the answer but where should i place these codes? where is this code sending the image? – Maryam Moeini Jul 21 '15 at 07:54
  • The image will be saved to the file system on the server. The code i pasted above is for the controller. However I think its better you take a look at this sample project - https://spring.io/guides/gs/uploading-files/ – Paul John Jul 21 '15 at 07:57
  • If this helps - would be great if you accept the answer. Thanks! – Paul John Jul 21 '15 at 07:57
1

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"

Maryam Moeini
  • 29
  • 1
  • 5
  • are you using spring-boot (the link uses spring boot). If not, you may need to add multipart config element and dependencies manually. http://stackoverflow.com/questions/15008049/org-springframework-web-multipart-multipartexception-the-current-request-is-not – Paul John Jul 22 '15 at 13:38