springmvc下多文件上传

/**
 * 多文件上传
 *
 * @param session
 * @param request
 * @return
 */
@RequestMapping(value = "imageUpload", method = RequestMethod.POST)
public ServerResponse imageUpload(HttpSession session, HttpServletRequest request) {
    List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
    if (!CollectionUtils.isEmpty(files)) {
        for (MultipartFile file : files) {
            if (!file.isEmpty()) {
                //获取文件名
                String fileName = file.getOriginalFilename();
                //文件后缀名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                String filepath = uploadpath;
                fileName = UUID.randomUUID() + suffixName;
                File dest = new File(filepath + fileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                    return ServerResponse.createByErrorMessage("上传失败");
                }
            }
        }
    }
    return ServerResponse.createBySuccess("上传成功");

}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页