您的位置:首页 > 大数据 > 人工智能

[置顶] flex的AIR处女作-QQ聊天记录拷贝另存服务器

2013-05-23 21:46 393 查看
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="800" height="600" creationComplete="callLater(init)">
<fx:Script>
<![CDATA[
import flashx.textLayout.conversion.TextConverter;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.factory.StringTextLineFactory;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

var content:String="";
var newcontent:String="";
var imgcount:int=0;
var imgreplace:int=0;

private function init():void{

}

protected function richeditabletext1_pasteHandler(event:Event):void
{
var a:* = Clipboard.generalClipboard.getData(ClipboardFormats.HTML_FORMAT); //从系统剪切板中读取是html格式的数据
if(a!=null){
var stra:String= a as String;
content=content as String;

var myPattern:RegExp =/file:\/\/\//gi;
stra=stra.replace(myPattern, "");

content=content+stra;
a=content as Object;
abc.textFlow = TextConverter.importToFlow(a, TextConverter.TEXT_FIELD_HTML_FORMAT);
}

}

protected function button1_clickHandler(event:MouseEvent):void
{

if(abc.text==""){
Alert.show("输入不能为空");
return ;
}
var flexreg:RegExp=/<IMG/gi;

if(content.search(flexreg)<0){//说明提交的内容没有图片,那么就直接提交
var params:Object = {};
params["content"]=abc.text;
service.send(params);
return;
}

//把content中的img的标签替换掉,替换成{图片发送上去,服务器返回给客户端的一个地址}

while(content.search(flexreg)>=0){

imgcount++;//图片数量加一

var start:int=content.search(flexreg);
newcontent=newcontent+content.substring(0,start);//newcontent信息取到标签之前
content=content.substring(start,content.length);//content字符串去掉<IMG之前的字符

var intsrc:int=content.indexOf("src");
newcontent=newcontent+content.substring(0,intsrc);//把src属性之前的字符取到,可能有width之类的
content=content.substring(intsrc,content.length);

var intone:int=content.indexOf('"');
newcontent=newcontent+content.substring(0,intone+1);//把第一个引号字符取到,可能有width之类的
newcontent=newcontent+"{"+imgcount+"}";

content=content.substring(intone+1,content.length);

var inttwo:int=content.indexOf('"');

var strsrc:String=content.substring(0,inttwo);//图片的地址
content=content.substring(inttwo,content.length);//原先字符串去掉路径

//向newcontent中添加返回的绝对路径
var file:File = new File(strsrc);
if(file.exists){
file.upload(new URLRequest("http://localhost:8080/WallpaperShowShow/terminal/serve_uploadImg?imgcount="+imgcount),"file");
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadComplete);
file.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
}

}

//要把最后一个img标签后面的字符加入进去
newcontent=newcontent+content;

}

protected function uploadComplete(event:DataEvent):void{

var obj:Object=JSON.parse(event.data)
var state:String=obj.state as String;
if(state == "0"){
Alert.show("图片上传失败");
return ;
}
var strs:Array=state.split("&");

newcontent=newcontent.replace("{"+strs[1]+"}",strs[0]);
imgreplace++;

if(imgcount==imgreplace){
trace(newcontent);
var params:Object = {};
params["content"]=newcontent;

service.send(params);
}

}

protected function onIoError(event:IOErrorEvent):void{
Alert.show("图片上传失败");
}

//调用失败
protected function service_faultHandler(event:FaultEvent):void
{
Alert.show("失败了","提示");
}

protected function service_resultHandler(event:ResultEvent):void
{
Alert.show("成功了","提示");
abc.text="";
}

]]>
</fx:Script>
<fx:Declarations>

<s:HTTPService id="service"
url="http://localhost:8080/WallpaperShowShow/terminal/serve_addServeRecord"
useProxy="false" method="POST"
fault="service_faultHandler(event)"
result="service_resultHandler(event)" resultFormat="text">

</s:HTTPService>

</fx:Declarations>
<s:HGroup verticalAlign="baseline" top="30" left="80">
<s:Label fontSize="14" text="客服:"/>
<s:TextInput />
</s:HGroup>

<s:VGroup horizontalCenter="0" verticalCenter="0">

<s:HGroup verticalAlign="baseline">
<s:Label backgroundColor="#FFFFFF" fontFamily="Arial" fontSize="14" text="内容:"/>
<s:Group>

<s:Rect width="600" height="450">
<s:stroke>
<s:SolidColorStroke color="0x0"/>
</s:stroke>
</s:Rect>
<s:RichEditableText id="abc" x="0" y="0" width="600" height="450" text="RichEditableText" paste="richeditabletext1_pasteHandler(event)">
<s:span></s:span>
</s:RichEditableText>

</s:Group>
</s:HGroup>
</s:VGroup>
<s:Button x="473" y="31" label="提交" id="btn" click="button1_clickHandler(event)"/>

</s:WindowedApplication>


服务器端的代码

/**
* 图片上传到服务器
* @return
*/
public String uploadImg(){

try {

HttpServletRequest request = ServletActionContext.getRequest();
String uploadPath = request.getSession().getServletContext().getRealPath("/uploadimg");

FileInputStream fis=new FileInputStream(file);

//对name进行处理
String str= new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+(int)(Math.random()*1000);
fileFileName=str+fileFileName.substring(fileFileName.lastIndexOf("."), fileFileName.length());

File f=new File(uploadPath+"/" + fileFileName);
if(!f.exists()){
f.createNewFile();
}

FileOutputStream fos=new FileOutputStream(f);
byte[] b = new byte[1024];
int len = 0;
while(true){
len = fis.read(b);
if(len<0)break;
fos.write(b, 0, len);
}
fos.close();
fis.close();
state="http://localhost:8080/WallpaperShowShow/uploadimg/"+fileFileName+"&"+imgcount;

} catch (Exception e) {
e.printStackTrace();
logger.error("UploadImgAction.uploadImg!", e);
state = "0";
return ERROR;
}

return SUCCESS;
}

/**
* 把上传的字符串写入数据库
* @return
*/
public String addServeRecord(){
//写入数据库

return SUCCESS;
}


这个不仅把你的QQ聊天记录中的信息保存起来,还可以把word中 的信息保存起来,用这个程序的前提是你在信息中的图片,不是真正就一张图片,比如,你拷贝过来的

QQ消息中有截图,其实是保存在你电脑中的一个文件夹下,拷贝过来的是路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: