下载来自http://www.cs.columbia.edu/CAVE/databases/pubfig/的样本。
C++程序:
//#define _AFXDLL//动态连接MFC,若不加此句,右键工程的属性->配置属性->常规->MFC的使用->在共享DLL中使用MFC得到同样效果
#include <afxinet.h>//MFC下封装socket的一个头文件,主管各种网络连接
#include <string>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
long DownLoadImg(CString szpUrl,char* szpBuff,char* imgName);
//BOOL DownLoadFile(LPCTSTR szRemoteURI, LPCTSTR szLocalPath);
//#define __BUFFER_SIZE 1024
int main(int argc,char** argv)
{
ifstream in("dev_urls.txt");
string temp;
int line=0;
while(getline(in,temp))
{
line++;
if(line==1||line==2)
continue;
istringstream ss(temp);//istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来
string s;
int j=0;
while(ss>>s)
{
j++;
if(j==4)
{
char* szpBuff;
szpBuff = new char[10<<20];
if(szpBuff == NULL)
{
cout<<"内存分配失败!"<<endl;
delete szpBuff;
break;
}
char imgName[10];
itoa(line-2,imgName,10);
*const int len=s.length();
char *szpUrl;
szpUrl=new char[len+1];
strcpy(szpUrl,s.c_str());*/
CString szpUrl;
szpUrl=s.c_str();
//DownLoadFile(szpUrl, "");
if(DownLoadImg(szpUrl,szpBuff,imgName))
{
cout<<"下载,失败!"<<endl;
delete szpBuff;
//delete szpUrl;
break;
}
delete szpBuff;
delete szpUrl;
}
}
}
in.close();
//DownLoadImg("http://lh5.ggpht.com/_wKqf41I-J5A/SF2AaMunTnI/AAAAAAAABo0/diBN91bfheo/Aishworiya+(96).jpg",szpBuff,"test.jpg");
//delete szpBuff;
return 0;
}
long DownLoadImg(CString szpUrl,char* szpBuff,char* imgName)
//szpUrl表示要访问的网址
//szpBuff
{
//CInternetSession session;如果是MFC应用程序,可以这样定义变量
CInternetSession session("My Session");//如果是控制台应用程序,则必须加上后面的"My Session"
CHttpFile *httpFile = NULL;
/*try
{*/
httpFile=(CHttpFile *)session.OpenURL(szpUrl);
/*}
catch(CInternetException * m_pException)
{
httpFile = NULL;
m_pException->m_dwError;
m_pException->Delete();
session.Close();
}*/
if(httpFile==NULL)
{
httpFile->Close();
session.Close();
return -1;
}
CStdioFile imgFile;
imgFile.Open(imgName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
DWORD dwStatusCode;
httpFile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
int size=0;
do
{
size = httpFile->Read(szpBuff,1024); // 读取图片
imgFile.Write(szpBuff,size);
}while(size > 0);
}
httpFile->Close();
session.Close();
return 0;
}
//BOOL DownLoadFile(LPCTSTR szRemoteURI, LPCTSTR szLocalPath)
//{
// //ENSURE_ARG_EX(NULL != szRemoteURI && NULL != szLocalPath);
// CInternetSession session("My Session") ;
// CHttpConnection* pHttpConnection = NULL;
// CHttpFile* pHttpFile = NULL;
// CString strServer = _T(""), strObject = _T("");
// INTERNET_PORT wPort = 0;
// DWORD dwType = 0;
// HANDLE hFile = NULL;
// TCHAR pszBuffer[__BUFFER_SIZE];
// DWORD dwFileSize = 0;
// const int nTimeOut = 2000;
// session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
// session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
// BOOL bResult = FALSE;
// /*try
// {*/
// AfxParseURL(szRemoteURI, dwType, strServer, strObject, wPort);
// pHttpConnection = session.GetHttpConnection(strServer, wPort, NULL, NULL);
// //VERIFY_EX(NULL != pHttpConnection);
// pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
// //VERIFY_EX(NULL != pHttpFile);
// if(!pHttpFile->SendRequest())
// goto _err_handler;
// DWORD dwStateCode;
// pHttpFile->QueryInfoStatusCode(dwStateCode);
// if(dwStateCode != HTTP_STATUS_OK)
// goto _err_handler;
// hFile = CreateFile(szLocalPath, GENERIC_WRITE,
// FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// if(hFile == INVALID_HANDLE_VALUE)
// goto _err_handler;
// BOOL bRet = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dwFileSize);
// if (!bRet)
// goto _err_handler;
//
// DWORD dwWrite = 0, dwTotalWrite = 0;
// UINT nRead = 0;
// do
// {
// nRead = pHttpFile->Read(pszBuffer, __BUFFER_SIZE);
// if (nRead <= 0) break;
// WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL);
// //VERIFY_EX(nRead == dwWrite);
// dwTotalWrite += dwWrite;
// } while (TRUE);
// if (dwTotalWrite != dwFileSize)
// goto _err_handler;
// bResult = TRUE;
// /*}
// catch (CFileException* e)
// {
// e->Delete();
// REPORT_CACHED_EXCEPTION(_T("CFileException"));
// }
// catch (CInternetException* e)
// {
// CString sError;
// sError.Format(_T("Inernet connection error : %d"), e->m_dwError);
// e->Delete();
// REPORT_CACHED_EXCEPTION(sError);
// }*/
//_err_handler:
// if (pHttpFile)
// {
// pHttpFile->Close();
// delete pHttpFile;
// }
// if (pHttpConnection)
// {
// pHttpConnection->Close();
// delete pHttpConnection;
// }
// session.Close();
// if (hFile) CloseHandle(hFile);
// return bResult;
//}
未调通,时间关系,转战matlab
clc;clear;close all;
fidin=fopen('dev_urls.txt','r');
i=0;
while ~feof(fidin)
tline=fgetl(fidin);
urlstart=strfind(tline,'http');
urlend=strfind(tline,'.jpg');
if(isempty(urlstart)||isempty(urlend))
continue;
end
url=tline(urlstart:urlend+3);
%imgName=tline(1:urlstart-1);
i=i+1;
imgName=[num2str(i) '.jpg'];
disp(['正在下载...' num2str(i)]);
[f,status] = urlwrite(url,imgName,'Timeout',3);
if(status==0)
disp(['第' num2str(i) '个链接无效..']);
else
disp(['第' num2str(i) '个链接有效..']);
end
clear url;
end
disp('下载完毕!');
fclose(fidin);
设置matlab的网络代理:File->Preferences->web