
@H_404_7@和单个上传文件基本相同,就是需要在后台控制器中,用数组来接收 Jsp页面提交过来的file数据。
也分为三个部分演示。
一、Jsp
<%-- Created by IntelliJ IDEA. User: administrator Date: 2019/8/12 Time: 14:32 To change this template use file | Settings | file Templates.--%><%@ page ContentType="text/HTML;charset=UTF-8" language="java" %><HTML><head> <Title>多文件上传</Title></head><%-- 多文件上传 Jsp 部分 1.在form 标签中定义属性 enctype="multipart/form-data" 2.在上传的input中定义属性 multiple="multiple"--%><body> <form action="../test" method="post" enctype="multipart/form-data"> <div>上传文件:<input type="file" name="file" multiple="multiple"></div> <div> <input type="submit" value="上传" /> </div> </form></body></HTML>
二、后台控制器
package com.aaa.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapPing;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.multipartfile;import javax.servlet.http.httpServletRequest;import java.io.file;import java.io.IOException;/*** 多文件上传 1.Jsp 2.后台控制层** 将接受的 file 属性 定义成一个数组* [email protected]("file")multipartfile [] files** 2.2 通过for循环 将得到的结果 便利出来。* */@Controllerpublic class TestController { @RequestMapPing("/test") public String test(@RequestParam("file")multipartfile [] files,Model model,httpServletRequest request) throws IOException { for (multipartfile file:files ) { if (!file.isEmpty()){ //获得上传的目标位置 String path = request.getSession().getServletContext().getRealPath("/static/upload"); //目标文件的对象 String filename = file.getoriginalfilename(); file file1 = new file(path + "/" + filename); //父级目录 不在 就创建一个 if (!file1.getParentfile().exists()){ file1.mkdirs(); } //数据库没有的文件 才上传 if (!file1.exists()){ file.transferTo(file1); model.addAttribute("file",filename); } }else { model.addAttribute("file","上传文件为空"); } } return "vIEw/ok1"; }}
三、接收数据的Jsp页面。
<%-- Created by IntelliJ IDEA. User: administrator Date: 2019/8/12 Time: 14:45 To change this template use file | Settings | file Templates.--%><%@ page ContentType="text/HTML;charset=UTF-8" language="java" %><HTML><head> <Title>接收数据</Title></head><body> 上传的文件:${file}</body></HTML>总结
以上是内存溢出为你收集整理的from 表单上传多个文件?全部内容,希望文章能够帮你解决from 表单上传多个文件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)