1.首先要确定工程里有commons-fileupload-1.2.2和commons-io-2.1.jar。如没有,在pom.xml里添加依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
2.对spring配置文件进行修改(beans.xml)
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
3.controller类编写
@RequestMapping(value="upload.htm",method = RequestMethod.POST)
public @ResponseBody Map<String, Object> fileUpload(HttpServletRequest request,
HttpServletResponse response){
//request.getParameter("crudflag");
Map<String,Object> filemap = new HashMap<String,Object>();
String result ="";
try{
// 转型为MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得文件:
MultipartFile file = multipartRequest.getFile("file");
// 获得文件名:
String filename = file.getOriginalFilename();
//文件存放地址(一般情况是在tomca项目文件下建立一个upload文件夹,用于存放上传的文件)
String filePath = request.getSession().getServletContext().getRealPath("/upload");
//文件大小
int filesize = request.getContentLength();
DecimalFormat df = new DecimalFormat("#.0");
String fileSize = "";
if(filesize>=1024*1024){
fileSize = df.format(filesize/1048576)+"M";
}
else if (filesize>=1024){
fileSize = df.format(filesize/1024)+"K";
}
else {
fileSize = df.format(filesize)+"B";
}
//文件后缀名
String filetype = filename.substring(filename.lastIndexOf(".")+1, filename.length());
//文件上传时间
Date dt=new Date();
SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = matter1.format(dt);
//写入文件
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath, filename));
<此处为把文件的相关字段添加进入数据库,可根据自已的实际情况进行修改>
//文件信息入数据库
Map<String, Object> requestMap = stringUtil.getParamsForJqgrid(request);
Map<String, Object> paraMap = (Map<String, Object>) requestMap.get("paraMap");
paraMap.put("file_name", filename);
paraMap.put("file_size", fileSize);
paraMap.put("file_path", filePath);
paraMap.put("file_type", filetype);
paraMap.put("file_time", date);
filemap = fileuploadservice.add(paraMap);
result = "文件上传成功";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = "文件上传失败";
}
filemap.put("desc", result);
return filemap;
}
4.js文件
$(document).ready(function(){
$("#Form").ajaxForm({
url:"<%=basePath%>upload.htm",
type:"post",
success:function (data){
data = $.parseJSON(data);
alert(data.desc);
if(data.desc!=""){
$("#fileInfo").val("");
}
},
error:function(data){
alert("未知异常");
}
});
});
function dosubmit(){
var fileName = $('#fileInfo').val();
if("" == fileName) {
alert("请选择上传文件");
return;
}
$("#Form").submit();
}
5.jsp文件
首先要添加<script src="lib/js/jquery.form.js" type="text/javascript"></script> 没有的话自行下载jquery.form.js文件
<form id="Form" method="post" enctype="multipart/form-data" >
<input id="fileInfo" type="file" name="file" />
<input id="But" type="button" value="上传" οnclick="dosubmit()"/>
</form>