自动采集程序
程序员文章站
2022-07-02 08:42:23
最近在做一个音乐站,音乐文件嘛。。。一般是从网上收集。。so。。写了一段采集程序。 复制代码 代码如下:<% on er...
最近在做一个音乐站,音乐文件嘛。。。一般是从网上收集。。so。。写了一段采集程序。
<%
on error resume next
const uploadpath = "/uploads/" '文件存放路径
const allowfileext = "jpg,wma,swf,gif" '允许被采集的文件类型
'const allowfilesize = "200"
function getfile(url)
if url = "" then
exit function
else
url = trim(url)
end if
'获取文件
fileext = lcase(mid(url,instrrev(url, ".")+1)) '文件类型
filename = lcase(mid(url,instrrev(url, "/")+1,instrrev(url, ".")-instrrev(url, "/")-1)) '无文件类型的文件名
newfilepath = getnow("date") & "_" & replace(formatdatetime(now(),3),":","") & "_" & cleanfilename(filename) & "." & fileext
if instr(","&lcase(allowfileext)&",",","&fileext&",") = 0 then
getfile = "文件类型不允许"
exit function
end if
set xmlhttp = server.createobject("microsoft.xmlhttp")
xmlhttp.open "get",url,false
xmlhttp.send
'while xmlhttp.readystate <> 4
' xmlhttp.waitforresponse 1000
'wend
if xmlhttp.status <> 200 then
getfile="获取文件出错"
exit function
else
set folder=server.createobject("scripting.filesystemobject")
dim foldername
foldername = getnow("year")&getnow("month") '文件夹
if folder.folderexists(server.mappath(uploadpath))=false then
folder.createfolder server.mappath(uploadpath)
end if
if folder.folderexists(server.mappath(uploadpath & foldername))=false then
folder.createfolder server.mappath(uploadpath & foldername)
end if
set folder=nothing
file=xmlhttp.responsebody
if lenb(file) > allowfilesize then
getfile = "文件太大,不能保存!"
exit function
else
set objadostream=server.createobject("adodb.str"&"eam")
objadostream.open()
objadostream.type=1
objadostream.write(file)
objadostream.savetofile(server.mappath(uploadpath & foldername &"/" & newfilepath))
objadostream.seteos
set objadostream=nothing
getfile= "<a href="""&uploadpath & foldername &"/" & newfilepath&""" target=""_balnk"">采集成功</a>"
end if
end if
set xmlhttp=nothing
end function
%>
<script runat="server" language="jscript">
function cleanfilename(str){
str = str.replace(/[^_\.a-za-z\d]/ig,"");
str = str.replace(/^[\/\.]+/,"");
return str;
}
function getnow(n)
{
d = new date();
switch (n)
{
case "year":
return d.getyear();
case "month":
return (d.getmonth() + 1);
case "date":
return d.getdate();
}
}
</script>
<%if request("do")="getfile" and request("file") <> "" then
response.write(getfile(request("file")))
else%>
<form id="gform" method="post" action="?do=getfile" style="display: inline">
<input name="file" type="input" style="font-size:12px;" size="40" value="http://blog.xiaobin.net/uploads/200512/08_093616_01.gif">
<input type="submit" name="submit" value="采集 " class="button">
</form>
<%end if%>
复制代码 代码如下:
<%
on error resume next
const uploadpath = "/uploads/" '文件存放路径
const allowfileext = "jpg,wma,swf,gif" '允许被采集的文件类型
'const allowfilesize = "200"
function getfile(url)
if url = "" then
exit function
else
url = trim(url)
end if
'获取文件
fileext = lcase(mid(url,instrrev(url, ".")+1)) '文件类型
filename = lcase(mid(url,instrrev(url, "/")+1,instrrev(url, ".")-instrrev(url, "/")-1)) '无文件类型的文件名
newfilepath = getnow("date") & "_" & replace(formatdatetime(now(),3),":","") & "_" & cleanfilename(filename) & "." & fileext
if instr(","&lcase(allowfileext)&",",","&fileext&",") = 0 then
getfile = "文件类型不允许"
exit function
end if
set xmlhttp = server.createobject("microsoft.xmlhttp")
xmlhttp.open "get",url,false
xmlhttp.send
'while xmlhttp.readystate <> 4
' xmlhttp.waitforresponse 1000
'wend
if xmlhttp.status <> 200 then
getfile="获取文件出错"
exit function
else
set folder=server.createobject("scripting.filesystemobject")
dim foldername
foldername = getnow("year")&getnow("month") '文件夹
if folder.folderexists(server.mappath(uploadpath))=false then
folder.createfolder server.mappath(uploadpath)
end if
if folder.folderexists(server.mappath(uploadpath & foldername))=false then
folder.createfolder server.mappath(uploadpath & foldername)
end if
set folder=nothing
file=xmlhttp.responsebody
if lenb(file) > allowfilesize then
getfile = "文件太大,不能保存!"
exit function
else
set objadostream=server.createobject("adodb.str"&"eam")
objadostream.open()
objadostream.type=1
objadostream.write(file)
objadostream.savetofile(server.mappath(uploadpath & foldername &"/" & newfilepath))
objadostream.seteos
set objadostream=nothing
getfile= "<a href="""&uploadpath & foldername &"/" & newfilepath&""" target=""_balnk"">采集成功</a>"
end if
end if
set xmlhttp=nothing
end function
%>
<script runat="server" language="jscript">
function cleanfilename(str){
str = str.replace(/[^_\.a-za-z\d]/ig,"");
str = str.replace(/^[\/\.]+/,"");
return str;
}
function getnow(n)
{
d = new date();
switch (n)
{
case "year":
return d.getyear();
case "month":
return (d.getmonth() + 1);
case "date":
return d.getdate();
}
}
</script>
<%if request("do")="getfile" and request("file") <> "" then
response.write(getfile(request("file")))
else%>
<form id="gform" method="post" action="?do=getfile" style="display: inline">
<input name="file" type="input" style="font-size:12px;" size="40" value="http://blog.xiaobin.net/uploads/200512/08_093616_01.gif">
<input type="submit" name="submit" value="采集 " class="button">
</form>
<%end if%>