// 文件上传需要的三个属性:
private File upload;
private String uploadFileName;
@SuppressWarnings("unused")
private String uploadContentType;import org.apache.commons.io.FileUtils;
// 保存商品的方法:
public String save() throws IOException {
// 将提交的数据添加到数据库中.
product.setPdate(new Date());
// product.setImage(image);
if(upload != null){
// 将商品图片上传到服务器上.
// 获得上传图片的服务器端路径.
String path = ServletActionContext.getServletContext().getRealPath(
"/products");
// 创建文件类型对象:
File diskFile = new File(path + "//" + uploadFileName);
// 文件上传:
FileUtils.copyFile(upload, diskFile);
product.setImage("products/" + uploadFileName);
}
productService.save(product);
return "saveSuccess";
}
// 修改商品的方法
public String update() throws IOException {
// 将信息修改到数据库
product.setPdate(new Date());
// 上传:
if(upload != null ){
String delPath = ServletActionContext.getServletContext().getRealPath(
"/" + product.getImage());
File file = new File(delPath);
file.delete();
// 获得上传图片的服务器端路径.
String path = ServletActionContext.getServletContext().getRealPath(
"/products");
// 创建文件类型对象:
File diskFile = new File(path + "//" + uploadFileName);
// 文件上传:
FileUtils.copyFile(upload, diskFile);
product.setImage("products/" + uploadFileName);
}
productService.update(product);
// 页面跳转
return "updateSuccess";
}
700

被折叠的 条评论
为什么被折叠?



