JEE spring boot

chouaib00
Member
Inscription: 27 April 2017

JEE spring boot

Post By chouaib00 » 27 April 2017, 21:37

JEE (Spring Boot)
Well my problem is i want to upload files(images) from an input into a database , 
and i can select the image from wherever in my disk and upload it 
hope someone helps me
rono
Member
Inscription: 04 May 2017

Re: JEE spring boot

Post By rono » 04 May 2017, 10:20

for exemple if u want to update the image of the user:
use a form in your html page like this :
<form method="POST" action="/savePicture" enctype="multipart/form-data" > 
 <i class="fa fa-cloud-upload"> 
              <input type="file" name="file Upload" style="display:inline-block;">
              </i>
             </span>
                <input type="submit" class="btn btn-info" name="changes" value="Save"> 
</form>
then create the method  "/savePicture" in your controller just like this :
@RequestMapping(value="/ savePicture" ,method=RequestMethod.POST)

public @ResponseBody void  savePicture( HttpServletRequest httpServletRequest,HttpServletResponse response,
@RequestParam(value="fileUpload",required = true) MultipartFile file1,
RedirectAttributes redirectAttributes
       ) throws Exception{

HttpSession session=httpServletRequest.getSession(true);
SecurityContext securityContext=(SecurityContext) session
         .getAttribute("SPRING_SECURITY_CONTEXT");
String login=securityContext.getAuthentication().getName();
    User user= userRepository.getUser(login);
 
    if(file1 != null) {
   BufferedInputStream entree = null;
   BufferedOutputStream sortie = null;

      String fichier =file1.getOriginalFilename();
      String extension = fichier.substring(fichier.lastIndexOf(".")+1);  

    String lienFichier="your_link";
    String lien = lienFichier+"/"+fichier ;

  try {
   entree = new BufferedInputStream( file1.getInputStream() );
                 sortie = new BufferedOutputStream( new FileOutputStream(lien));
         
          byte[] buffer = new byte[1024];
          int read;
          while ((read = entree.read(buffer)) > 0)
       sortie.write(buffer, 0, read);
          
          sortie.close();
          entree.close();
       
                
              } catch ( IOException ignore ) { }
 
      user.setPhoto("file_in_the _project_where_u want to put your images/"+fichier); 
      userRepository.save(user);
   }
 }
Nico.Fr
Member
Inscription: 12 May 2017

Re: JEE spring boot

Post By Nico.Fr » 12 May 2017, 23:31

Nice example