springMVC文件上传
需要在spring-mvc.xml配置文件中配置文件上传解析器
<!--配置文件上传解析器-->
<!—id写法固定,一定要写-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="90000000000" />
</bean>
上传时通过
.getOriginalFilename()
方法获取到文件名
然后通过
.transferTo(new File(“D:\” + filename))
以filename为文件名上传到D盘根目录
Springmvc异常处理
处理思路:系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器 交由异常处理器进⾏异常处理。
/**
* controller内部异常处理
* 在这个controller任一方法中,只要出现了在@ExceptionHandler(Exception.class)
* 中定义的异常,就会被该方法捕获,然后运行该方法
*/
@ExceptionHandler(Exception.class)
public String exceptionHandler(Exception e){
//对异常的处理
//打印该异常信息
System.out.println(e.getMessage());
return "error";
}
这个方法只适用于同一个@controller注解类中的方法
可以设置一个全局异常处理类
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(Exception.class)
public String aexceptionHandler(Exception e){
return "error";
}
}
有内部异常处理就先内部,没有才会使用全局