• Spring mvc上传多文件


    jsp页面

    <form action="${pageContext.request.contextPath }/user/upload" method="post" enctype="multipart/form-data">
       	attach 1:<input type="file" name="attachs" /><br/>
       	attach 2:<input type="file" name="attachs"/><br/>
       	<button type="submit">提交</button>
    </form>
    

    controller.java 接收数据(代码如下)

    @RequestMapping(value="/upload",method=RequestMethod.POST )
        public String upload(@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest req){
            String realPath=req.getSession().getServletContext().getRealPath("/resources/upload");//保存的路径
            System.out.println(attachs.length);
            System.out.println(realPath);
            for(MultipartFile attach:attachs){
                System.out.println(attach);
                if(!attach.isEmpty()){
                    File file=new File(realPath+"/"+attach.getOriginalFilename());
                    try {
                //底层数据io传输 attach.transferTo(file); }
    catch (IllegalStateException | IOException e) { e.printStackTrace(); } } } return "welcome"; }


    dispatcherServlet-servlet.xml  代码:

    <!-- 配置MultipartResolver,用于上传文件,使用Spring的CommonsMultiparResolver -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="5000000"/>
                <property name="defaultEncoding" value="UTF-8"/>
        </bean>
  • 相关阅读:
    [HAOI2008] 硬币购物
    [HNOI2002] Kathy 函数
    [SCOI2009] windy数
    圆方树总结
    ABOUT ME && 友链
    逝念偶拾
    文化课日常
    记一些欢愉
    浅谈
    洛谷 4823 [TJOI2013]拯救小矮人
  • 原文地址:https://www.cnblogs.com/zk753159/p/5018318.html
Copyright © 2020-2023  润新知