满天星
Fork me on GitHub

Java爬坑笔记-上传文件

@Autowired
    private ServletContext servletContext;
    /**
     * 上传单个文件的页面
     * @return 页面的路径
     */
    @RequestMapping(value = "/blog/test", method = RequestMethod.GET)
    public String uploadFilePage() {
        return "publicityDeliveryUpLoad.jsp";
    }
/**
 * 上传单个文件
 *
 * @param file 上传文件 MultipartFile 的对象
 * @return 上传的结果
 */
@RequestMapping(value = "/blog/test", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile file) {
    saveFile(file);
    return "Success";
}
/**
 * 把 HTTP 请求中的文件流保存到本地
 *
 * @param file MultipartFile 的对象
 */
private boolean saveFile(MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            // getRealPath() 取得 WEB-INF 所在文件夹路径
            // 如果参数是 "/temp", 当 temp 存在时返回 temp 的本地路径, 不存在时返回 null/temp (无效路径)
            String path = servletContext.getRealPath("../libs/img/publicityDeliveryImg") + File.separator + file.getOriginalFilename();
            FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(path));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}
/**
 * 上传多个文件的页面
 * @return 页面的路径
 */
@RequestMapping(value = "/blog/test", method = RequestMethod.GET)
public String uploadFilesPage() {
    return "publicityDeliveryUpLoad.jsp";
}
/**
 * 上传多个文件
 *
 * @param files 上传文件 MultipartFile 的对象数组
 * @return 上传的结果
 */
@RequestMapping(value = "/blog/test", method = RequestMethod.POST)
@ResponseBody
public String uploadFiles(@RequestParam("files") MultipartFile[] files) {
    for (MultipartFile file : files) {
        saveFile(file);
    }
    return "Success";
}
-------------本文结束期待您的评论-------------