文件上传回顾
文件上传条件
1、from表单的enctype属性必须为:“multipart/form-date”
2、表单的提交数据为:post
3、提供一个文件的选择域<input type=“file” / >
文件上传依赖组件
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
详细参考javaweb文件上传
public class UploadImpl {
//文件上传方法
@RequestMapping("/fileUpload")
public String fileUpload(HttpServletRequest request)throws Exception{
System.out.println("文件上传方法执行了");
//使用fileUpload组件完成文件上传
//设定文件上传的目标目录
String Path = request.getSession().getServletContext().getRealPath("/upload");
//若目录不存在则创建文件上传的位置
File file = new File(Path);
if (!file.exists()){
//不存在则创建该文件夹
file.mkdirs();
}
//解析request对象,获取上传文件项
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
//通过upload.parserRequest解析form中的请求字段,并将返回值保存到items对象中
List<FileItem> items = upload.parseRequest(request);
System.out.println(items);
for(FileItem item:items){
//进行判断,当前文件项是否是文件上传请求
if(item.isFormField()){
//当前item是普通表单数据
String fieldName = item.getFieldName();
}else{
//是文件上传项
//获取文件名称
String name = item.getName();
System.out.println(name);
//文件上传
File file1 = new File(Path, name);
item.write(file1);
//删除临时文件
item.delete();
}
}
System.out.println("上传路径为"+Path);
return "success";
}
}
SpringMVC中的文件上传
1、依赖:springMVC的文件解析器(id值是固定的multipartResolver)
<!--配置文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置最大上传大小-->
<property name="maxUploadSize" value="10485760"/>
</bean>
2、控制器参数列表中额外添加MultipartFile upload,注意:该MultipartFile 参数名必须和前端文件请求的name保持一致!。
@RequestMapping("/SpringfileUpload")
public String SpringfileUpload(HttpServletRequest request, MultipartFile upload)
3、获取文件名称:upload.getOriginalFilename();
4、文件的上传:upload.transferTo(new File(Path,fileName));
源代码:
@RequestMapping("/SpringfileUpload")
public String SpringfileUpload(HttpServletRequest request, MultipartFile upload)throws Exception{
//使用fileUpload组件完成文件上传
//设定文件上传的目标目录
String Path = request.getSession().getServletContext().getRealPath("/upload");
//若目录不存在则创建文件上传的位置
File file = new File(Path);
if (!file.exists()){
//不存在则创建该文件夹
file.mkdirs();
}
//获取文件名称
String fileName=upload.getOriginalFilename();
System.out.println(fileName);
//文件上传
upload.transferTo(new File(Path,fileName));
System.out.println("上传路径为"+Path);
return "success";
}
SpringMVC跨服务器上传
实际项目中,往往存在响应请求服务器和上传文件服务器等多个服务器,请求服务器响应客户端的请求,接收文件上传的确实文件服务器。SpringMVC跨服务器上传可以解决
实现步骤
1、实现跨服务器文件上传至少需要两个服务器,创建两个服务器。注意端口号不能重复。
2、在请求服务的工程中导入开发jar包,jersey-core、jersey-client
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
3、获取文件名称:upload.getOriginalFilename();
4、创建客户端对象
Client client=Client.create();
5、与文件服务器进行连接
WebResource webResource = client.resource(path + fileName);
6、完成上传
webResource.put(upload.getBytes());
完整代码:
@RequestMapping("/disServerUpload")
public String disServerUpload( MultipartFile upload)throws Exception{
//使用fileUpload组件完成文件上传
//定义上传文件服务器路径
String path="http://localhost:8888/fileupload/uploads/";
//获取文件名称
String fileName=upload.getOriginalFilename();
System.out.println(fileName);
//创建一个客户端
Client client=Client.create();
//客户端与文件服务器连接
WebResource webResource = client.resource(path + fileName);
//上传
webResource.put(upload.getBytes());
return "success";
}
springMVC跨服务器文件上传报错:
400:提交的文件中含有中文名,
404:文件上传的项目路径出错
405:tomcat服务器的web.xml中配置 只读 属性为否
409:文件服务器中存放文件的路径不存在
异常处理
web工程中,出现异常默认的是将异常抛给上一级,一直到控制层,若控制层不做处理,则默认将异常抛给前端,这样对软件使用者是不友好的。SpringMVC中,在前端控制器层配置异常处理器组件。
SpringMVC异常处理步骤
1、编写自定义异常类(显示提示信息)
定义一个异常类,继承exception接口
2、编写异常处理器
定义一个异常处理器,实现HandlerExceptionResolver
//HandlerExceptionResolve接口实现的方法
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {
代码实现:
从程序中获得异常对象,对异常进行处理
创建ModelAndView对象,存入该异常对象
返回ModleAndView对象
public class SysExceptionSlover implements HandlerExceptionResolver {
/**
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param ex
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {
//获得异常对象
SysException sysException=null;
if(ex instanceof SysException){
sysException=(SysException)ex;
}else
sysException=new SysException("系统正在维护");
//创建ModelAndView对象并返回
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("SysException",sysException);
modelAndView.setViewName("error");
return modelAndView;
}
}
3、配置异常处理器
@component或者配置bean标签。