之前发表过一篇ckeditor上传文件的博客,其中用到一个插件,是用delphi开发的ActiveX,先将ocx的代码发上来,代码很少,不过需要找工具生成一下,由于我的代码也是在公司里,公司里限制,ocx、dll文件无法拷出上传,只能贴源码了。 插件在jsp和aspx里的接收流是一样的,具体可参考以前的aspx的接受代码实例。
公司里的同事已经测试通过,在项目里正式使用了
// UpImage.cpp : Implementation of CActiveXUpImgApp and DLL registration.
#include "stdafx.h"
#include "ActiveXUpImg.h"
#include "UpImage.h"
#include "string"
#include "iostream"
#include "stdio.h"
#include "Wininet.h"
#pragma comment(lib, "Wininet.lib")
#define BUF_SIZE 1024
using namespace std;
/
//
STDMETHODIMP UpImage::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_IUpImage,
};
for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
STDMETHODIMP UpImage::UploadImg2Server(long a,long b)
{
// TODO: Add your implementation code here
char* Ip="192.168.0.133";
int port=3011;
char* upFile="c:/kinectsensor.jpg";
HINTERNET hSession=NULL, hConnect=NULL, hRequest=NULL;
hSession = InternetOpen("test", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
hConnect = InternetConnect(hSession, Ip, port,//INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL,INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, NULL);
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet;
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
hRequest = HttpOpenRequest(hConnect, "POST", "xml_convert", NULL, NULL, NULL, 0, 0); // xml_convert请求实体
if (!hRequest)
{
printf("Failed to open request handle: %lu\n", GetLastError ());
return FALSE;
}
HANDLE hFile = CreateFile(upFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("\nFailed to open local file %s.", upFile);
return FALSE;
}
BufferIn.dwBufferTotal = GetFileSize(hFile, NULL);
printf ("File size is %d\n", BufferIn.dwBufferTotal );
if(!HttpSendRequestEx(hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
printf("Error on HttpSendRequestEx %lu\n",GetLastError());
return FALSE;
}
DWORD sum = 0;
do
{
if (!(bRead = ReadFile(hFile, pBuffer, sizeof(pBuffer), &dwBytesRead, NULL)))
{
printf ("\nReadFile failed on buffer %lu.",GetLastError());
break;
}
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
{
printf ("\nInternetWriteFile failed %lu", GetLastError());
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer)) ;
CloseHandle(hFile);
printf ("Actual written bytes: %d\n", sum);
if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
int contextLengthId = HTTP_QUERY_CONTENT_LENGTH;
int statusCodeId = HTTP_QUERY_STATUS_CODE;
int statusTextId = HTTP_QUERY_STATUS_TEXT;
char szBuf[BUF_SIZE] = {0};
DWORD dwSize = BUF_SIZE;
char pcBuffer[BUF_SIZE];
DWORD dwByteRead;
//printf("\n\n服务端返回如下:\n");
do
{
dwBytesRead = 0;
if(InternetReadFile(hRequest, pcBuffer, BUF_SIZE - 1, &dwByteRead))
{
pcBuffer[dwByteRead] = 0x00; // Null-terminate buffer
printf("%s", pcBuffer);
memset(pcBuffer, 0, BUF_SIZE);
}
else
{
printf("InternetReadFile failed\n");
}
}while(dwByteRead>0);
/*if (HttpQueryInfo(hRequest, statusTextId, szBuf, &dwSize, 0))
{
szBuf[dwSize] = 0;
printf("Status text:[%s]\n", szBuf);
}*/
//cout<<"上传成功"<<endl;
return TRUE;
return S_OK;
}