自动获得远程图片 #Writ
程序实现功能:自动将远程页面的文件中的图片下载到本地服务器
将下文保存为 save2local.
测试:save2local.asp?url=https://ent.sina.com.cn/s/m/2003-11-11/1411231388.html
<%
参数设置开始
url = request("url")
localaddr = server.mappath("images_remote/") 保存到本地的目录
localdir = "images_remote/" http 访问的相对路径
allowfileext = "jpg|bmp|png|gif" 支持的文件名格式
参数设置完毕
if createdir(localaddr) = false then
response.write "创建目录失败,请检查目录权限"
response.end
end if
response.write convert2localaddr(url,localaddr,localdir)
function convert2localaddr(url,localaddr,localdir)
参数说明
url 页面地址
localaddr 保存本地的物理地址
localdir 相对路径
strcontent = gethttppage(url)
set objregexp = new regexp
objregexp.ignorecase = true
objregexp.global = true
objregexp.pattern = "<img.+?>"
set matches =objregexp.execute(strcontent)
for each match in matches
retstr = retstr & getremoteimages(match.value)
next
imagesarray=split(retstr,"||")
remoteimage=""
localimage=""
for i=1 to ubound(imagesarray)
if imagesarray(i)<>"" and instr(remoteimage,imagesarray(i))<1 then
fname=baseurl&cstr(i&mid(imagesarray(i),instrrev(imagesarray(i),".")))
imagesfilename = imagesarray(i)
allowfileextarray = split(allowfileext,"|")
isgetfile = false
for tmp = 0 to ubound(allowfileextarray)
if lcase(getfileext(imagesfilename)) = allowfileextarray(tmp) then
isgetfile=true
end if
next
if isgetfile = true then
newfilename = generaterandomfilename(fname)
call save2local(imagesfilename,localaddr & "/" & newfilename)
remoteimage=remoteimage&"||"& imagesfilename
localimage=localimage&"||" & localdir & newfilename
end if
end if
next
arrnew=split(localimage,"||")
arrall=split(remoteimage,"||")
for i=1 to ubound(arrnew)
strcontent=replace(strcontent,arrall(i),arrnew(i))
next
convert2localaddr = strcontent
end function
function getremoteimages(str)
set objregexp1 = new regexp
objregexp1.ignorecase = true
objregexp1.global = true
objregexp1.pattern = "https://.+? "
set mm=objregexp1.execute(str)
for each match1 in mm
tmpaddr = left(match1.value,len(match1.value)-1)
getremoteimages=getremoteimages&"||" & replace(replace(tmpaddr,"""",""),"","")
next
end function
function gethttppage(url)
on error resume next
dim http
set http=server.createobject("msxml2.xmlhttp")
http.open "get",url,false
http.send()
if http.readystate<>4 then exit function
gethttppage=bytes2bstr(http.responsebody)
set http=nothing
if err.number<>0 then err.clear
end function
function bytes2bstr(vin)
dim strreturn
dim i,thischarcode,nextcharcode
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
function gethttpimg(url)
on error resume next
dim http
set http=server.createobject("msxml2.xmlhttp")
http.open "get",url,false
http.send()
if http.readystate<>4 then exit function
gethttpimg=http.responsebody
set http=nothing
if err.number<>0 then err.clear
end function
function save2local(from,tofile)
dim geturl,objstream,imgs
geturl=trim(from)
imgs=gethttpimg(geturl)
set objstream = server.createobject("adodb.stream")
objstream.type =1
objstream.open
objstream.write imgs
objstream.savetofile tofile,2
objstream.close()
set objstream=nothing
end function
function geturlencodel(byval url)中文文件名转换
dim i,code
geturlencodel=""
if trim(url)="" then exit function
for i=1 to len(url)
code=asc(mid(url,i,1))
if code<0 then code = code + 65536
if code>255 then
geturlencodel=geturlencodel&"%"&left(hex(code),2)&"%"&right(hex(code),2)
else
geturlencodel=geturlencodel&mid(url,i,1)
end if
next
end function
function generaterandomfilename(byval szfilename) 根据原文件名,自动以日期yyyy-mm-dd-random格式生成新文件名
randomize
rannum = int(90000 * rnd) + 10000
if month(now) < 10 then c_month = "0" & month(now) else c_month = month(now)
if day(now) < 10 then c_day = "0" & day(now) else c_day = day(now)
if hour(now) < 10 then c_hour = "0" & hour(now) else c_hour = hour(now)
if minute(now) < 10 then c_minute = "0" & minute(now) else c_minute = minute(now)
if second(now) < 10 then c_second = "0" & second(now) else c_second = minute(now)
fileext_a = split(szfilename, ".")
fileext = lcase(fileext_a(ubound(fileext_a)))
generaterandomfilename = year(now) & c_month & c_day & c_hour & c_minute & c_second & "_" & rannum & "." & fileext
end function
function createdir(byval localpath) 建立目录的程序,如果有多级目录,则一级一级的创建
on error resume next
localpath = replace(localpath, "", "/")
set fileobject = server.createobject("scripting.filesystemobject")
patharr = split(localpath, "/")
path_level = ubound(patharr)
for i = 0 to path_level
if i = 0 then pathtmp = patharr(0) & "/" else pathtmp = pathtmp & patharr(i) & "/"
cpath = left(pathtmp, len(pathtmp) - 1)
if not fileobject.folderexists(cpath) then fileobject.createfolder cpath
next
set fileobject = nothing
if err.number <> 0 then
createdir = false
err.clear
else
createdir = true
end if
end function
function getfileext(byval filename)
fileext_a=split(filename,".")
getfileext=lcase(fileext_a(ubound(fileext_a)))
end function
%>
上一篇: 移动h5相关