pdb文件-优酷视频下载器官方下载
![useshellexecute](/uploads/image/0961.jpg)
2023年3月31日发(作者:cad字体库大全下载)
浅谈Web打印服务
服务器侦听http端⼝,接收客户端传来的的url下载,本地打印,要求客户端和服务器在同⼀个⽹内,打印机共享。
主要代码如下,需要引⽤,
classiLisen
{
///
///打印⽂件按默认⽅式
///
///
///
privateboolprintfile(stringfilePath,stringprinter)
{
try
{
if(printer=="")
{
PrintDocumentfPrintDocument=newPrintDocument();
printer=rName;
}
ProcessStartInfoinfo=newProcessStartInfo();
nts="""+printer+""";
llExecute=true;
NoWindow=true;
="PrintTo";
me=filePath;
NoWindow=true;
Style=;
Processp=newProcess();
nfo=info;
();
rInputIdle();
();
returntrue;
}
catch(Exceptione)
{
returnfalse;
}
}
///
///读取指定key的值
///
///
///
publicstringGetConfigValue(stringkey)
{
urationconfig=
eConfiguration();
if(gs[key]==null)
return"";
else
gs[key].Value;
}
///
///Http⽅式下载⽂件
///
///
///
///
publicboolDownloadFile(stringurl,stringlocalfile,stringcookie="")
{
boolflag=false;
longstartPosition=0;//上次下载的⽂件起始位置
FileStreamwriteStream;//写⼊本地⽂件流对象
if((localfile))
{
//⽂件存在直接删掉重建
(localfile);
}
writeStream=newFileStream(localfile,);//⽂件不保存创建⼀个⽂件
startPosition=0;
try
{
HttpWebRequestmyRequest=(HttpWebRequest)(url);//打开⽹络连接
("Cookie",cookie);
if(startPosition>0)
{
ge((int)startPosition);//设置Range值,与上⾯的⽤意相同,是为了定义远程⽂件读取位置
}
using(StreamreadStream=ponse().GetResponseStream())//向服务器请求,获得服务器的回应数据流
{
byte[]btArray=newbyte[512];//定义⼀个字节数据,⽤来向readStream读取内容和向writeStream写⼊内容
intcontentSize=(btArray,0,);//向远程⽂件读第⼀次
while(contentSize>0)//如果读取长度⼤于零则继续读
{
(btArray,0,contentSize);//写⼊本地⽂件
contentSize=(btArray,0,);//继续向远程⽂件读取
}
//();
}
FileInfofi=newFileInfo(localfile);
longilen=;
if(ilen<2000)
{
flag=false;//⽂件空⽩
}
else
{
flag=true;//返回true下载成功
}
}
catch(Exceptionex)
{
flag=false;//返回false下载失败
}
finally
{
();
}
returnflag;
}
intmaxThreadNum,portThreadNum;
//线程池
intminThreadNum;
Threads(outmaxThreadNum,outportThreadNum);
Threads(outminThreadNum,outportThreadNum);
ine("s:{0}",maxThreadNum);
ine("reads:{0}",minThreadNum);
//serWorkItem(newWaitCallback(TaskProc1),x);
stringprinterset=GetConfigValue("printer");
if(printerset=="")
{
//没有设置就使⽤默认打印机
PrintDocumentfPrintDocument=newPrintDocument();
printerset=rName;
}
ine("Printername="+printerset);
ine("nnWaitingforconnection...nn");
while(true)
{
//等待请求连接
//没有请求则GetContext处于阻塞状态
stringistatus="";
try
{
HttpListenerContextctx=text();
if(f("printurl=")>=0)
{
stringrescon=ng();
stringurltest=('=')[1];
stringfilename=('/')[('/').Length-1];
ine(ng("yyyy-MM-ddHH:mm:ss"));
ine(urltest);
booldownloadstatus=DownloadFile(urltest,"temp"+filename);
ine("Downloadfile"+filename+"..."+ng());
boolprintstatus=doprint("temp"+filename,printerset);
ine("Printfile"+filename+"..."+ng());
HttpListenerResponseresponse=se;
Code=200;
tType="application/json;charset=UTF-8";
tEncoding=8;
Header("Content-Type","application/json;charset=UTF-8");
using(StreamWriterwriter=newStreamWriter(Stream,8))
{
if(downloadstatus&&printstatus)
{
("ok");
}
else
{
("failed!"+"Download"+ng()+".Print"+ng());
}
();
();
}
}
else
{
HttpListenerResponseresponse=se;
Code=201;
tType="application/json;charset=UTF-8";
tEncoding=8;
Header("Content-Type","application/json;charset=UTF-8");
using(StreamWriterwriter=newStreamWriter(Stream,8))
{
("failed");
();
();
}
}
}
catch(Exceptionex)
{
ine(e);
}
}
();
}
}
catch(Exceptione)
{
ine(e);
HttpListenerlisterner=newHttpListener();
HttpListenerContextctx=text();
HttpListenerResponseresponse=se;
}
}
}
//调⽤⽅式
staticvoidMain(string[]args)
{
try
{
if(("temp"))
{
("temp",true);
}
Directory("temp");
}
catch(Exceptionex)
{
ine("CreateDirectorytempfailed!");
ine("");
ine(e);
return;
}
iLisennewlisten=newiLisen();
isen();
}
//客户端通过get⽅式访问,可以在浏览器测试打印效果,⽹址在上⽅设置
//打印动作通过⽂件默认打开⽅式打印,因此打印pdf⽂件需要安装个pdf打开⼯具,如foxitpdfreader
更多推荐
useshellexecute
发布评论