flex 手寫在線簽名實現(xiàn)代碼第2/2頁
更新時間:2009年08月03日 23:40:56 作者:
企業(yè)信息系統(tǒng)中,有時候需要用到手寫簽名的功能。在這里用flex 實現(xiàn)一個。功能實現(xiàn)了,效果還在改善中。
java端主要有兩個方法。一個是接收客戶端保存圖片時的流,另一個是根據(jù)客戶端的ID返回流。當前例子是運行在google app engine上的。以下是核心代碼:
servlet:
Java代碼
復(fù)制代碼 代碼如下:
/**
*
*/
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());
}
}
業(yè)務(wù)類:
Java代碼
復(fù)制代碼 代碼如下:
/**
*
*/
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();
}
}
域?qū)ο瘢?
Java代碼
復(fù)制代碼 代碼如下:
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;
/**
* 內(nèi)容
*/
@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;
}
}
相關(guān)文章
Flex與.NET互操作 使用FileReference+HttpHandler實現(xiàn)文件上傳/下載
Flex與.NET互操作 使用FileReference+HttpHandler實現(xiàn)文件上傳/下載2009-06-06Flex 編程注意之性能優(yōu)化、垃圾回收的一些總結(jié)
自從開始做Flex、ActionScript 3.0的項目,我就一直與垃圾回收、性能優(yōu)化這些問題打交道,因此也總結(jié)了一些優(yōu)化的方案,同時在一些QQ群中也得到了一些“高人”的指點,因此將此內(nèi)容記錄一下。2009-07-07Flex與.NET互操作(十二):FluorineFx.Net的及時通信應(yīng)用(Remote Shared Objects
遠程共享對象(Remote Shared Objects) 可以用來跟蹤、存儲、共享以及做多客戶端的數(shù)據(jù)同步操作。只要共享對象上的數(shù)據(jù)發(fā)生了改變,將會把最新數(shù)據(jù)同步到所有連接到該共享對象的應(yīng)用程序客戶端。2009-06-06Flex 如何得到itemRenderer里面的內(nèi)容
itemRenderer里面的內(nèi)容 獲取技巧。2009-07-07Flex與.NET互操作(十三):FluorineFx.Net實現(xiàn)視頻錄制與視頻回放
本文主要介紹使用FluorineFx.Net來實現(xiàn)視頻錄制與視頻回放,F(xiàn)luorineFx如同F(xiàn)MS一樣,除了有AMF通信,RTMP協(xié)議,RPC 和遠程共享對象外,它同樣具備視頻流服務(wù)的功能。2009-06-06Flex與.NET互操作(八) 使用FluorineFx網(wǎng)關(guān)實現(xiàn)遠程訪問
關(guān)于遠程訪問在本系列文章中陸續(xù)的寫了不少示例了,本文沒有準備深入的去探討,為了鞏固FluorineFx網(wǎng)關(guān)的學(xué)習(xí)和使用。2009-06-06