城市联盟网站怎么做,seo网站推广价格,云端商城买流量,中国建设教育协会的官方网站spring mvc 文件上传 一、单文件上传 配置步骤#xff1a; 步骤一、在配置文件中配置包扫描器#xff08;暂且这样配#xff0c;会出问题#xff0c;我们下面说解决方案#xff09; ?xml version1.0 encodingUTF-8?
beans xmlns… spring mvc 文件上传 一、单文件上传 配置步骤 步骤一、在配置文件中配置包扫描器暂且这样配会出问题我们下面说解决方案 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd !--让spring扫描包下所有的类让标注spring注解的类生效 --context:component-scan base-packagecn.hmy.controller//beans 步骤二定制我们的文件上传jsp页面 % page languagejava importjava.util.* pageEncodingutf-8%
%
String path request.getContextPath();
String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadbase href%basePath%title文件上传/title/headbodyform action${pageContext.request.contextPath }/list.do methodpost enctypemultipart/form-datah1文件上传/h1文件:input typefile namefileUpLoad//brinput typesubmit value上传/ /form/body
/html 步骤三、书写我们的处理器代码 package cn.hmy.controller;import java.io.File;
import java.io.IOException;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;import cn.hmy.pojo.UserInfo;Controller
public class MyController{//处理器方法RequestMapping(value/list.do) public void doFirst(MultipartFile fileUpLoad,HttpSession session) throws Exception{//1.获取文件名称String filename fileUpLoad.getOriginalFilename();//2.获取文件的前半部分路径String realPath session.getServletContext().getRealPath(/images);//3.拼接成完整的路径File filenew File(realPath,filename);//4.保存文件fileUpLoad.transferTo(file); }
} 此时我们启动服务器进行代码上传会报如下错误 解决方案更改我们的spring-servlet.xml配置文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd !--让spring扫描包下所有的类让标注spring注解的类生效 --context:component-scan base-packagecn.hmy.controller/!--配置复杂类型表达解析器-- bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver/bean/beans 启动发现还是会报上述错误 解决方案配置注解驱动 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd !--让spring扫描包下所有的类让标注spring注解的类生效 --context:component-scan base-packagecn.hmy.controller/ !--配置复杂类型表达解析器--bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver/bean!--配置注解驱动-- mvc:annotation-driven/
/beans 如果在上述配置文件中缺少复杂类型解析器会报如下错误 在解决了以上错误后我们会发现如果我们上传的文件中含有中文 会出现乱码现象 解决乱码 解决方案我们暂且提供以下两种方式 方案一、 方案二、在web.xml中配置 filterfilter-namecharacterEncoding/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueutf-8/param-value/init-paraminit-paramparam-nameforceEncoding/param-nameparam-valuetrue/param-value/init-param/filterfilter-mappingfilter-namecharacterEncoding/filter-nameurl-pattern/*/url-pattern/filter-mapping 如何控制文件上传大小 在spring-servlet.xml中配置如下 maxUploadSize为上传的总文件的大小 5MmaxInMemorySize为上传的单个文件的大小 1M bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolverproperty namedefaultEncoding valueutf-8/propertyproperty namemaxUploadSize value5000000/propertyproperty namemaxInMemorySize value1000000/property/bean 如果超出所限制的大小 会报如下错误 控制文件上传的类型通过后缀名进行控制在处理器中进行判定 例如 只允许上传.jpg .png .gif为后缀的文件 package cn.hmy.controller;import java.io.File;
import java.io.IOException;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;import cn.hmy.pojo.UserInfo;Controller
public class MyController{//处理器方法RequestMapping(value/list.do) public String doFirst(MultipartFile fileUpLoad,HttpSession session) throws Exception{//1.获取文件名称String filename fileUpLoad.getOriginalFilename();//限定文件上传的类型if(filename.endsWith(jpg)||filename.endsWith(png)||filename.endsWith(gif)){//2.获取文件的前半部分路径String realPath session.getServletContext().getRealPath(/images);//3.拼接成完整的路径File filenew File(realPath,filename);//4.保存文件fileUpLoad.transferTo(file); }else{System.out.println(不支持上传文件的类型);}return /list.jsp;}
} 如何判断用户没用选上传文件 多文件上传 步骤一、 spring-servlet.xml配置文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd !--让spring扫描包下所有的类让标注spring注解的类生效 --context:component-scan base-packagecn.hmy.controller/bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolverproperty namedefaultEncoding valueutf-8/propertyproperty namemaxUploadSize value5000000/propertyproperty namemaxInMemorySize value1000000/property/beanmvc:annotation-driven/
/beans 步骤二、准备多文件上传的jsp页面 % page languagejava importjava.util.* pageEncodingutf-8%
%
String path request.getContextPath();
String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadbase href%basePath%title文件上传/title/headbodyform action${pageContext.request.contextPath }/list.do methodpost enctypemultipart/form-datah1文件上传/h1文件1:input typefile namefileUpLoad//br文件2:input typefile namefileUpLoad//br文件3:input typefile namefileUpLoad//brinput typesubmit value上传/ /form/body
/html 步骤三、编写处理器的代码 //多文件上传RequestMapping(value/list.do) public String doFirst2(RequestParam MultipartFile[] fileUpLoad,HttpSession session) throws Exception{for (MultipartFile item : fileUpLoad) {//1.获取文件名称String filename item.getOriginalFilename();//限定文件上传的类型if(filename.endsWith(jpg)||filename.endsWith(png)||filename.endsWith(gif)){//2.获取文件的前半部分路径String realPath session.getServletContext().getRealPath(/images);//3.拼接成完整的路径File filenew File(realPath,filename);//4.保存文件item.transferTo(file); }else{System.out.println(不支持上传文件的类型);}}return /list.jsp;} 注意在处理器方法中一定要对参数进行校对使用注解RequestParam校正参数 丢到 会报错 即可实现多文件上传 转载于:https://www.cnblogs.com/hmy-1365/p/6104501.html