网站运营谁都可以做吗,深圳最好的网站建设,cms是网站吗,net快速建站1 单文件上传 在程序开发中#xff0c;有时候需要上传一些文件。我们在学习Servlet的时候#xff0c;也做过文件上传的操作#xff0c;只不过基于Servlet的文件上传操作起来过于复杂#xff0c;因此所有的MVC框架都提供了自己的文件上传操作#xff0c;基本上都是基于File…1 单文件上传 在程序开发中有时候需要上传一些文件。我们在学习Servlet的时候也做过文件上传的操作只不过基于Servlet的文件上传操作起来过于复杂因此所有的MVC框架都提供了自己的文件上传操作基本上都是基于FileUpload的文件上传。 Spring MVC在处理文件上传的时候有自己的处理方法但是也是基于FileUpload的操作因此在处理文件上传的时候也需要导入commons-fileupload-1.2.2.jar包和commons-io-2.4.jar包。 在操作的时候首先需要在配置文件中配置Spring MVC文件上传功能具体代码如下。
!-- 设置了multipartResolver才能完成文件上传 --
bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver!-- 文件上传字符编码 --property namedefaultEncoding valueUTF-8/property!-- 设置文件上传的大小,单位是字节 --property namemaxUploadSize value2000000/property
/bean接下来需要一个表单用来执行选择文件操作首先在控制器中添加方法用来跳转到文件上传页面具体代码如下。
RequestMapping(value/uploadInput, methodRequestMethod.GET)
public String upload() {return upload;
}在WEB-INF/jsp目录下创建upload.jsp页面并添加如下代码。
form actionupload methodpost enctypemultipart/form-dataname:input typetext namenamebrfile:input typefile namefileNamebrinput typesubmit value提交
/form页面效果如图所示。 在控制器中只需要在处理方法中加入参数MultipartFile就可以实现文件上传了。编写处理方法代码内容如下。
RequestMapping(value/upload, methodRequestMethod.POST)
public String upload(String name, MultipartFile file, HttpServletRequest req) {System.out.println(name);//获取表单域的名字System.out.println(file.getName());//获取文件原始的名字System.out.println(file.getOriginalFilename());//获取文件的类型System.out.println(file.getContentType());//获取文件上传的路径String realpath req.getSession().getServletContext().getRealPath(/upload);//创建文件对象File f new File(realpath / file.getOriginalFilename());try {//文件上传
FileUtils.copyInputStreamToFile(file.getInputStream(), f);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return upload;
}注意方法中MultipartFile对象的名字必须和文件表单域的名字一致否则会报错。 在WebRoot目录下创建文件夹upload访问http://localhost:8080/springmvc/uploadInput选择文件就可以正常上传文件了。
2 多文件上传 在实际的开发中有时要同时上传多个文件在处理的时候只需要稍作修改就可以了。首先需要修改文件上传页面代码如下所示。
form actionupload methodpost enctypemultipart/form-dataname:input typetext namename size30brbrfile:input typefile namefilesbrbrfile:input typefile namefilesbrbrfile:input typefile namefilesbrbrinput typesubmit value提交
/form页面显示的效果如图所示。 在控制器中的处理也非常简单只需要把对象改为一个数组即可代码修改如下。
RequestMapping(value/upload, methodRequestMethod.POST)
public String upload(String name, MultipartFile files[], HttpServletRequest req) {//获取文件上传的路径String realpath req.getSession().getServletContext().getRealPath(/upload);for(MultipartFile file:files) {//在多文件上传时,防止有的文件表单域没有选择文件if(file.isEmpty()) {continue;}//创建文件对象File f new File(realpath / file.getOriginalFilename());try {//文件上传 FileUtils.copyInputStreamToFile(file.getInputStream(), f);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return success;
}此时选择多个文件也能同时完成上传即使有的表单域没有选择文件也是可以的同时这种方法同样也能完成单文件的上传。不过这样做也有点费事在实际开发中往往都是通过插件使在一个选择框中可以同时选择若干文件实现同时上传。这一点在HTML5中可以通过multiple属性来实现在一个文件表单域中可接受多个值的文件上传在HTML4中仍然可以使用具体代码如下所示。
form actionupload methodpost enctypemultipart/form-dataname:input typetext namename size30brbrfile:input typefile namefiles multiplemultiplebrbrinput typesubmit value提交
/form页面效果如图所示。 后台接收文件的处理代码不用修改可以直接使用。