当前位置: 首页 > news >正文

来宾市网站建设_网站建设公司_React_seo优化

有没有咨询求助做任务的网站,龙岗爱联网站建设,保定网站建设工作,wordpress怎么编写用户中心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
http://www.ihoyoo.com/news/37740.html

相关文章:

  • 网站开发邮件品牌宣传文案范文
  • 网站设计 品牌设计国际新闻最新消息今天233
  • 网站后台管理系统摘要怎么写公司企业邮箱怎么登陆
  • 网站设计的主要机构有哪些进入网站wordpress配置
  • 网站重构案例哈尔滨公告
  • 电子商务网站模板免费下载word模板免费网站
  • 网站验证码怎么做的seo和sem是干什么的
  • 门户网站建设情况简述网站建设的基本思路
  • 哈尔滨php网站开发公司网站优化用户体验
  • 建设个人购物网站计算机网站建设论文
  • 移动网站 案例让网站降权
  • 门户网站开发技术 知乎wordpress文件上传插件
  • 自己学做网站需要学多久网站开发工资怎么样
  • 怎么查询网站开通时间怎么清理网站后门文件
  • 阳江市网站建设网站建设调研报告
  • 杭州网站建设哪个平台好建设网站服务费会计分录
  • 鞋网站模版建筑公司资质甲级乙级
  • 自己做网站需要什么软件下载sharepoint做网站
  • 做企业网站要怎么设计方案板瓦工安装wordpress
  • 做炫光素材的网站重庆旅游网站
  • 云南凡科建站哈尔滨建设工程造价信息网
  • 网站换稳定服务器wordpress中page与post
  • 唐山网站建设哪家优惠平面设计网站有哪些比较好的
  • 台州网站建设咨询薇个人简历网站模板免费
  • 专业网站建设平台1688官网下载
  • 上海市工程建设信息网官方网站企业做什么需要有网站
  • 网站建设下坡路网站建设风险是什么意思
  • 美橙网站建设南的外贸网站定制
  • 徐州建设局规划网站鲜花网站模板下载
  • 佛山企业做网站世界十大著名服装设计师