商城网站中的图片是使用助理工具批量上传的,
现在遇到一个问题,商品描述中的图片都是外部链接 例如
src="http://tbphoto3.bababian.com/upload6/%E9%99%88%E7%BE%8E%E5%AE%89/201203/00190158590_m.jpg"
现在有没有一个办法将数据库里 产品描述中对应的图片全部下载下来,并且保存到服务器中的某个文件夹下
然后再修改数据库中的商品描述
------解决方案--------------------------------------------------------
外部连接不行吗,很多大型网站都是有专门的图片服务器
------解决方案--------------------------------------------------------
可以通过正则表达式获取所有图片的URL,再进行下载
------解决方案--------------------------------------------------------
这个技术实现不难。但php不适合作这种需要批量处理的工作。
图片地址一个个分析出来,然后用webrequest来请求指向的服务器,获取图片的字节流,然后存到服务器指定目录。
------解决方案--------------------------------------------------------
既然偷人家的网站,就别懒得写程序。你首先手工保存图片、手工替换url,然后再考虑自动化编写程序替换,这时候就简单了。一开始不勤快,一开始就想偷懒,最后一事无成。
------解决方案--------------------------------------------------------
你遍历下数据库所有的数据,如果格式混在一起的话 ,就用正则给图片提出来,然后用webrequest访问,下载下来,格式化图片,保存到文件,然后将路径和之前在数据库找到的路径做替换
------解决方案--------------------------------------------------------
将库中的链接导入到记事本中,考到迅雷去,让迅雷童鞋帮你,阿迅很勤快的呀
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
PHP的没做过,VB.NET的正好做过,帖源码给你。
- VB.NET code
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click '建立文件夹 Dim s_rpath As String = System.Configuration.ConfigurationSettings.AppSettings("fileUrl") Dim Datedir As String = DateTime.Now.ToString("yyyyMMdd") & "/x_more" Dim updir As String = s_rpath & "\" & Datedir If Not Directory.Exists(updir) Then Directory.CreateDirectory(updir) End If '获取字符串 Dim sHtmlText As String = a_img_more.Value Dim regImg As New Regex("<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase) Dim matches As MatchCollection = regImg.Matches(sHtmlText) Dim fileName As String Dim pid As Integer = Request("id") Dim jsq As Integer = 0 '定义计数器 For Each match As Match In matches Dim imgurl As String = match.Groups("imgUrl").Value If (imgurl.IndexOf("img.xxx.com") < 0) Then '获取不为本站链接的图片 '检测图片尺寸(暂定高或宽大于300px的便下载) Dim image As System.Drawing.Image = LoadImage(imgurl) If image.Width >= 300 Or image.Height >= 300 Then fileName = pid & "_" & GenerateRandom(8) & ".jpg" Dim wc As Net.WebClient = New System.Net.WebClient() wc.DownloadFile(imgurl, updir & "/" & fileName) sHtmlText = sHtmlText.Replace(imgurl, "http://img.xxx.com/" & Datedir & "/" & fileName) jsq = jsq + 1 End If End If Next a_img_more.Value = sHtmlText ClientScript.RegisterStartupScript(Page.[GetType](), "", "<script>alert('共计下载" & jsq & "张图片,更改尚未保存到数据库中,如无误请点击[直接保存内容]!');</script>") End Sub '下面两个类的作用是获取网络图片的属性,目前使用的是高和宽 Public Function LoadImage(ByVal imageURI As String) As Image Dim image__1 As Image If imageURI.StartsWith("http://") Then image__1 = LoadImageFromWeb(imageURI) Else image__1 = Image.FromFile(imageURI) End If Return image__1 End Function Public Function LoadImageFromWeb(ByVal sURL As String) As Bitmap Dim i As Integer = sURL.LastIndexOf("/") + 1 Dim str As String = sURL.Substring(i, sURL.Length - i) Dim webRequest__1 As Net.WebRequest = Net.WebRequest.Create(sURL) webRequest__1.Credentials = Net.CredentialCache.DefaultCredentials Dim stream As Stream = webRequest__1.GetResponse().GetResponseStream() Dim memoryStream As New MemoryStream() Dim bs As Byte() = New Byte(255) {} Dim j As Integer = stream.Read(bs, 0, CInt(bs.Length)) While j > 0 memoryStream.Write(bs, 0, j) j = stream.Read(bs, 0, CInt(bs.Length)) End While stream.Close() memoryStream.Position = CLng(0) Return New Bitmap(memoryStream) End Function '产生文件名 Private Shared constant As Char() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} Public Shared Function GenerateRandom(ByVal Length As Integer) As String Dim newRandom As System.Text.StringBuilder = New System.Text.StringBuilder(62) Dim rd As Random = New Random Dim i As Integer = 0 While i < Length newRandom.Append(constant(rd.Next(36))) System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1) End While Return newRandom.ToString End Function