通过 http 协议上传文件(rfc1867协议概述,jsp 应用举例,客户端发送内容构造) http://www.cnblogs.com/kaixuan/archive/2008/01/31/1060284.html
VC HTTP Post 上传文件 windows客户端+php服务端
客户端:
#include <afx.h>
#include <afxinet.h>
#include <winsock2.h>
//using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define MAXRESPONSESIZE 10240
#define MAXREQUESTSIZE 1024000
#define DEFAULT_PAGE_BUF_SIZE 2048000 //返回头文件最大长度
int uploadData(char *sSQLFileName,int nSQLFileSize);
int main(int argc,char *argv[])
{
char sfname[256]="E:\\textUpload.txt";
FILE *fpsize=fopen(sfname,"r");
if (fpsize==NULL)
{
printf("Error:open filesize\n");
return -1;
}
int nFS=0;
fseek(fpsize,0,SEEK_END);
nFS = ftell(fpsize);
fclose(fpsize);
printf("FileName=%s,FileSize=%d\n",sfname,nFS);
getchar();
uploadData(sfname,nFS);
return 0;
};
//返回值说明
//0:执行成功正常返回
//1、2、3:初始化socket错误
//4:域名解析错误
//5:连接服务器错误
//6:向服务器发送数据错误
//7:网页状态不对
int uploadData(char *sSQLFileName,int nSQLFileSize)
{
int err;
WORD wVersion;
WSADATA WSAData;
wVersion=MAKEWORD(2,0);
err=WSAStartup(wVersion,&WSAData);
if(err!=0) return 1;
if(LOBYTE( WSAData.wVersion ) != 2)
{
WSACleanup();
return 2;
}
struct protoent *ppe;
ppe=getprotobyname("tcp");
SOCKET m_s=NULL;
m_s=socket(PF_INET,SOCK_STREAM,ppe->p_proto);
if(m_s==INVALID_SOCKET) return 3;
//从配置文件中获得??????????????????????????????????????????????????????????????????????????
CString m_strRequest="http://127.0.0.1/uploadFile.php";
//const char *pRequestHeader = NULL;
CString strServer,strObject;
unsigned short nPort;
DWORD dwServiceType;
AfxParseURL(m_strRequest,dwServiceType,strServer,strObject,nPort);
if (0==strObject.GetLength() || 0==strServer.GetLength())
{
if (0==strServer.GetLength())
{
strServer=m_strRequest.Mid(7,m_strRequest.Find("/",7)-7);
int nm=strServer.Find(":");
if (-1!=nm) strServer=strServer.Left(nm);
}
nPort=80;
int nH=strServer.GetLength();
int nPHS=m_strRequest.Find(strServer);
int nPH=m_strRequest.Find("/",nPHS+nH);
int nsp=m_strRequest.Left(nPH).Find(":",nPHS+nH);
if(-1!=nsp)
{
char strTmp[100];
sprintf(strTmp,"%s",m_strRequest.Mid(nsp+1,nPH-nsp-1));
nPort=atoi(strTmp);
}
strObject=m_strRequest.Mid(nPH,m_strRequest.GetLength()-nPH);
}
int m_port=nPort;;
char szPort[10];
sprintf(szPort,"%d",m_port);
hostent *m_phostent;
m_phostent=gethostbyname((LPTSTR)(LPCTSTR)strServer);
if(m_phostent==NULL)
{
printf("Error:gethostbyname\n");
return 4;
}
else
{
struct in_addr ip_addr;
memcpy(&ip_addr,m_phostent->h_addr_list[0],4);///h_addr_list[0]里4个字节,每个字节8位
struct sockaddr_in destaddr;
memset((void *)&destaddr,0,sizeof(destaddr));
destaddr.sin_family=AF_INET;
destaddr.sin_port=htons(m_port);
destaddr.sin_addr=ip_addr;
char m_ipaddr[256];
for(int i=0;i<256;i++)
m_ipaddr[i]='\0';
sprintf(m_ipaddr,"%d.%d.%d.%d",
destaddr.sin_addr.S_un.S_un_b.s_b1,
destaddr.sin_addr.S_un.S_un_b.s_b2,
destaddr.sin_addr.S_un.S_un_b.s_b3,
destaddr.sin_addr.S_un.S_un_b.s_b4);
if(connect(m_s,(struct sockaddr*)&destaddr,sizeof(destaddr))!=0)
{
return 5;
}
}
char *m_requestheader; //请求头
m_requestheader=(char *)malloc(sizeof(char)*(MAXREQUESTSIZE+1));
memset(m_requestheader,0,MAXREQUESTSIZE);
m_requestheader[MAXREQUESTSIZE]='\0';
memset(m_requestheader,'\0',MAXREQUESTSIZE);
strcat(m_requestheader,"POST ");//
strcat(m_requestheader,(LPTSTR)(LPCTSTR)strObject);//
strcat(m_requestheader," HTTP/1.1");//
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"Host: ");//
strcat(m_requestheader,(LPTSTR)(LPCTSTR)strServer);//
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*");
strcat(m_requestheader,"\r\n");//
strcat(m_requestheader,"Accept-Language: zh-cn");//
strcat(m_requestheader,"\r\n");
char m_sBoundary_start[512]="---------------------------7b4a6d158c9\r\n";
strcat(m_requestheader,"Content-Type: multipart/form-data; boundary=");
strcat(m_requestheader,m_sBoundary_start);
strcat(m_requestheader,"Accept-Encoding: gzip, deflate");//
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"User-Agent: Mozilla/4.0 (compatible; bmdspider; windows 5.1)");//
strcat(m_requestheader,"\r\n");
char m_sContDesc[1024];
//sprintf(m_sContDesc,"Content-Disposition:form-data; name=\"filePath\"; filename=\"%s\"\r\nContent-Type:application/octet-stream\r\n\r\n",sSQLFileName);
sprintf(m_sContDesc,"Content-Disposition: form-data; name=\"filePath\"; filename=\"%s\"\r\nContent-Type: text/plain\r\n\r\n",sSQLFileName);
char m_sBoundary_end[1024]="\r\n-----------------------------7b4a6d158c9--\r\n";
int nRequerstSize=nSQLFileSize+strlen("--")+strlen(m_sBoundary_start)+strlen(m_sBoundary_end)+strlen(m_sContDesc);
strcat(m_requestheader,"Content-Length: ");//
char strTempLen[10];
itoa(nRequerstSize,strTempLen,10);
strcat(m_requestheader,strTempLen);
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"Connection: Keep-Alive");//
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"Cache-Control: no-cache");//
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"\r\n");
strcat(m_requestheader,"--");
strcat(m_requestheader,m_sBoundary_start);
strcat(m_requestheader,m_sContDesc);
int nType=1;
DWORD dwErr;
int nTime=30000;
//dwErr=setsockopt(m_s,SOL_SOCKET,nType,(char*)&nTime,sizeof(nTime));
if(send(m_s,m_requestheader,strlen(m_requestheader),0)==SOCKET_ERROR)
{
return 6;
}
printf("%s",m_requestheader);
FILE *fpSQL=fopen(sSQLFileName,"rb");
if(fpSQL==NULL)
{
return 8;
}
else
{
char sBuff[1024];
while (fgets(sBuff,1024,fpSQL))
{
if(send(m_s,sBuff,strlen(sBuff),0)==SOCKET_ERROR)
{
return 6;
}
printf("%s",sBuff);
}
fclose(fpSQL);
}
if(send(m_s,m_sBoundary_end,strlen(m_sBoundary_end),0)==SOCKET_ERROR)
{
return 6;
}
printf("%s",m_sBoundary_end);
nType=0;
dwErr=setsockopt(m_s,SOL_SOCKET,nType,(char*)&nTime,sizeof(nTime));
char m_ResponseHeader[MAXRESPONSESIZE]; //回应头
memset(m_ResponseHeader,0,MAXRESPONSESIZE);
int nFileSize = 0;
char c = 0;
int nIndex = 0;
BOOL bEndResponse = FALSE;
while(!bEndResponse && nIndex < MAXRESPONSESIZE)
{
recv(m_s,&c,1,0);
m_ResponseHeader[nIndex++] = c;
if(nIndex >= 4)
{
if(m_ResponseHeader[nIndex - 4] == '\r' && m_ResponseHeader[nIndex - 3] == '\n'
&& m_ResponseHeader[nIndex - 2] == '\r' && m_ResponseHeader[nIndex - 1] == '\n')
bEndResponse = TRUE;
}
}
int m_nResponseHeaderSize = nIndex;
printf("%s",m_ResponseHeader);
CString strRespons;
strRespons = m_ResponseHeader;
char scValue[50];
int nPos = -1;
nPos = strRespons.Find("HTTP/1.",0);
if(nPos != -1)
{
nPos += strlen("HTTP/1.");
int nCr = strRespons.Find("\r\n",nPos);
CString strValue = strRespons.Mid(nPos,nCr - nPos);
strcpy(scValue,strValue);
}
scValue[5]='\0';
for(int nt=0;nt<4;nt++)
{
scValue[nt]=scValue[nt+2];
}
int pageState=atoi(scValue);
if(pageState>=400 || pageState==0)
{
return 7;
}
nPos = -1;
nPos = strRespons.Find("Content-Length: ",0);
if(nPos != -1)
{
nPos += strlen("Content-Length: ");
int nCr = strRespons.Find("\r\n",nPos);
CString strValue = strRespons.Mid(nPos,nCr - nPos);
strcpy(scValue,strValue);
nFileSize=atoi(scValue);
}
else
{
nFileSize = DEFAULT_PAGE_BUF_SIZE;
}
char * pData;
pData=(char *) malloc((nFileSize+1)*sizeof(char));
memset(pData,'0',nFileSize);
strcpy(pData,"");
int bytesReads=0;
int nReceSize = 0;
char strTempFile[1024]="";
int nReads=0;
if(nFileSize==DEFAULT_PAGE_BUF_SIZE)
nReads=1024;
while((bytesReads+nReads) < nFileSize)
{
memset(strTempFile,0,1024);
nReceSize=recv(m_s,strTempFile,1024,0);
if(nReceSize == 0)
{
break;
}
if(nReceSize == -1)
{
break;
}
if(nReceSize>0)
{
bytesReads +=nReceSize;
}
strcat(pData,strTempFile);
}
pData[bytesReads]='\0';
printf("%s\n",pData);
free(pData);
pData=NULL;
if(m_s != NULL) closesocket(m_s);
m_s = NULL;
if(m_requestheader!=NULL)
{
free(m_requestheader);
m_requestheader=NULL;
}
return 0;
};
服务端:uploadFile.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传</title>
</head>
<body>
<?
$uploaddir = 'D:/uploads/';
$uploadfile = $uploaddir . basename($_FILES['filePath']['name']);
if (move_uploaded_file($_FILES['filePath']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
}
?>
<div align="center">
<form enctype="multipart/form-data" action="uploadFile.php" method="post">
<input type="file" name="filePath" style="width:300px; height:30px"/><input type="submit" value="上传" style="height:30px"/>
</form>
</div>
</body>
</html>
过 http 协议上传文件(rfc1867协议概述,jsp 应用举例,客户端发送内容构造)
1、概述
在最初的 http 协议中,没有上传文件方面的功能。 rfc1867 (http://www.ietf.org/rfc/rfc1867.txt) 为 http 协议添加了这个功能。客户端的浏览器,如 Microsoft IE, Mozila, Opera 等,按照此规范将用户指定的文件发送到服务器。服务器端的网页程序,如 php, asp, jsp 等,可以按照此规范,解析出用户发送来的文件。
Microsoft IE, Mozila, Opera 已经支持此协议,在网页中使用一个特殊的 form 就可以发送文件。
绝大部分 http server ,包括 tomcat ,已经支持此协议,可接受发送来的文件。
各种网页程序,如 php, asp, jsp 中,对于上传文件已经做了很好的封装。
2、上传文件的实例:用 servelet 实现(http server 为 tomcat 4.1.24)
1. 在一个 html 网页中,写一个如下的form :
<form enctype="multipart/form-data" action="http://192.168.29.65/UploadFile" method=post>
load multi files :<br>
<input name="userfile1" type="file"><br>
<input name="userfile2" type="file"><br>
<input name="userfile3" type="file"><br>
<input name="userfile4" type="file"><br>
text field :<input type="text" name="text" value="text"><br>
<input type="submit" value="提交"><input type=reset>
</form>
2. 服务端 servelet 的编写
现在第三方的 http upload file 工具库很多。Jarkata 项目本身就提供了fileupload 包http://jakarta.apache.org/commons/fileupload/ 。文件上传、表单项处理、效率问题基本上都考虑到了。在 struts 中就使用了这个包,不过是用 struts 的方式另行封装了一次。这里我们直接使用 fileupload 包。至于struts 中的用法,请参阅 struts 相关文档。
这个处理文件上传的 servelet 主要代码如下:
public void doPost( HttpServletRequest request, HttpServletResponse response ) {
DiskFileUpload diskFileUpload = new DiskFileUpload();
// 允许文件最大长度
diskFileUpload.setSizeMax( 100*1024*1024 );
// 设置内存缓冲大小
diskFileUpload.setSizeThreshold( 4096 );
// 设置临时目录
diskFileUpload.setRepositoryPath( "c:/tmp" );
List fileItems = diskFileUpload.parseRequest( request );
Iterator iter = fileItems.iterator();
for( ; iter.hasNext(); ) {
FileItem fileItem = (FileItem) iter.next();
if( fileItem.isFormField() ) {
// 当前是一个表单项
out.println( "form field : " + fileItem.getFieldName() + ", " + fileItem.getString() );
} else {
// 当前是一个上传的文件
String fileName = fileItem.getName();
fileItem.write( new File("c:/uploads/"+fileName) );
}
}
}
为简略起见,异常处理,文件重命名等细节没有写出。
3、 客户端发送内容构造
假设接受文件的网页程序位于 http://192.168.29.65/upload_file/UploadFile.
假设我们要发送一个二进制文件、一个文本框表单项、一个密码框表单项。文件名为 E:\s ,其内容如下:(其中的XXX代表二进制数据,如 01 02 03)
a
bb
XXX
ccc
客户端应该向 192.168.29.65 发送如下内容:
POST /upload_file/UploadFile HTTP/1.1
Accept: text/plain, */*
Accept-Language: zh-cn
Host: 192.168.29.65:80
Content-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6
User-Agent: Mozilla/4.0 (compatible; OpenOffice.org)
Content-Length: 424
Connection: Keep-Alive
-----------------------------7d33a816d302b6
Content-Disposition: form-data; name="userfile1"; filename="E:\s"
Content-Type: application/octet-stream
a
bb
XXX
ccc
-----------------------------7d33a816d302b6
Content-Disposition: form-data; name="text1"
foo
-----------------------------7d33a816d302b6
Content-Disposition: form-data; name="password1"
bar
-----------------------------7d33a816d302b6--
此内容必须一字不差,包括最后的回车。
注意:Content-Length: 424 这里的424是红色内容的总长度(包括最后的回车)
注意这一行:
Content-Type: multipart/form-data; boundary=---------------------------7d33a816d302b6
根据 rfc1867, multipart/form-data是必须的.
---------------------------7d33a816d302b6 是分隔符,分隔多个文件、表单项。其中33a816d302b6 是即时生成的一个数字,用以确保整个分隔符不会在文件或表单项的内容中出现。前面的 ---------------------------7d 是 IE 特有的标志。 Mozila 为---------------------------71
用手工发送这个例子,在上述的 servlet 中检验通过。
用户可以选择多个文件,填写表单其它项,点击“提交”按钮后就开始上传给 http://192.168.29.65/upload_file/UploadFile 这是一个 servelet 程序
注意 enctype="multipart/form-data", method=post, type="file" 。根据 rfc1867, 这三个属性是必须的。multipart/form-data 是新增的编码类型,以提高二进制文件的传输效率。具体的解释请参阅 rfc1867