public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String imgDebug = null;
boolean canUpdate = true;
ImageForm imageForm = (ImageForm)form;
if(imageForm.getId() == 0){
ActionForward forward = new ActionForward();
forward.setPath("/cms/image.do");
forward.setRedirect(true);
return forward;
}
Image image = this.imageService.getById(imageForm.getId());
BeanUtils.copyProperties(image , imageForm);
FormFile ff = imageForm.getFf_image_path();
if(ff != null && ff.getFileSize() > 0){
String imageName = ff.getFileName();
String[] str = imageName.split("\\.");
int length = str.length;
if(length == 1){
imgDebug = "文件格式错误,只能为jpg,gif格式的图片文件";
canUpdate = false;
}else{
String suffix = str[length-1];
if(!suffix.equals("jpg") && !suffix.equals("gif") && !suffix.equals("GIF") && !suffix.equals("JPG") ){
imgDebug = "文件格式错误,只能为jpg,gif格式的图片文件";
canUpdate = false;
}else{
int fileSize = ff.getFileSize();
if(fileSize > 102400){
imgDebug = "图片文件太大,保存失败,图片不能超过100k";
canUpdate = false;
}
}
}
if(canUpdate == false){
this.saveDirectlyError(request, imgDebug);
ActionForward forward = new ActionForward();
forward.setPath("/cms/image.do?method=edit&id=" + imageForm.getId());
forward.setRedirect(true);
return forward;
}
String newImageName = imageForm.getId() + "." + str[length-1];
image.setImage_path(newImageName);
String appPath = request.getSession().getServletContext().getRealPath("/");
String imgPath = appPath + Constant.imageFileDir + newImageName;
System.out.println("=------------" + imgPath);
InputStream is=ff.getInputStream();//输入流
OutputStream os=new FileOutputStream(imgPath);//输出流
int bytesRead=0;
byte [] buffer = new byte[8192];
while((bytesRead=is.read(buffer,0,8192))!=-1){
os.write(buffer,0,bytesRead);//将文件写入服务器
}
if(os!=null){
try {
os.close();
}catch(Exception el){
el.printStackTrace();
}
}if(is!=null){
try {
is.close();
}catch(Exception el){
el.printStackTrace();
}
}
}
image.setUpdate_time(DateUtil.getCurDateTime());
this.imageService.saveOrUpdate(image);
ActionForward forward = new ActionForward();
forward.setPath("/cms/image.do?method=edit&id=" + imageForm.getId());
forward.setRedirect(true);
return forward;
图片显示
<tr>
<td class="tabletdtitlefirst" width="80">
图片</td>
<td class="tabletdcontext" width="380" colspan="3">
<html:file property="ff_image_path" size="50"/><br><br><br>
<c:if test="${imageForm.image_path != null}">
<img src="${ctx}/upload/image/<c:out
value='${imageForm.image_path}'/>" NAME="img">
</c:if>
</td>
</tr>