title: Java : Rename MultiPartFile with unique name and store it to a folder and save the url to database
body: <p>i have a Backend based on Spring Boot, the frontend based on Angular 5.
  after recieving the MultiPartFile via Angular i want to rename this file with 
  unique name(id of the table) and store the file into folder then put the URL of this file to DataBase PostgreSQL.
  please help me, i tested a lot of methods without any good result. 
  i tryed to convert the MultiPartFile to File, but i don't know how to do. 
  please help me</p>
  
  <h3>Here is the Class that contain the methods of handling MultiPartFile :</h3>
  
  <pre><code>package smart.syndic.metier;
  
  import java.net.MalformedURLException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.nio.file.StandardCopyOption;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.core.io.Resource;
  import org.springframework.core.io.UrlResource;
  import org.springframework.stereotype.Service;
  import org.springframework.util.FileSystemUtils;
  import org.springframework.web.multipart.MultipartFile;
  
  @Service
  public class StorageService 
  {
  Logger logger = LoggerFactory.getLogger(this.getClass().getName());
  private final Path rootLocation = Paths.get("fichiers/prestataires");
  
  public void store(MultipartFile file) {
      try {
  
          Files.copy(file.getInputStream(), 
  this.rootLocation.resolve(file.getOriginalFilename()), 
  StandardCopyOption.REPLACE_EXISTING);
      } catch (Exception e) {
          throw new RuntimeException("FAIL!");
      }
   }
  
  public Resource loadFile(String filename) {
      try {
          Path file = rootLocation.resolve(filename);
          Resource resource = new UrlResource(file.toUri());
          if (resource.exists() || resource.isReadable()) {
              return resource;
          } else {
              throw new RuntimeException("FAIL!");
          }
      } catch (MalformedURLException e) {
          throw new RuntimeException("FAIL!");
      }
   }
  
  public void deleteAll() {
      FileSystemUtils.deleteRecursively(rootLocation.toFile());
   }
  }
  </code></pre>
  
  <h3>Here is the RestController :</h3>
  
  <pre><code>package smart.syndic.web;
  
  import java.util.ArrayList;
   import java.util.List;
   import java.util.stream.Collectors;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.core.io.Resource;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.PageRequest;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.ResponseEntity;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.CrossOrigin;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.multipart.MultipartFile;
  
  
  import  org.springframework.web.servlet.mvc.method.annotation.
  MvcUriComponentsBuilder;
  import smart.syndic.dao.PrestatairesRepository;
  import smart.syndic.dao.PrestatairesTypesRepository;
  import smart.syndic.entities.Prestataires;
  import smart.syndic.metier.StorageService;
  
  @RestController
  @CrossOrigin("*")
  public class PrestatairesRestController 
  {
  @Autowired
  private PrestatairesRepository repository;
  
  @Autowired
  private PrestatairesTypesRepository repository2;
  
  @Autowired
  private StorageService storageService;
  
  private List&lt;String&gt; files = new ArrayList&lt;String&gt;();
  
  @RequestMapping(value="/prestataires/{id}", 
          method=RequestMethod.GET)
  public Prestataires getVilles(@PathVariable Long id)
  {
      return repository.findOne(id);
  }
  
  @RequestMapping(value="/prestataires", 
          method=RequestMethod.POST)
  public Prestataires addVilles(Prestataires p,
          @RequestParam("multipartFile") MultipartFile file)
  {
      try{
          storageService.store(file);
          files.add(file.getOriginalFilename());
          p.setPhoto("fichiers/prestataires/"+file.getOriginalFilename());
  
      }catch(Exception e){
          e.printStackTrace();
      }
  
  
   p.setPrestatairesTypes(repository2.findOne(p.getPrestatairesTypes()
   .getId()));
  
      return repository.save(p);              
  }
  
  }
  </code></pre>
  
  <h3>Here is the Entity Prestataires :</h3>
  
  <pre><code>package smart.syndic.entities;
  
  import java.io.Serializable;
  import java.util.Collection;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.OneToMany;
  
  @Entity
  public class Prestataires implements Serializable 
  {
  @Id @GeneratedValue
  private Long id;
  private String nom;
  private String email;
  private String tele;
  private String fax;
  private String rib;
  private String adresse;
  private String taches;
  private String photo;
  
  //-------------------Constructors--------------------
  //-------------------Getters and Setters--------------------
  
  
  @ManyToOne
  @JoinColumn(name="ID_PRESTATAIRES_TYPES")
  private PrestatairesTypes prestatairesTypes;
  }
  </code></pre>