当前位置:编程学习 > wap >>

从Java端接收图片数据时字节丢失

_LIT8(KUserAgent, "MegjoyClient 1.0");
_LIT8(KAccept, "*/*");
_LIT(KAcceptPackType,"Accepted :");
_LIT(KImageLength,"The image's lenght is :");
_LIT(KSeparator, "#");

CMegajoyHttpEngine* CMegajoyHttpEngine::NewL(MClientObserver& aObserver)
{
CMegajoyHttpEngine* self = CMegajoyHttpEngine::NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}


CMegajoyHttpEngine* CMegajoyHttpEngine::NewLC(MClientObserver& aObserver)
{
CMegajoyHttpEngine* self = new (ELeave) CMegajoyHttpEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}


CMegajoyHttpEngine::CMegajoyHttpEngine(MClientObserver& aObserver)
:iObserver(aObserver),
iPostData(NULL),
iRunning(EFalse)
{
}


CMegajoyHttpEngine::~CMegajoyHttpEngine()
{
iSession.Close();

delete iPostData;
iPostData = NULL;
}

void CMegajoyHttpEngine::ConstructL()
{
// Open RHTTPSession with default protocol ("HTTP/TCP")
TRAPD(err, iSession.OpenL());

if(err != KErrNone) {
// Most common error; no access point configured, and session creation leaves with KErrNotFound.
_LIT(KErrMsg,"Cannot create session. Is internet access point configured?");
_LIT(KExitingApp, "Exiting app.");
CEikonEnv::Static()->InfoWinL(KErrMsg, KExitingApp);
User::Leave(err);
}
}

void CMegajoyHttpEngine::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
{
RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue);
CleanupClosePushL(valStr);
THTTPHdrVal val(valStr);
aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField,RHTTPSession::GetTable()), val);
CleanupStack::PopAndDestroy(); // valStr
}

void CMegajoyHttpEngine::IssueHTTPPostL(const TDesC8& aUri, const TDesC8& aContentType, const TDesC8& aBody) 
{
// Parse string to URI
TUriParser8 uri; 
uri.Parse(aUri);

// Copy data to be posted into member variable; iPostData is used later in methods inherited from MHTTPDataSupplier.
delete iPostData;
iPostData = aBody.AllocL();

// Get request method string for HTTP POST
RStringF method = iSession.StringPool().StringF(HTTP::EPOST,RHTTPSession::GetTable());

// Open transaction with previous method and parsed uri. This class will receive transaction events in MHFRunL and MHFRunError.
iTransaction = iSession.OpenTransactionL(uri, *this, method);

// Set headers for request; user agent, accepted content type and body's content type.
RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
SetHeaderL(hdr, HTTP::EAccept, KAccept);
SetHeaderL(hdr, HTTP::EContentType, aContentType);

// Set this class as an data supplier. Inherited MHTTPDataSupplier methods are called when framework needs to send body data.
MHTTPDataSupplier* dataSupplier = this;
iTransaction.Request().SetBody(*dataSupplier);

// Submit the transaction. After this the framework will give transaction events via MHFRunL and MHFRunError.
iTransaction.SubmitL();

iRunning = ETrue;
_LIT(KConnecting,"Connecting...");
iObserver.ClientEvent(KConnecting);
DEBUGLOG("Connecting...",0,0,0);
}

void CMegajoyHttpEngine::CancelTransaction()
{
if(!iRunning) 
return;

// Close() also cancels transaction (Cancel() can also be used but resources allocated by transaction must be still freed with Close())
iTransaction.Close();

// Not running anymore
iRunning = EFalse;
_LIT(KTransactionCancelled, "Transaction cancelled");
iObserver.ClientEvent(KTransactionCancelled);
DEBUGLOG("Transaction cancelled",0,0,0);
}


void CMegajoyHttpEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{

switch (aEvent.iStatus) 
{
case THTTPEvent::EGotResponseHeaders:
{
RHTTPResponse resp = aTransaction.Response();
TInt status = resp.StatusCode();

TBuf<32> statusText;
statusText.Copy(resp.StatusText().DesC());

TBuf<256> text;
_LIT(KHeaderReceived, "Header received. Status: %d %S");
text.Format(KHeaderReceived, status, &statusText);
iObserver.ClientEvent(text);
}
break;

case THTTPEvent::EGotResponseBodyData:
{
MHTTPDataSupplier* body = aTransaction.Response().Body();

// GetNextDataPart() returns ETrue, if the received part is the last one.
TBool isLast = body->GetNextDataPart(dataChunk);
iObserver.ClientBodyReceived(dataChunk);

TBuf<64> text;
_LIT(KBodyPartReceived, "%d bytes received... ");
text.Format(KBodyPartReceived, dataChunk.Length());
iObserver.ClientEvent(text);


if (isLast)
{
_LIT(KBodyReceived,"Body received");
iObserver.ClientEvent(KBodyReceived);
CEikonEnv::Static()->InfoMsg(_L("the isLast is ETrue!"));
}

// Always remember to release the body data.
body->ReleaseData();
}
break;

case THTTPEvent::EResponseComplete:
{
_LIT(KTransactionComplete, "Transaction Complete");
iObserver.ClientEvent(KTransactionComplete);
}
break;

case THTTPEvent::ESucceeded:
{
ExternalizeConnInfoL(dataChunk);
_LIT(KTransactionSuccessful, "Transaction Successful");
iObserver.ClientEvent(KTransactionSuccessful);
_LIT(KImagesName, "c:\\System\\Apps\\megajoy\\MyJpg.jpg");
ExternalizeConnInfoL(KImagesName,dataChunk);
HBufC* temp = HBufC::NewL(100);
temp->Des().AppendNum(dataChunk.Length());
iObserver.ClientEvent(*temp);
aTransaction.Close();
iRunning = EFalse;
}
break;

case THTTPEvent::EFailed:
{
// Transaction completed with failure. 
_LIT(KTransactionFailed, "Transaction Failed");
iObserver.ClientEvent(KTransactionFailed);
aTransaction.Close();
iRunning = EFalse;
}
break;

default:

{
TBuf<64> text;
if (aEvent.iStatus < 0)
{
_LIT(KErrorStr, "Error: %d");
text.Format(KErrorStr, aEvent.iStatus);
// Just close the transaction on errors
aTransaction.Close();
iRunning = EFalse;
} else {
// Other events are not errors (e.g. permanent and temporary redirections)
_LIT(KUnrecognisedEvent, "Unrecognised event: %d");
text.Format(KUnrecognisedEvent, aEvent.iStatus);
}
iObserver.ClientEvent(text);
}
break;
}



TInt CMegajoyHttpEngine::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/)
{
// Just notify about the error and return KErrNone.
TBuf<64> text;
_LIT(KRunError, "MHFRunError: %d");
text.Format(KRunError, aError);
iObserver.ClientEvent(text);
return KErrNone;
}


TBool CMegajoyHttpEngine::GetNextDataPart(TPtrC8& aDataPart)
{
if(iPostData) 
{
aDataPart.Set(iPostData->Des());
}
return ETrue;
}



void CMegajoyHttpEngine::ReleaseData()
{
// It's safe to delete iPostData now.
delete iPostData;
iPostData = NULL;
}



TInt CMegajoyHttpEngine::Reset()
{
return KErrNone;
}



TInt CMegajoyHttpEngine::OverallDataSize()
{
if(iPostData)
return iPostData->Length();
else
return KErrNotFound ;
}


我用的就是上面这个程序,SDK下自带的!
但是服务器发给我的是1589个字节,我接收过来打印出来只有1266个字节.
不知道谁碰到过这种问题
谢谢 --------------------编程问答-------------------- 我已经查过了 server端没错误  用这些字节可以在本地构造一个jpg文件 并且大小也正常
补充:移动开发 ,  Symbian
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,