SpringMvc上传单个文件以及多个文件

    xiaoxiao2021-03-25  93

    1,springMvc 上传单个文件

    前端:简单的form表单

    <form action="跳转地址" method="post" enctype="multipart/form-data">

    <input type="file' name="photo" /><input type="submit" value="上传" />

        </form>

    后台控制层:

    @RequestMapping("upload")

    public String upload(@RequestParam("photo" multipartFile,HttpSession))throws Exception

    {

    if(!photo.isEmpty())

    {

    String path=session.getServletContext().getRealPath("储存地址");

    String fileName = photo.getOriginalFilename();

    if(fileName .endWith(".jpg") || fileName.endWith(".png"))

    {

    File file=new File(path,fileName);

    photo.transferTo(file);

    }else

    {

    return "上传失败";

    }

    }

    return  “上传成功”;

    }

    最后要想能够正常的上传还需要在spring-mybatis文件中配置

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> //maxUploadSize指的是文件最大值 以B为单位             <property name="maxUploadSize"><value>100000000</value> </property> <property name="defaultEncoding"><value>UTF-8</value></property> </bean>

    2,多个文件上传

    前端:简单的form表单多个File

    后台:用数组MultipartFile 然后for循环

    转载请注明原文地址: https://ju.6miu.com/read-16712.html

    最新回复(0)