自动采集程序

2018-09-06 11:38

阅读:376

  最近在做一个音乐站,音乐文件嘛。。。一般是从网上收集。。so。。写了一段采集程序。
复制代码 代码如下:
<%
OnErrorResumeNext
ConstuploadPath=/uploads/文件存放路径
ConstallowFileExt=jpg,wma,swf,gif允许被采集的文件类型
ConstallowFileSize=200
FunctiongetFile(url)
Ifurl=Then
ExitFunction
Else
url=Trim(url)
EndIf

获取文件
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

ifInstr(,&Lcase(allowFileExt)&,,,&fileExt&,)=0Then
getFile=文件类型不允许
ExitFunction
EndIf

Setxmlhttp=Server.CreateObject(Microsoft.XMLHTTP)
xmlhttp.openget,url,false
xmlhttp.send
Whilexmlhttp.readyState<>4
xmlhttp.waitForResponse1000
Wend
Ifxmlhttp.status<>200Then
getFile=获取文件出错
ExitFunction
Else
Setfolder=Server.CreateObject(Scripting.FileSystemObject)
DimfolderName
folderName=getNow(Year)&getNow(Month)文件夹
Iffolder.FolderExists(Server.MapPath(uploadPath))=FalseThen
folder.CreateFolderServer.MapPath(uploadPath)
EndIf
Iffolder.FolderExists(Server.MapPath(uploadPath&folderName))=FalseThen
folder.CreateFolderServer.MapPath(uploadPath&folderName)
EndIf
Setfolder=Nothing

file=xmlhttp.ResponseBody
Iflenb(file)>allowFileSizeThen
getFile=文件太大,不能保存!
ExitFunction
Else
SetobjAdostream=Server.Createobject(ADODB.Str&eam)
objAdostream.Open()
objAdostream.Type=1
objAdostream.Write(file)
objAdostream.SaveToFile(Server.Mappath(uploadPath&folderName&/&newFilePath))
objAdostream.SetEOS
SetobjAdostream=Nothing
getFile=<ahref=&uploadPath&folderName&/&newFilePath&target=_balnk>采集成功</a>
EndIf
EndIf
Setxmlhttp=Nothing
EndFunction
%>
<scriptrunat=serverlanguage=jscript>
functioncleanFileName(str){
str=str.replace(/[^_\.a-zA-Z\d]/ig,);
str=str.replace(/^[\/\.]+/,);
returnstr;
}
functiongetNow(n)
{
d=newDate();
switch(n)
{
caseYear:
returnd.getYear();
caseMonth:
return(d.getMonth()+1);
caseDate:
returnd.getDate();
}

}
</script>
<%ifrequest(do)=getfileandrequest(file)<>then
response.write(getFile(request(file)))
else%>
<formid=gformmethod=postaction=?do=getfilestyle=display:inline>
<inputname=filetype=inputstyle=font-size:12px;size=40value=
<inputtype=Submitname=Submitvalue=采集class=button>
</form>
<%endif%>


评论


亲,登录后才可以留言!