| | |
| | | * 通用上传请求(单个) |
| | | */ |
| | | @PostMapping("/upload") |
| | | public AjaxResult uploadFile(@RequestParam("uploadFile") MultipartFile file) throws Exception |
| | | public AjaxResult uploadFile(@RequestParam("uploadFile") MultipartFile file,String fname) throws Exception |
| | | { |
| | | try |
| | | { |
| | | // 上传文件路径 |
| | | String filePath = RuoYiConfig.getUploadPath(); |
| | | // 上传并返回新文件名称 |
| | | String fileName = FileUploadUtils.upload(filePath, file); |
| | | String fileName = FileUploadUtils.upload(filePath, file,fname); |
| | | String url = serverConfig.getUrl() + fileName; |
| | | |
| | | AjaxResult ajax = AjaxResult.success(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 通用上传请求(多个) |
| | | */ |
| | | @PostMapping("/uploads") |
| | | public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception |
| | | { |
| | | try |
| | | { |
| | | // 上传文件路径 |
| | | String filePath = RuoYiConfig.getUploadPath(); |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> newFileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | for (MultipartFile file : files) |
| | | { |
| | | // 上传并返回新文件名称 |
| | | String fileName = FileUploadUtils.upload(filePath, file); |
| | | String url = serverConfig.getUrl() + fileName; |
| | | urls.add(url); |
| | | fileNames.add(fileName); |
| | | newFileNames.add(FileUtils.getName(fileName)); |
| | | originalFilenames.add(file.getOriginalFilename()); |
| | | } |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); |
| | | ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | return ajax; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return AjaxResult.error(e.getMessage()); |
| | | } |
| | | } |
| | | // /** |
| | | // * 通用上传请求(多个) |
| | | // */ |
| | | // @PostMapping("/uploads") |
| | | // public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception |
| | | // { |
| | | // try |
| | | // { |
| | | // // 上传文件路径 |
| | | // String filePath = RuoYiConfig.getUploadPath(); |
| | | // List<String> urls = new ArrayList<String>(); |
| | | // List<String> fileNames = new ArrayList<String>(); |
| | | // List<String> newFileNames = new ArrayList<String>(); |
| | | // List<String> originalFilenames = new ArrayList<String>(); |
| | | // for (MultipartFile file : files) |
| | | // { |
| | | // // 上传并返回新文件名称 |
| | | // String fileName = FileUploadUtils.upload(filePath, file); |
| | | // String url = serverConfig.getUrl() + fileName; |
| | | // urls.add(url); |
| | | // fileNames.add(fileName); |
| | | // newFileNames.add(FileUtils.getName(fileName)); |
| | | // originalFilenames.add(file.getOriginalFilename()); |
| | | // } |
| | | // AjaxResult ajax = AjaxResult.success(); |
| | | // ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | // ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | // ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); |
| | | // ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | // return ajax; |
| | | // } |
| | | // catch (Exception e) |
| | | // { |
| | | // return AjaxResult.error(e.getMessage()); |
| | | // } |
| | | // } |
| | | |
| | | /** |
| | | * 本地资源通用下载 |
| | |
| | | return error("修改密码异常,请联系管理员"); |
| | | } |
| | | |
| | | /** |
| | | * 头像上传 |
| | | */ |
| | | @Log(title = "用户头像", businessType = BusinessType.UPDATE) |
| | | @PostMapping("/avatar") |
| | | public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception |
| | | { |
| | | if (!file.isEmpty()) |
| | | { |
| | | LoginUser loginUser = getLoginUser(); |
| | | String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION); |
| | | if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) |
| | | { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("imgUrl", avatar); |
| | | // 更新缓存用户头像 |
| | | loginUser.getUser().setAvatar(avatar); |
| | | tokenService.setLoginUser(loginUser); |
| | | return ajax; |
| | | } |
| | | } |
| | | return error("上传图片异常,请联系管理员"); |
| | | } |
| | | // /** |
| | | // * 头像上传 |
| | | // */ |
| | | // @Log(title = "用户头像", businessType = BusinessType.UPDATE) |
| | | // @PostMapping("/avatar") |
| | | // public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception |
| | | // { |
| | | // if (!file.isEmpty()) |
| | | // { |
| | | // LoginUser loginUser = getLoginUser(); |
| | | // String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION); |
| | | // if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) |
| | | // { |
| | | // AjaxResult ajax = AjaxResult.success(); |
| | | // ajax.put("imgUrl", avatar); |
| | | // // 更新缓存用户头像 |
| | | // loginUser.getUser().setAvatar(avatar); |
| | | // tokenService.setLoginUser(loginUser); |
| | | // return ajax; |
| | | // } |
| | | // } |
| | | // return error("上传图片异常,请联系管理员"); |
| | | // } |
| | | } |
| | |
| | | * @return 文件名称 |
| | | * @throws Exception |
| | | */ |
| | | public static final String upload(MultipartFile file) throws IOException |
| | | public static final String upload(MultipartFile file,String fname) throws IOException |
| | | { |
| | | try |
| | | { |
| | | return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,fname); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | |
| | | * @return 文件名称 |
| | | * @throws IOException |
| | | */ |
| | | public static final String upload(String baseDir, MultipartFile file) throws IOException |
| | | public static final String upload(String baseDir, MultipartFile file,String fname) throws IOException |
| | | { |
| | | try |
| | | { |
| | | return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,fname); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | |
| | | * @throws IOException 比如读写文件出错时 |
| | | * @throws InvalidExtensionException 文件校验异常 |
| | | */ |
| | | public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) |
| | | public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension,String fname) |
| | | throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, |
| | | InvalidExtensionException |
| | | { |
| | |
| | | |
| | | assertAllowed(file, allowedExtension); |
| | | |
| | | String fileName = extractFilename(file); |
| | | String fileName = extractFilename(file,fname); |
| | | |
| | | String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); |
| | | file.transferTo(Paths.get(absPath)); |
| | |
| | | /** |
| | | * 编码文件名 |
| | | */ |
| | | public static final String extractFilename(MultipartFile file) |
| | | public static final String extractFilename(MultipartFile file,String fname) |
| | | { |
| | | if (StringUtils.isNotEmpty(fname)){ |
| | | return fname+'.'+getExtension(file); |
| | | } |
| | | return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), |
| | | FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); |
| | | } |