2017-07-01 58 views
0

我开发了一个应用程序,用户可以在其中提供文本信息和图像。我想将图像保存在文件系统和数据库中,我将添加每个用户的链接到它的图像因此,我需要将用户ID添加到图像名称,以便不会获得具有相同名称的图像。我使用SPRING MVC。 在我的控制器:在文件系统上而不是数据库上保存图像

@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"; 
} 

,但我不能获得ID女巫产生的,所以我不能把它扔@RequestParam像ADRESS或categorie,因为它是自动生成的汽车,我还是不要吨有它,直到我们呼叫保存方法巫婆是最后的。 欢迎任何建议。

+0

你想为用户生成一个随机ID? – abstractnature

+0

不,每次我添加一个用户一个ID生成,我想从数据库 –

+0

得到最后一个ID,你没有告诉它是哪个数据库,mysql?并在其架构是什么,所以我可以帮助 – abstractnature

回答

1

我使用的查询与选择MAX(ID) @Query的ID(“SELECT MAX( id)FROM Annonce“) Long findAutoincrementAnnonce();

我希望能帮到别人。

0

您可以为每个用户生成一个随机ID,并将其分配给特定用户,因此其图像名称也是(您打算在此处进行的操作)。

我会简单说明一下。

// 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. 

如果输入: “第64街,市中心,计算器”,然后

randomId = 8AB126F8EBC0F7B5CCBBEB3E21582ADF

相关问题