以保存客户为例子
public String save() throws IOException{
// 做文件的上传,说明用户选择了上传的文件了
if(uploadFileName != null){
// 打印
System.out.println("文件类型:"+uploadContentType);
// 把文件的名称处理一下
String uuidname = UploadUtils.getUUIDName(uploadFileName);
// 把文件上传到如 D:\\apache-tomcat-7.0.52\\webapps\\upload
//获取应用的真实根路径
String realPath = ServletActionContext.getServletContext().getRealPath("/");
//获取webapps之前的字符串
realPath=realPath.substring(0, realPath.indexOf("webapps"));
//创建带文件名的真实路径
String path = realPath+"webapps\\upload\\"+uuidname;
// 创建file对象
File file = new File(path);
// 简单方式
FileUtils.copyFile(upload, file);
// 把上传的文件的路径,保存到客户表中
customer.setFilepath(path);
}
// 保存客户成功了
customerService.save(customer);
return "save";
}