我现在要做一个浏览器插件,用来浏览符和条件的图片,而这些图片放在服务器上,大至如下:
客户端(ActiveX插件)---》服务器后台程序---》数据库服务器
这样的话,我需要和服务器后台程序间进行通信并传输所需要的图片文件,在下没有网络编程的任何经验,还望高手不吝赐教,谢谢大家了。:)
客户端(ActiveX插件)---》服务器后台程序---》数据库服务器
这样的话,我需要和服务器后台程序间进行通信并传输所需要的图片文件,在下没有网络编程的任何经验,还望高手不吝赐教,谢谢大家了。:)
CString CDownInfoDlg::DownURL(CString strURL)
{
CInternetSession m_Session("DigitalTitan");
CHttpFile* pFile=NULL;
CException* e; TCHAR szTempPath[MAX_PATH],szTempFile[MAX_PATH];
DWORD dwResult=::GetTempPath(MAX_PATH,szTempPath);
CString strURLPath;
GetTempFileName(szTempPath,_T("DigitalTitan_"),0,szTempFile);
strURLPath=szTempFile;
TRY
{
pFile=(CHttpFile*)m_Session.OpenURL(strURL);
}
CATCH_ALL(e)
{
pFile=NULL;
AfxMessageBox("URL地址不合法",MB_ICONINFORMATION);
return "";
}
END_CATCH_ALL if(pFile)
{
DWORD dwStatus;
DWORD dwBufLen=sizeof(dwStatus);
BOOL bSuccess=pFile->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,&dwStatus,&dwBufLen);
if(bSuccess&&dwStatus>=200&&dwStatus<300)
{
CStdioFile m_File;
if(m_File.Open(strURLPath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary))
{
BYTE pBuf[1024];
DWORD dwRead;
do
{
dwRead=pFile->Read(pBuf,1024);
m_File.Write(pBuf,dwRead);
}
while(dwRead>0);
m_File.Close();
}
}
pFile->Close();
delete pFile;
}
else
{
m_Session.Close();
}
return strURLPath;
}
用JSP分析multipart/form-data基于表单的文件上传
http://blog.csdn.net/gjd111686/archive/2004/08/18/78324.aspx
用JSP分析multipart/form-data基于表单的文件上传
<%
int iTotalByte,iTotalRead,iReadByte;
iTotalByte=request.getContentLength();
iTotalRead=0;
iReadByte=0;
byte[] Buffer=new byte[iTotalByte];
if(iTotalByte>0)
{
for(;iTotalRead<iTotalByte;iTotalRead+=iReadByte)
{
try
{
iReadByte=request.getInputStream().read(Buffer,iTotalRead,iTotalByte-iTotalRead);
}
catch(Exception e)
{
e.printStackTrace();
}
}
String strContentType=request.getContentType();
//数据处理开始
String strBuffer=new String(Buffer);
%><!--<br>表单数据:<br>strBuffer<br>--><%
String strBoundary="--"+strContentType.substring(strContentType.lastIndexOf("=")+1,strContentType.length());
String strArray[]=strBuffer.split(strBoundary); String strSubString;
int iBegin,iEnd;
iBegin=0;iEnd=0;
String strFieldName="";
String strFieldValue="";
String strFilePath="";
String strFileName="";
String strFileType="";
boolean bTrue;
bTrue=false;
int iLocation=0;
for(int iIndex=1;iIndex<strArray.length-1;iIndex++)
{
strSubString=strArray[iIndex];
iBegin=strSubString.indexOf("name=\"",0);
if(iBegin!=-1)
{
strFieldName="";strFieldValue="";
strFilePath="";strFileName="";strFileType="";
iEnd=strSubString.indexOf("\"",iBegin+6);
strFieldName=strSubString.substring(iBegin+6,iEnd);
iBegin=strSubString.indexOf("filename=\"",0); if(iBegin!=-1)
{
bTrue=true;
}
iEnd=strSubString.indexOf("\r\n\r\n",0);
if(bTrue==true)
{
//文件路径
strFilePath=strSubString.substring(iBegin+10,strSubString.indexOf("\"",iBegin+10));strFileName=strFilePath.substring(strFilePath.lastIndexOf("\\")+1);
strFileType=strSubString.substring(strSubString.indexOf("Content-Type: ")+14,strSubString.indexOf("\r\n\r\n"));
%><!--<br>文件类型:<br>strFileType<br>--><%
//文件数据
iBegin=strSubString.indexOf("\r\n\r\n",iBegin);
strFieldValue=strSubString.substring(iBegin+4);
strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
%><!--<br>文件路径:<br>strFilePath<br>文件名称:<br>strFileName<br>--><%
byte[] pFile=strFieldValue.getBytes();
byte[] pFileExtend=new byte[pFile.length];
iLocation=strBuffer.indexOf("filename=\"",iLocation);
for(int kIndex=iLocation;kIndex<iTotalByte-2;kIndex++)
{
if(Buffer[kIndex]==13&&Buffer[kIndex+2]==13)
{iLocation=kIndex+4;break;}
}
for(int nIndex=0;nIndex<pFile.length;nIndex++)
{
pFileExtend[nIndex]=Buffer[iLocation+nIndex];
}
/*
//保存到Local Disk;
FileOutputStream pFileOutputStream=new FileOutputStream("F:\\Site_App\\UploadFile\\"+strFileName);
pFileOutputStream.write(pFileExtend);
pFileOutputStream.close();
*/
session.putValue(strFieldName+"_FileType",strFileType);
session.putValue(strFieldName+"_FilePath",strFilePath);
session.putValue(strFieldName+"_FileName",strFileName);
session.putValue(strFieldName,pFileExtend);
}
else
{
strFieldValue=strSubString.substring(iEnd+4);
strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
session.putValue(strFieldName,strFieldValue);
}
bTrue=false;
}
%><!--<br>表单域名:<br>strFieldName<br>表单域值:<br>strFieldValue<br>--><%
}
//数据处理结束
}
%>这样(String)session.getValue("表单域名")返回表单域值,而(byte[])session.getValue("File上传控件域名")返回的字节数组就可以用new ByteArrayInputStream(byte[])调用updateBinaryStream来更新到数据库了
<div style="width:100%;height:200px" id="ImageID"></div><br>
<input type=text value="http://YourHost/ImageLib/default.gif" id="ImageURLID">
<input type=button onclick="BrowseImage(ImageURLID.value);" vale="浏览图片">
<script>
function BrowseImage(strURL)
{
ImageID.innerHTML="<img src='"+strURL+"'>";
}
</script>