六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

项目组

[复制链接]

升级  16.67%

19

主题

19

主题

19

主题

秀才

Rank: 2

积分
75
 楼主| 发表于 2013-1-15 03:01:01 | 显示全部楼层 |阅读模式
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>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表