標簽歸檔:stream

用ASP/VBScript獲取文章列表

< %
'ASP獲取文章列表
function getHTTPPage(url)  
	dim Http  
	set Http=server.createobject("MSXML2.XMLHTTP")  
	Http.open "GET",url,false  
	Http.send()  
	if Http.readystate<>4 then  
		exit function  
	end if  
	getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")  
	set http=nothing  
	if err.number<>0 then err.Clear  
end function  
 
Function BytesToBstr(body,Cset)  
	dim objstream  
	set objstream = Server.CreateObject("adodb.stream")  
	objstream.Type = 1  
	objstream.Mode =3  
	objstream.Open  
	objstream.Write body  
	objstream.Position = 0  
	objstream.Type = 2  
	objstream.Charset = Cset  
	BytesToBstr = objstream.ReadText  
	objstream.Close  
	set objstream = nothing  
End Function  
 
Function ZZ(ustr,uexp)
	Set regEx=New RegExp
	regEx.Pattern=uexp
	regEx.IgnoreCase=True
	regEx.Global=True
	Set ZZ=regEx.Execute(ustr)
End Function
 
Sub Echo(t)
	response.write t&"<br />"&vbcrlf
End Sub
 
Sub getall(url,xurl,uri)
	Html = getHTTPPage(uri&Url)
	set Rs=ZZ(html,"("&xurl&"?.*)[#""].*>(.*)< \/a")
	for each uurl in Rs
		'echo uri&replace(uurl,"""","")
		Set oMacs=uurl
		echo uri&oMacs.SubMatches(0)&"|"&oMacs.SubMatches(1)
	next
End Sub
 
urls=split("ftgb|ftlg|zykt","|")
xurls=split("ftgbxx|ftlgxx|zyktcpxx","|")
for i=0 to ubound(urls)
	getall urls(i)&".jsp",xurls(i)&".jsp","http://gree.com.cn/gree_product/"
next
%>

下邊發個VBscript版本的

'VBScript獲取文章列表
function getHTTPPage(url)  
	dim Http  
	set Http=createobject("MSXML2.XMLHTTP")  
	Http.open "GET",url,false  
	Http.send()  
	if Http.readystate<>4 then  
		exit function  
	end if  
	getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")  
	set http=nothing  
	if err.number<>0 then err.Clear  
end function  
 
Function BytesToBstr(body,Cset)  
	dim objstream  
	set objstream = CreateObject("adodb.stream")  
	objstream.Type = 1  
	objstream.Mode =3  
	objstream.Open  
	objstream.Write body  
	objstream.Position = 0  
	objstream.Type = 2  
	objstream.Charset = Cset  
	BytesToBstr = objstream.ReadText  
	objstream.Close  
	set objstream = nothing  
End Function 
 
Function ZZ(ustr,uexp)
	Set regEx=New RegExp
	regEx.Pattern=uexp
	regEx.IgnoreCase=True
	regEx.Global=True
	Set ZZ=regEx.Execute(ustr)
End Function
 
Sub Echo(t)
	Wscript.echo t
End Sub
 
Sub getall(url,xurl,uri)
	Html = getHTTPPage(uri&Url)
	set Rs=ZZ(html,"("&xurl&"?.*)[#""].*>(.*)< \/a")
	for each uurl in Rs
		'echo uri&replace(uurl,"""","")
		Set oMacs=uurl
		echo uri&oMacs.SubMatches(0)&"|"&oMacs.SubMatches(1)
	next
End Sub
 
urls=split("ftgb|ftlg|zykt","|")
xurls=split("ftgbxx|ftlgxx|zyktcpxx","|")
for i=0 to ubound(urls)
	getall urls(i)&".jsp",xurls(i)&".jsp","http://gree.com.cn/gree_product/"
next

用到了正則表達式,令以前收藏的一個正則表達式教程分享給代價

ASP使用BinaryWrite與Adodb.Stream實現下載

因為某種原因要實現一個RAR的文件的下載,而且為了節省服務器資源,需要做一個判斷,下載引用方法如下:

   	<input type="button" value="下載工具包" onclick="location.href='http-localhost-api?/easywork-imageupload-client-full/download/port-80/ver=<%=date%/>.gzip.rar';">
   	<input type="button" value="下載升級包" onclick="location.href='http-localhost-api?/easywork-imageupload-client-fast/download/port-80/ver=<%=date%/>.gzip.rar';">

初步打算用FSO讀取為TextStream,然后用BinaryWrite輸出,代碼如下:

< %@codepage=65001%>
< %
response.buffer=false
if session("safe_mode")<>"ok" then response.write "<form action=""../../../admin.asp"" method=""post"" id=""a""><script type=""text/javascript"">document.getElementById(""a"").submit();</script>":response.end
if instr(request.Querystring,"/")< =0 then response.redirect "/":response.end
set fso=server.createobject("Scripting.FileSystemObject")
file=server.mappath(split(request.QueryString,"/")(1)&".rar")
if not fso.fileexists(file) then response.redirect "/":response.end
set fto=fso.getfile(file)
set fts=fto.OpenAsTextStream(1,-1)
 
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment;filename="&split(request.QueryString,"/")(1)&".rar"
 
response.binarywrite fts.readall
%>
</form>

實際運行時速度不是很快,可能是fts.readall的原因吧,于是修改一下代碼,一行一行的輸出:

< %@codepage=936%>
< %
response.buffer=false
if session("safe_mode")<>"ok" then response.write "<form action=""../../../admin.asp"" method=""post"" id=""a""><script type=""text/javascript"">document.getElementById(""a"").submit();</script>":response.end
if instr(request.Querystring,"/")< =0 then response.redirect "/":response.end
set fso=server.createobject("Scripting.FileSystemObject")
file=server.mappath(split(request.QueryString,"/")(1)&".rar")
if not fso.fileexists(file) then response.redirect "/":response.end
set fto=fso.getfile(file)
set fts=fto.OpenAsTextStream(1,-1)
 
 
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment;filename="&split(request.QueryString,"/")(1)&".rar"
do while not fts.atendofstream
	response.binarywrite fts.readall
loop
%>
</form>

效率有所提高,因為FSO每次讀取硬盤,速度很慢~到網上搜索了一下,發現Adodb中除了Connection和Recordset,還有一個Stream,于是有了第三種方法:

< %@codepage=65001%>
< %
response.buffer=false
if session("safe_mode")<>"ok" then response.write "<form action=""../../../admin.asp"" method=""post"" id=""a""><script type=""text/javascript"">document.getElementById(""a"").submit();</script>":response.end
if instr(request.Querystring,"/")< =0 then response.redirect "/":response.end
set fso=server.createobject("Scripting.FileSystemObject")
file=server.mappath(split(request.QueryString,"/")(1)&".rar")
if not fso.fileexists(file) then response.redirect "/":response.end
 
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment;filename="&now&".rar"
 
Set Ados=Server.CreateObject("Adodb.Stream")
Ados.Mode=3
Ados.Type=1
Ados.Open
Ados.LoadFromFile(Server.mappath(split(request.QueryString,"/")(1)&".rar"))
 
response.binarywrite Ados.read()
%>
</form>

速度已經不錯了,同時很多人訪問CPU占用也不是很高。