📄 appoctetstream.cpp
字号:
// AppOctetStream.cpp: implementation of the CAppOctetStream class.
// Author: Wes Clyburn (clyburnw@enmu.edu)
//////////////////////////////////////////////////////////////////////
#include "../stdafx.h"
#include "AppOctetStream.h"
#include "Base64.h"
#include "MIMEMessage.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// IMPORTANT: The number of bytes we read must be
// a multiple of 3 because CBase64's Encode()
// method will append padding characters ('=')
// to make the output's size a multiple of 4.
// (Base64 treats 3 8-bit bytes as 4 6-bit 'bytes').
// MIME decoders are free to treat '=' as a signal
// that there's no more data, so we don't want to pad
// until we're supposed to.
// When at the end of the file, the # of bytes read
// may not be a multiple of 3, but that's okay
// because we DO want the padding chars then.
#define BYTES_TO_READ 54 // This number guarantess output won't
// won't exceed line-length limit
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAppOctetStream::CAppOctetStream( int nContentType )
:CMIMEContentAgent( nContentType )
{
}
CAppOctetStream::~CAppOctetStream()
{
}
BOOL CAppOctetStream::AppendPart(LPCTSTR szContent,
LPCTSTR szParameters,
int nEncoding,
BOOL bPath,
CString & sDestination)
{
CStdioFile fAttachment;
ASSERT( szContent != NULL );
if( szContent == NULL )
return FALSE;
//打开附件文件
if( !fAttachment.Open( szContent, (CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary) ) )
return FALSE;
//创建信息头
sDestination += build_sub_header( szContent,
szParameters,
nEncoding,
TRUE );
//对附件文件进行编码
attach_file( &fAttachment, CMIMEMessage::BASE64, sDestination );
fAttachment.Close();
return TRUE;
}
CString CAppOctetStream::build_sub_header(LPCTSTR szContent,
LPCTSTR szParameters,
int nEncoding,
BOOL bPath)
{
CString sSubHeader;
CString sTemp;
TCHAR szFName[ _MAX_FNAME ];
TCHAR szExt[ _MAX_EXT ];
//分离出文件目录
_tsplitpath( szContent, NULL, NULL, szFName, szExt );
if( bPath )
sTemp.Format( "; file=%s%s", szFName, szExt );
else
sTemp = _T( "" );
//创建Content-Type信息头
sSubHeader.Format( _T( "Content-Type: %s%s\r\n" ),
(LPCTSTR)GetContentTypeString(),
(LPCTSTR)sTemp );
//表明编码方式为Base64
sSubHeader += _T( "Content-Transfer-Encoding: base64\r\n" );
sTemp.Format( _T( "Content-Disposition: attachment; filename=%s%s\r\n" ),
szFName, szExt );
sSubHeader += sTemp;
//表明信息头结束
sSubHeader += _T( "\r\n" );
return sSubHeader;
}
CString CAppOctetStream::GetContentTypeString()
{
CString s;
s = _T( "application/octet-stream" );
return s;
}
// 读取附件文件进行编码
//pFileAtt:文件指针
//nEncoding:编码类型
//sDestination:目标字符串
void CAppOctetStream::attach_file(CStdioFile* pFileAtt,
int nEncoding,
CString & sDestination)
{
//编码类
CMIMECode* pEncoder;
int nBytesRead;
TCHAR szBuffer[ BYTES_TO_READ + 1 ];
ASSERT( pFileAtt != NULL );
if( pFileAtt == NULL )
return;
switch( nEncoding )
{
default:
case CMIMEMessage::BASE64:
try
{
pEncoder = new CBase64;
}
catch( CMemoryException* e )
{
delete e;
return;
}
}
if( pEncoder == NULL )
return;
do
{
try
{
//读取一行文件数据进行编码
nBytesRead = pFileAtt->Read( szBuffer, BYTES_TO_READ );
}
catch( CFileException* e )
{
delete e;
break;
}
szBuffer[ nBytesRead ] = 0;
//进行base64编码
sDestination += pEncoder->Encode( szBuffer, nBytesRead );
sDestination += _T( "\r\n" );
} while( nBytesRead == BYTES_TO_READ );
sDestination += _T( "\r\n" );
delete pEncoder;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -