[java]代码库
/**
*
* 修改
* @throws Exception
* @throws Exception
*
*/
//带图片的增删改查
//删除原来的图片
//上传新图片
//不修改原来图片获取原来图片
public String update() throws Exception {
File file = this.getFile();
if(file != null && !("").equals(toGongQiuShangJiaXinXi.getGimge()) ) { //如果不存在(没有改变) 获取原来图片的路径
String oldpath = ServletActionContext.getServletContext().getRealPath("/upload/");
System.out.println(oldpath);
File oldImageFile = new File(oldpath + File.separator + toGongQiuShangJiaXinXi.getGimge());
if (oldImageFile.exists()) {
oldImageFile.delete();
}
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
String newpath = ServletActionContext.getServletContext().getRealPath("upload/"+ fileFileName);
System.out.println(newpath);
FileOutputStream fos = new FileOutputStream(newpath);
FileInputStream fis = new FileInputStream(file);
byte[] butter = new byte[1024];
int len = 0;
while ((len = fis.read(butter))!= -1) {
fos.write(butter,0,len);
}
fos.close();
fis.close();
toGongQiuShangJiaXinXi.setGimge(fileFileName);
} else {
if (toGongQiuShangJiaXinXi.getGimge().equals(1)) {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
//图片没有修改时,输入框中的值为NULL,获取原来图片的地址在上传
String path = ServletActionContext.getServletContext().getRealPath("upload/"+ toGongQiuShangJiaXinXi.getGimge());
System.out.println(path);
FileOutputStream fos = new FileOutputStream(path);
FileInputStream fis = new FileInputStream(file);
byte[] butter = new byte[1024];
int len = 0;
while ((len = fis.read(butter))!= -1) {
fos.write(butter,0,len);
}
fos.close();
fis.close();
toGongQiuShangJiaXinXi.setGimge(fileFileName);
} else {
System.out.println("图片不能为空!!!");
}
}
toGongQiuShangJiaXinXiService.update(toGongQiuShangJiaXinXi);
return "update";
}