@RequestMapping(value = "/save.do", method = RequestMethod.POST)
public String saveDriver(
@RequestParam(value = "photo", required = false) MultipartFile filedata,
Driver driver, Model model) {
trimDriver(driver);
model.addAttribute(driver);
// 判断图片大小是否大于2M
if (filedata.getSize() > Constant.UPLOAD_PHOTO_SIZE) {
model.addAttribute("isSaveSuccess", false);
IMessage msg = new Message("MSG_YS030_002", MsgTypeEnum.INFO, null);
model.addAttribute("msg", msg.toString());
return "ys030/YS033";
}
// 判断司机是否已存在
if (this.ys030Service.isExist(driver.getOrganizationId(),
driver.getIdCardNumber(), null)) {
model.addAttribute("isSaveSuccess", false);
IMessage msg = new Message("ERR_YS030_003", MsgTypeEnum.ERROR, null);
model.addAttribute("msg", msg.toString());
return "ys030/YS033";
}
// 保存相对路径到数据库 图片写入服务器
if (filedata != null && !filedata.isEmpty()) {
// 获取图片的文件名
String fileName = filedata.getOriginalFilename();
// 获取图片的扩展名
String extensionName = fileName
.substring(fileName.lastIndexOf(".") + 1);
// 新的图片文件名 = 获取时间戳+"."图片扩展名
String newFileName = String.valueOf(System.currentTimeMillis())
+ "." + extensionName;
try {
driver.setPicUrl(newFileName);
saveFile(newFileName, filedata);
} catch (Exception e) {
log.error("上传图片失败.", e);
model.addAttribute("isSaveSuccess", false);
IMessage msg = new Message("MSG_YS030_001", MsgTypeEnum.INFO,
null);
model.addAttribute("msg", msg.toString());
return "ys030/YS033";
}
}
this.ys030Service.save(driver);
model.addAttribute(driver);
model.addAttribute("isSaveSuccess", true);
return "ys030/YS033";
}
private void saveFile(String newFileName, MultipartFile filedata) {
// TODO Auto-generated method stub
// 根据配置文件获取服务器图片存放路径
String picDir = "";
try {
//这里封装了读取配置文件的方法 配置文件中有图片的存放地址和获取地址
Properties properties = PropertiesUtil
.getProperties("configure/driverpicurl.properties");
picDir = properties.getProperty("savePicUrl");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String saveFilePath = picDir;
/* 构建文件目录 */
File fileDir = new File(saveFilePath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
try {
FileOutputStream out = new FileOutputStream(saveFilePath + "\"
+ newFileName);
// 写入文件
out.write(filedata.getBytes());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void deleteFile(String oldPicName) {
// TODO Auto-generated method stub
String picDir = "";
try {
Properties properties = PropertiesUtil
.getProperties("configure/driverpicurl.properties");
picDir = properties.getProperty("savePicUrl");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* 构建文件目录 */
File fileDir = new File(picDir+"/"+oldPicName);
if (fileDir.exists()) {
//把修改之前的图片删除 以免太多没用的图片占据空间
fileDir.delete();
}
}
这是properties 文件
#============================================================================
#savePicUrl=D:/tomcat/apache-tomcat-7.0.39/webapps/driverPic 服务器图片存放路径(服务器绝对路径与工程同目录)
#getPicUrl=http://xxx.xxx.xx.xxx:8080/driverPic/ 服务器图片获取地址
#============================================================================
savePicUrl=D:/tomcat/apache-tomcat-7.0.39/webapps/driverPic
getPicUrl=http://xxx.xxx.xx.xxx:8080/driverPic/