ASP下载远程图片保存到本地

<%
'asp下载远程图片保存到本地或者直接输出图片源代码
mode=2'模式,1为保存图片到本地 2为直接输出图片
url="https://ssl.captcha.qq.com/getimage?aid=2001601&0.5978519566745728" '远程图片地址

set xmlhttp=server.createobject("Microsoft.XMLHTTP")
xmlhttp.open "get",url,false
xmlhttp.send
img = xmlhttp.ResponseBody
contenttype = lcase(xmlhttp.getResponseHeader("content-type"))'获取响应头类型
set xmlhttp=nothing
if instr(contenttype,"image/")=1 then '为图片
  if mode=1 then
    savepath="upload/" '保存路径
    '给远程文件重命名
    randomize
    ranNum=int(999*rnd)
    filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum

    set objAdostream=server.createobject("ADODB.Stream")
    objAdostream.Open()
    objAdostream.type=1
    objAdostream.Write(img)
    objAdostream.SaveToFile(server.MapPath(savepath&filename&".jpg"))
    objAdostream.SetEOS
    set objAdostream=nothing
    response.Write "save complete!"
  else'输出图片
    response.ContentType=contenttype'设置响应头
    response.BinaryWrite img'输出图片2进制数据
  end if
else
  response.Write "响应头错误,不是图片,请检查!"&contenttype
end if
 %>

 


评论(0)网络
阅读(903)喜欢(0)Asp/VBScript