最近一直在网上看资料,今天我在网上发现一个demo,是基于springMVC开发的上传。由于本人刚好这两天在学习springMVC。以前用struts2.0 做过多文件上传的,现在我把这springMVC的多文件上传来分享给大家。我是根据http://zhoshijie.iteye.com/blog/1177390 给的demo完成的啊,希望原著能支持!!!我来简单讲解一下。
lib包
目录结构:
spring-servlet-xml***********************************
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
UploadFileController.java******************************
package com.len.trans.controller;
import java.io.FileOutputStream;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.len.trans.common.Constant;
import com.len.trans.util.PropertiesUtil;
@Controller//声明该类为控制器类
@RequestMapping("/upload")
public class UploadFileController {
/**
* 进入上传页面
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value="/toUpload")//将文件上传请求映射到该方法
public ModelAndView toUpload() throws Exception{
return new ModelAndView("/upload");//返回上传视图
}
/**
* 文件上传
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value="/uploadFile",method=RequestMethod.POST)//将文件上传请求映射到该方法
public ModelAndView uploadFile(MultipartHttpServletRequest request) throws Exception{
PropertiesUtil pUtil=PropertiesUtil.createPropertiesUtil(Constant.UPLOADPATH_FILE);
List
String uploadpath=request.getSession().getServletContext().getRealPath(pUtil.getProperty(Constant.UPLOADPATH_PATH));
System.out.println("uploadpath :"+uploadpath);
for (MultipartFile file : files) {
if (file.isEmpty()) continue;
FileOutputStream fileOS=new FileOutputStream(uploadpath+"/"+file.getOriginalFilename());
fileOS.write(file.getBytes());
fileOS.close();
}
return new ModelAndView("/success");//返回成功视图
}
一下这段主要是告诉大家另外一种传参的方式,上面是用request取的: @RequestParam("name") String name, //设置请求参数的名称和类型 @RequestParam("file") CommonsMultipartFile mFile
// public String handleFormUpload(@RequestParam("name") String name, //设置请求参数的名称和类型
// @RequestParam("file") CommonsMultipartFile mFile) { //请求参数一定要与form中的参数名对应
// if (!mFile.isEmpty()) {
// String path = this.servletContext.getRealPath("/tmp/"); //获取本地存储路径
// File file = new File(path + new Date().getTime() + ".jpg"); //新建一个文件
// try {
// mFile.getFileItem().write(file); //将上传的文件写入新建的文件中
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
upload.jsp**********************************************
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>