spring mvc的异常与文件上传

1.异常:

spring注解版的异常有局部异常和全局异常
        
       1.局部异常对单个controller有效;(在controller类写一个处理异常的方法)

@ExceptionHandler(value={UserException.class})
public String handlerExceptionTest(UserException e ,HttpServletRequest request){
request.setAttribute("msg", "用户不存在"); return "error";
}

2.全局异常对所有的controller有效(在配置文件中配置;用于所有的controller)

   <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="po.UserException">error</prop>
</props>
</property>
</bean>

//上边两种方法中使用的异常类UserException是我们自己创建的,继承自一个异常类.  (我通常extends(继承)  RuntimeException或Exception)

2.文件上传

//页面

form表单设置enctype属性

      <form action="/SpringOne/login" method="post" enctype="multipart/form-data" class="hehe">
用户名<input type="text" name="name"/>
密码<input type="text" name="pwd"/>
文件 <input type="file" name="attach"/>
<input type="submit" value="提交"/>
</form>

//

在配置文件(xxx-servlet.xml)中配置如下代码:

      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

           <property name="maxUploadSize" value="5000000">

           </property>

      </bean>

//controller中

    @RequestMapping(value="/login",method=RequestMethod.POST)
public String login(String name,String pwd,HttpSession session,MultipartFile attach) throws UserException, IOException{
boolean flag=true;
if(name!=null && !name.equals("")){
for (User item : list) {
if(name.equals(item.getName())){
//model.addAttribute("user",item);
session.setAttribute("user", item); flag= false; }
}
}
//为true抛出局部异常
if(flag){
throw new UserException();
}
//文件上传
System.out.println(session.getServletContext().getRealPath("/static/upoload")); FileUtils.copyInputStreamToFile(attach.getInputStream(),new File(session.getServletContext().getRealPath("/static/upoload")+File.separator+attach.getOriginalFilename())); return "redirect:/hello"; }

Spring Mvc 笔记二之异常和文件上传的更多相关文章

  1. Spring MVC-表单(Form)标签-文件上传(File Upload)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_upload.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显 ...

  2. Spring MVC3.2 通过Servlet3.0实现文件上传

    Servlet3.0规范增加了对文件上传的原生支持,这里记录一下Spring MVC3通过Servlet3上传文件的实现. 配置文件: applicationContext.xml <!-- s ...

  3. JavaEE系列之(二)commons-fileupload实现文件上传、下载

    一.文件上传概述     实现Web开发中的文件上传功能,需要两步操作:     1.在Web页面中添加上传输入项 <form action="#" method=" ...

  4. java web学习总结(二十四) -------------------Servlet文件上传和下载的实现

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  5. MVC+WCF框架下广告位管理——文件上传

    广告位是站点中不可缺少的内容之中的一个.也是能直接给我们站点带来经济收益的内容之中的一个. 好的广告位不仅不会强宾压主,而会为我们的站点锦上添花.起到画龙点睛的作用.因此设计好广告位也是开发过程中一大 ...

  6. CodeIgniter学习笔记五:分页,文件上传,session,验证码

    一.分页 示例代码: //装载类文件 $this -> load -> library('pagination'); $controller = $this->router-> ...

  7. Grails笔记三:完整的文件上传实例

    文件上传在web应用中是比较普遍的,相对于使用jsp等技术实现文件上传,Grails的文件上传着实让人喜爱,因为极其简单,让人看一遍就容易轻松记住!不多说,实例如下: 假设已有一个名为uploadFi ...

  8. Struts(二十六):文件上传

    表单的准备 想要使用html表单上传一个或多个文件 1.须把html表单的enctype属性设置为multipart/form-data 2.须把html表单的method属性设置为post 3.须添 ...

  9. JSP学习笔记(四):文件上传

    JSP 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器.上传的文件可以是文本文件或图像文件或任何文档.我们使用 Servlet 来处理文件上传,使用到的文件有: upload.j ...

随机推荐

  1. javascript数组操作汇总

    javascript之数组操作 - 不悔的青春 - 博客园 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array( ...

  2. Java时间转换类实现

    Java时间类型非常的差,首先版本问题,本人使用java在Android平台开发.很多Data类的方法已经不提倡使用,一个时间,居然要使用Calendar.DateFormat等类共同编码,非常麻烦. ...

  3. Python 学习入门(28)—— 服务器实例

    在新的Python 3.x中,BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer整合到http.server包,SocketServer改名为sockets ...

  4. ListBox基础

    关键点   在列表框生成后需要向其中加入或是删除行,可以利用: int AddString( LPCTSTR lpszItem )添加行, int DeleteString( UINT nIndex ...

  5. Delphi中DLL的其他应用

    http://blog.csdn.net/zhenghui1/article/details/6618273 1.DLL的入口函数和出口函数 在编写DLL时可以在DLL项目文件的begin..end之 ...

  6. 在Java项目中整合Scala

    Scala是一个运行在Java JVM上的面向对象的语言.它支持函数编程,在语法上比Java更加灵活,同时通过Akka库,Scala支持强大的基于Actor的多线程编程.具有这些优势,使得我最近很想在 ...

  7. Java基础知识强化之IO流笔记55:IO流练习之 自定义类模拟LineNumberReader的获取行号功能案例

    1. 自定义类模拟LineNumberReader的获取行号功能案例 2. 代码实现: (1)MyBufferedReader.java: package cn.itcast_08; import j ...

  8. start mysqld on Mac server

    #!/bin/sh # Source the common setup functions for startup scripts test -r /etc/rc.common || exit 1 . ...

  9. kindle paperwhite2 root 密码修改方法

    昨天由于kindle的耗电量突然增大,开始查找原因.经过检查搜索后,确定是由于卡索引的问题导致,于是开始解决这个问题.然而在通过ssh以root身份登陆到kindle上时,始终出现登陆错误,提示密码不 ...

  10. 8.LNMP环境的配置

    LNMP环境的配置 参照文档:https://oneinstack.com/install/ 安装文件位置:/data/soft: ```yum -y install wget screen pyth ...