0

I m developping an application where user can provide text informations and images. I want to save images on file system and in DB, I will add a link of each user to its images So I need to add the user ID to the image name to not get images with same name. I m using SPRING MVC. in my Controller :

@RequestMapping(value="/save",method=RequestMethod.POST)
    public String add ( @RequestParam("prix") Long prix, 
            RequestParam("adresse") String ville,
            @RequestParam("categorie") String categorie,
            @RequestParam("photos") MultipartFile file,
            ) throws FileNotFoundException


 {

   String chemin=null;
   if (!file.isEmpty())
     {
      try {
       String orgName = file.getOriginalFilename();
       // this line to retreive just file name 
       String 
       name=orgName.substring(orgName.lastIndexOf("\\")+1,orgName.length());
       chemin="e:\\images\\"+name;  //here I want to add id (auto generated)
       File file1=new File(chemin);
       file.transferTo(file1);
         } catch (IOException e) {
                                    e.printStackTrace();
                                  }

    }
    annonce.setImage(chemin);
    annonce.setTitre(prix);
    annonce.setCorps(ville);
    annonce.setPrix(cetegorie)
    annoncedao.save(annonce);
    return "SuccessAddAnnonce";
}

but I can not get the ID witch is auto generated so I can not get it throw @RequestParam like adress or categorie because it is auto generated and I still don t have it until we call save method witch is last . Any suggestions are welcome .

4
  • you want to generate a random ID for a user? Commented Jul 1, 2017 at 19:40
  • no, each time I add a user an ID is generated , I want to get the last ID from the Database Commented Jul 1, 2017 at 20:13
  • you didnt tell which DB it is, mysql? and on what is its schema, so i can help Commented Jul 1, 2017 at 20:21
  • sorry , yes it s MySQL Commented Jul 1, 2017 at 22:42

2 Answers 2

1

I got the Id using query with Select max (id) @Query("SELECT MAX(id) FROM Annonce") Long findAutoincrementAnnonce();

I wish help someone.

Sign up to request clarification or add additional context in comments.

Comments

0

You can generate a random ID for each user and assign it to a particular user, and thus its image name also (what you intend to do here I guess).

I will illustrate it simply.

// Get a random ID coressponding to the user address or whatever, here i will just use address

MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(ville.getBytes("UTF-8"));

String randomId = DatatypeConverter.printHexBinary(hash);

// Here you have a uniqueID for an address.
// You could have just used ville.hashCode() also but it might give you collisions with some different Strings.

if Input: "64th Street, Downtown, StackOverFlow" then

randomId = 8AB126F8EBC0F7B5CCBBEB3E21582ADF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.