自动采集程序

时间:2021-05-18

复制代码 代码如下:
<%
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-Z\d]/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%>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章