最近在做一个音乐站,音乐文件嘛。。。一般是从网上收集。。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.open"get",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="server"language="jscript">
functioncleanFileName(str){
str=str.replace(/[^_.a-zA-Zd]/ig,"");
str=str.replace(/^[/.]+/,"");
returnstr;
}
functiongetNow(n)
{
d=newDate();
switch(n)
{
case"Year":
returnd.getYear();
case"Month":
return(d.getMonth()+1);
case"Date":
returnd.getDate();
}
}
</script>
<%ifrequest("do")="getfile"andrequest("file")<>""then
response.write(getFile(request("file")))
else%>
<formid="gform"method="post"action="?do=getfile"style="display:inline">
<inputname="file"type="input"style="font-size:12px;"size="40"value="http://blog.xiaobin.net/uploads/200512/08_093616_01.gif">
<inputtype="Submit"name="Submit"value="采集"class="button">
</form>
<%endif%>
【自动采集程序】相关文章: