java 文件上传(MultipartFile)

版权声明:本文为博主原创,无版权,未经博主允许可以随意转载,无需注明出处,随意修改或保持可作为原创! https://blog.csdn.net/z1729734271/article/details/79026295

需求:MultipartFile 文件上传,依赖包可百度自行下载

public static void uploadFile(MultipartFile file, HttpServletRequest request,String path)
            throws IllegalStateException, IOException {
        // String url =
        // request.getSession().getServletContext().getRealPath("/"); //服务器地址
        if (!file.isEmpty()) {
            // 文件保存路径
            String filePath = path + file.getOriginalFilename();
            // 转存文件
            file.transferTo(new File(filePath));
        }
    }

报错:找不到路径
原因:通过看源码可以知道 transferTo 使用的是绝对路径,所以需要找到项目的绝对路径。

猜你喜欢

转载自blog.csdn.net/z1729734271/article/details/79026295