flex 手写在线签名实现代码

 更新时间:2009年08月03日 23:40:56   作者:  
企业信息系统中,有时候需要用到手写签名的功能。在这里用flex 实现一个。功能实现了,效果还在改善中。

java端主要有两个方法。一个是接收客户端保存图片时的流,另一个是根据客户端的ID返回流。当前例子是运行在google app engine上的。以下是核心代码:
servlet:
Java代码
复制代码 代码如下:

/**
*
*/
package com.humanmonth.rea.pic;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.datastore.Blob;
import com.humanmonth.framework.web.servlet.Action;
import com.humanmonth.framework.web.servlet.AutoMapperServlet;
import com.humanmonth.framework.web.servlet.ServletUtil;
/**
* @author presses
*
*/
@SuppressWarnings ( "serial" )
public class PictureServlet extends AutoMapperServlet {
private Logger log=Logger.getLogger( this .getClass().getName());
/**
* 测试
*/
@Action ( "sayHi" )
public void sayHi(HttpServletRequest req, HttpServletResponse res) throws IOException {
ServletUtil.outTips(res, "hi" , null );
}
/**
* 保存图片
*/
@Action ( "savePic" )
public void savePic(HttpServletRequest req, HttpServletResponse res) throws IOException {
String name= ServletUtil
.getStringParameterWithTrim(req, "name" );
if (name== null ){
name="签名.jpg" ;
}
Picture pic = new Picture(ServletUtil.getStringParameterWithTrim(req, "module" ),name, new Blob(IOUtils.toByteArray(req.getInputStream())));
new PictureService().savePic(pic);
log.info("保存的文件大小:" +pic.getContent().getBytes().length);
ServletUtil.outView(res, pic.getId());
}
/**
* 查找所有图片
*/
@Action ( "queryAllPic" )
public void queryAllPic(HttpServletRequest req, HttpServletResponse res) throws IOException {
String result = "" ;
for (Picture pic : new PictureService().queryAllPicture()) {
if (pic.getContent() == null ) {
continue ;
}
result += new String(pic.getContent().getBytes(), "utf-8" ) + ":" ;
}
ServletUtil.outView(res, result);
}
/**
* 以ID获取图片
*/
@Action ( "queryPicById" )
public void queryPicById(HttpServletRequest req, HttpServletResponse res) throws IOException {
String id = ServletUtil.getStringParameterWithTrim(req, "pid" );
log.info("开始下载文件,ID为:" +id);
Picture pic = new PictureService().queryPicById(Long.valueOf(id));
log.info("下载的文件大小:" +pic.getContent().getBytes().length);
ServletUtil.downloadToClient(res, pic.getContent().getBytes(), pic.getName());
}
}
/**
*
*/
package com.humanmonth.rea.pic;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.datastore.Blob;
import com.humanmonth.framework.web.servlet.Action;
import com.humanmonth.framework.web.servlet.AutoMapperServlet;
import com.humanmonth.framework.web.servlet.ServletUtil;
/**
* @author presses
*
*/
@SuppressWarnings("serial")
public class PictureServlet extends AutoMapperServlet {
private Logger log=Logger.getLogger(this.getClass().getName());
    /**
     * 测试
     */
    @Action("sayHi")
    public void sayHi(HttpServletRequest req, HttpServletResponse res) throws IOException {
        ServletUtil.outTips(res, "hi", null);
    }
    /**
     * 保存图片
     */
    @Action("savePic")
    public void savePic(HttpServletRequest req, HttpServletResponse res) throws IOException {
String name= ServletUtil
.getStringParameterWithTrim(req, "name");
if(name==null){
    name="签名.jpg";
}
        Picture pic = new Picture(ServletUtil.getStringParameterWithTrim(req, "module"),name, new Blob(IOUtils.toByteArray(req.getInputStream())));
        new PictureService().savePic(pic);
        log.info("保存的文件大小:"+pic.getContent().getBytes().length);
        ServletUtil.outView(res, pic.getId());
    }
    /**
     * 查找所有图片
     */
    @Action("queryAllPic")
    public void queryAllPic(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String result = "";
        for (Picture pic : new PictureService().queryAllPicture()) {
            if (pic.getContent() == null) {
                continue;
            }
            result += new String(pic.getContent().getBytes(), "utf-8") + ":";
        }
        ServletUtil.outView(res, result);
    }
    /**
     * 以ID获取图片
     */
    @Action("queryPicById")
    public void queryPicById(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String id = ServletUtil.getStringParameterWithTrim(req, "pid");
        log.info("开始下载文件,ID为:"+id);
        Picture pic = new PictureService().queryPicById(Long.valueOf(id));
        log.info("下载的文件大小:"+pic.getContent().getBytes().length);
        ServletUtil.downloadToClient(res, pic.getContent().getBytes(), pic.getName());
    }
}

业务类:
Java代码
复制代码 代码如下:

/**
*
*/
package com.humanmonth.rea.pic;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import com.humanmonth.framework.dao.JDOTemplate;
/**
* @author presses
*
*/
public class PictureService {
@SuppressWarnings ( "unused" )
private final Logger log=Logger.getLogger( this .getClass().getName());
/**
* 保存图片
*/
public void savePic( final Picture pic) {
new JDOTemplate<Picture>() {
@Override
public void deal(PersistenceManager pm, List<Picture> result) {
pm.makePersistent(pic);
pm.flush();
}
}.execute();
}
/**
* 以ID和条件获取图片
*/
public Picture queryPicById( final Long id) {
return new JDOTemplate<Picture>() {
@Override
public void deal(PersistenceManager pm, List<Picture> result) {
result.add((Picture) pm.getObjectById(Picture.class ,id));
}
}.execute().get(0 );
}
/**
* 查找所有的图片
*/
public List<Picture> queryAllPicture() {
return new JDOTemplate<Picture>() {
@SuppressWarnings ( "unchecked" )
@Override
public void deal(PersistenceManager pm, List<Picture> result) {
Object obj = pm.newQuery("select from " + Picture. class .getName()).execute();
result.addAll((List<Picture>) obj);
}
}.execute();
}
}
/**
*
*/
package com.humanmonth.rea.pic;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import com.humanmonth.framework.dao.JDOTemplate;
/**
* @author presses
*
*/
public class PictureService {
    @SuppressWarnings("unused")
    private final Logger log=Logger.getLogger(this.getClass().getName());
    /**
     * 保存图片
     */
    public void savePic(final Picture pic) {
        new JDOTemplate<Picture>() {
            @Override
            public void deal(PersistenceManager pm, List<Picture> result) {
                pm.makePersistent(pic);
                pm.flush();
            }
        }.execute();
    }
    /**
     * 以ID和条件获取图片
     */
    public Picture queryPicById(final Long id) {
        return new JDOTemplate<Picture>() {
            @Override
            public void deal(PersistenceManager pm, List<Picture> result) {
                result.add((Picture) pm.getObjectById(Picture.class,id));
            }
        }.execute().get(0);
    }
    /**
     * 查找所有的图片
     */
    public List<Picture> queryAllPicture() {
        return new JDOTemplate<Picture>() {
            @SuppressWarnings("unchecked")
            @Override
            public void deal(PersistenceManager pm, List<Picture> result) {
                Object obj = pm.newQuery("select from " + Picture.class.getName()).execute();
                result.addAll((List<Picture>) obj);
            }
        }.execute();
    }
}

域对像:
Java代码
复制代码 代码如下:

package com.humanmonth.rea.pic;
import java.sql.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Blob;
/**
* 图片
*
* @author presses
*
*/
@PersistenceCapable (identityType = IdentityType.APPLICATION)
public class Picture {
/**
* 主键
*/
@PrimaryKey
@Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
/**
* 模块
*/
@Persistent
private String module;
/**
* 文件名
*/
@Persistent
private String name;
/**
* 内容
*/
@Persistent
private Blob content;
/**
* 保存时间
*/
@Persistent
private Date date;
public Picture(String module, String name,Blob content) {
this .module = module;
this .name=name;
this .content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this .id = id;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this .module = module;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this .date = date;
}
public Blob getContent() {
return content;
}
public void setContent(Blob content) {
this .content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
}

相关文章

最新评论