SpringMVC请求(下)-文件上传

本文阅读 1 分钟
首页 代码,Java 正文

img

<form action="${pageContext.request.contextPath}/user/quick21" method="post" enctype="multipart/form-data">
    名称<input type="text" name="username"><br>
    文件<input type="file" name="upload"><br>
    <input type="submit" value="提交">
</form>

img

img

1.导入fileload和io坐标

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.2</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.11.0</version>
    </dependency>

2.配置文件上传解析器

<!--配置文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxInMemorySize" value="500000"></property>
    </bean>

3.编写文件上传代码

@RequestMapping(value = "/quick21")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save21(String username, MultipartFile upload) throws IOException { 
        System.out.println(username);
        /*System.out.println(upload);*/
        //获得上传文件名称
        String originalFilename = upload.getOriginalFilename();
        upload.transferTo(new File("F:\\upload\\"+originalFilename));
    }

1.方式一

<form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
    名称<input type="text" name="username"><br>
    文件<input type="file" name="upload"><br>
    文件<input type="file" name="upload1"><br>
    <input type="submit" value="提交">
</form>
@RequestMapping(value = "/quick22")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save22(String username, MultipartFile upload,MultipartFile upload1) throws IOException { 
        System.out.println(username);
        /*System.out.println(upload);*/
        //获得上传文件名称
        String originalFilename = upload.getOriginalFilename();
        upload.transferTo(new File("F:\\upload\\"+originalFilename));
        String originalFilename1 = upload1.getOriginalFilename();
        upload.transferTo(new File("F:\\upload\\"+originalFilename1));
    }

2.方式二(数组)

<form action="${pageContext.request.contextPath}/user/quick23" method="post" enctype="multipart/form-data">
    名称<input type="text" name="username"><br>
    文件<input type="file" name="upload"><br>
    文件<input type="file" name="upload"><br>
    <input type="submit" value="提交">
</form>
@RequestMapping(value = "/quick23")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save23(String username, MultipartFile[] upload) throws IOException { 
        System.out.println(username);
        /*System.out.println(upload);*/
        //获得上传文件名称
        for (MultipartFile multipartFile : upload) { 
            String originalFilename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File("F:\\upload\\"+originalFilename));

        }
    }

img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125537631
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复