设为首页 加入收藏

TOP

SpringMCV(八):文件上传
2023-07-25 21:26:29 】 浏览:21
Tags:SpringMCV 文件上

一、导入相关依赖

    <dependencies>
        <!--文件上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

实际上我们需要的包有commons-fileupload和commons-io,但我们使用Maven时导入commons-fileupload包,Maven会自动帮我们导入它的依赖包commons-io。

二、前端建立一个文件上传的表单

    <form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
        <input type="file" accept="image/*" name="file"/>
        <input type="submit" value="上传文件"/>
    </form>

enctype时设置被提交数据的编码,enctype="multipart/form-data"是设置文件以二进制数据发送给服务器。

<input type="file" accept="image/*"> type="file"说明是要选择文件进行上传,accept="image/*"表示上传文件的MIME类型,这里表示的是各种类型的图片文件。

三、SpringMVC中配置MultipartResolver

SpringMVC能够支持文件上传的操作,但是需要在上下文中对MultipartResolver进行配置,具体配置如下:

<!--文件上传-->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
        <!--请求的编码格式,默认为ISO-8859-1-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--
        上传文件大小上限,单位为字节(byte),这里是10M和40K
        第二个其实是一个阈值,小于此值文件会存在内存中,大于此值文件会存在磁盘上,所以理解成上传文件的最小上限没有问题
        -->
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

四、编写文件上传的controller,这里有两种上传方法

方法一:

   @RequestMapping("/upload")
    @ResponseBody
    public String fileUpload(@RequestParam("file") CommonsMultipartFile file , HttpServletRequest request) throws IOException {

        //上传的文件名
        String uploadFileName = file.getOriginalFilename();

        //判断文件名是否为空
        if ("".equals(uploadFileName)){
            return null;
        }

        //上传路径保存设置
        String path = request.getServletContext().getRealPath("/upload");
        //如果路径不存在,创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }

        InputStream is = file.getInputStream(); //文件输入流
        OutputStream os = new FileOutputStream(new File(realPath,uploadFileName)); //文件输出流

        //读取写出
        int len=0;
        byte[] buffer = new byte[1024];
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
            os.flush();
        }
        os.close();
        is.close();
        return "/upload/" + uploadFileName;
    }

方法二:

  @RequestMapping("/upload2")
    @ResponseBody
    public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

        //上传路径保存设置
        String path = request.getServletContext().getRealPath("/upload");
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }

        //上传的文件名
        String uploadFileName = file.getOriginalFilename();

        //通过CommonsMultipartFile的方法直接写文件(
        file.transferTo(new File(realPath +"/"+ uploadFileName));

        return "/upload/" + uploadFileName;
    }

注意: 这里如果上传路径保存设置是多级目录,就需要用File.mkdirs()。

 

(本文仅作个人学习记录用,如有纰漏敬请指正)

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SpringBoot(九) - Swagger 下一篇一站式统一返回值封装、异常处理..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目