用ASP VBS xmlhttp adodbstream下载和保存图片的代码
程序员文章站
2023-11-12 14:58:52
函数: 复制代码 代码如下:function savefile(data,recfilen) set&...
函数:
function savefile(data,recfilen)
set astream=createobject("adodb.stream")'asp server.createobject("adodb.stream")
fxt=mid(recfilen,instrrev(recfilen,".")+1)
txt=false
if fxt="asp" or fxt="xml" or fxt="aspx" or fxt="php" or fxt="txt" or fxt="jsp" then
txt=true
end if
if txt then
astream.type=2 '1 bin,2 txt
else
astream.type=1 '1 bin,2 txt
end if
astream.mode = 3' admoderead =1
' admodereadwrite =3
' admoderecursive =4194304
' admodesharedenynone =16
' admodesharedenyread =4
' admodesharedenywrite =8
' admodeshareexclusive =12
' admodeunknown =0
' admodewrite =2
astream.open
'astream.charset = "gb2312"
'astream.loadfromfile(recfilen) '装载文件
'assp=astream.size
astream.position =0 '装载文件时设置为assp
'astream.writetext tmpstr00,1
if txt then
data=bytes2bstr(data)
astream.writetext data,1
else
astream.write data
end if
astream.savetofile recfilen,2
astream.close
end function
'server.
function downimg(url)
set oxmlhttp =createobject("microsoft.xmlhttp")'asp server.createobject("microsoft.xmlhttp")
data_got=""
oxmlhttp.open "get",url, false
oxmlhttp.setrequestheader "accept-encoding"," gzip, deflate"
oxmlhttp.setrequestheader "user-agent","mozilla/4.0 (compatible; msie 6.0; windows nt 5.2; sv1; .net clr 2.0.50727)"
oxmlhttp.send
rtstatus=oxmlhttp.status
data_got=oxmlhttp.responsebody
filename=mid(url,instrrev(url,"/")+1)
if rtstatus=200 then
data_got=oxmlhttp.responsebody
savefile data_got,filename
else
data_got=""
end if
set oxmlhttp =nothing
end function
function bytes2bstr(vin) '二进制转化为汉字
strreturn = ""
for i = 1 to lenb(vin)
thischarcode = ascb(midb(vin,i,1))
if thischarcode < &h80 then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin,i+1,1))
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
i = i + 1
end if
next
bytes2bstr = strreturn
end function
使用方法:
imgurl="http://www.163car.com/upfile/carimages/0092/s_b_20051241127326f6uew1s.jpg" '图片
downimg(imgurl)
imgurl="http://login.zydn.net/news.asp" '文字页面
downimg(imgurl)
把代码保存为vbs文件,不需要iis就可以运行~
复制代码 代码如下:
function savefile(data,recfilen)
set astream=createobject("adodb.stream")'asp server.createobject("adodb.stream")
fxt=mid(recfilen,instrrev(recfilen,".")+1)
txt=false
if fxt="asp" or fxt="xml" or fxt="aspx" or fxt="php" or fxt="txt" or fxt="jsp" then
txt=true
end if
if txt then
astream.type=2 '1 bin,2 txt
else
astream.type=1 '1 bin,2 txt
end if
astream.mode = 3' admoderead =1
' admodereadwrite =3
' admoderecursive =4194304
' admodesharedenynone =16
' admodesharedenyread =4
' admodesharedenywrite =8
' admodeshareexclusive =12
' admodeunknown =0
' admodewrite =2
astream.open
'astream.charset = "gb2312"
'astream.loadfromfile(recfilen) '装载文件
'assp=astream.size
astream.position =0 '装载文件时设置为assp
'astream.writetext tmpstr00,1
if txt then
data=bytes2bstr(data)
astream.writetext data,1
else
astream.write data
end if
astream.savetofile recfilen,2
astream.close
end function
'server.
function downimg(url)
set oxmlhttp =createobject("microsoft.xmlhttp")'asp server.createobject("microsoft.xmlhttp")
data_got=""
oxmlhttp.open "get",url, false
oxmlhttp.setrequestheader "accept-encoding"," gzip, deflate"
oxmlhttp.setrequestheader "user-agent","mozilla/4.0 (compatible; msie 6.0; windows nt 5.2; sv1; .net clr 2.0.50727)"
oxmlhttp.send
rtstatus=oxmlhttp.status
data_got=oxmlhttp.responsebody
filename=mid(url,instrrev(url,"/")+1)
if rtstatus=200 then
data_got=oxmlhttp.responsebody
savefile data_got,filename
else
data_got=""
end if
set oxmlhttp =nothing
end function
function bytes2bstr(vin) '二进制转化为汉字
strreturn = ""
for i = 1 to lenb(vin)
thischarcode = ascb(midb(vin,i,1))
if thischarcode < &h80 then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin,i+1,1))
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
i = i + 1
end if
next
bytes2bstr = strreturn
end function
使用方法:
复制代码 代码如下:
imgurl="http://www.163car.com/upfile/carimages/0092/s_b_20051241127326f6uew1s.jpg" '图片
downimg(imgurl)
imgurl="http://login.zydn.net/news.asp" '文字页面
downimg(imgurl)
上一篇: 如何优化SQL语句的心得浅谈