Java頭像上傳

@RequestMapping(path = { "/user-img-upload" }, method = { RequestMethod.POST })
    public AjaxReturn  fileUpload2(HttpServletRequest request,@RequestParam("file") MultipartFile file,String account){
        String path= "D:\\nginx-1.12.1\\html\\home\\im\\images/"+new Date().getTime()+file.getOriginalFilename();
        //按指定大小把圖片進行縮和放(會遵循原圖高寬比例)
        File newFile=new File(path);
        String path2;
        try {
             path2="D:\\nginx-1.12.1\\html\\home\\im\\images/"+new Date().getTime()+file.getOriginalFilename();
            file.transferTo(newFile);
            Thumbnails.of(path).size(400,400).toFile(path2);//變爲400*300,遵循原圖比例縮或放到400*某個高度
            MemInfo memInfo = new MemInfo();
            memInfo.setUserAccount(account);
            memInfo.setPortrait(request.getScheme()+"://"+request.getServerName()+"/images"+path2.substring(path2.lastIndexOf("/"),path2.length()));
            memInfoService.updateUserPortrait(memInfo);
        } catch (IOException e) {
            return new AjaxReturn(500,"上傳失敗!",null);
        }
        return new AjaxReturn(200,null,request.getScheme()+"://"+request.getServerName()+"/images"+path2.substring(path2.lastIndexOf("/"),path2.length()));
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章