在網(wǎng)絡(luò)編程過程中需要向服務(wù)器上傳文件。Multipart/form-data是上傳文件的一種方式。

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的康巴什網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Multipart/form-data其實就是瀏覽器用表單上傳文件的方式。最常見的情境是:在寫郵件時,向郵件后添加附件,附件通常使用表單添加,也就是用multipart/form-data格式上傳到服務(wù)器。
表單形式上傳附件
具體的步驟是怎樣的呢?
首先,客戶端和服務(wù)器建立連接(TCP協(xié)議)。
第二,客戶端可以向服務(wù)器端發(fā)送數(shù)據(jù)。因為上傳文件實質(zhì)上也是向服務(wù)器端發(fā)送請求。
第三,客戶端按照符合“multipart/form-data”的格式向服務(wù)器端發(fā)送數(shù)據(jù)。
既然Multipart/form-data格式就念敗是瀏覽器用表單提交數(shù)據(jù)的格式,我們就來看看文件經(jīng)過瀏覽器編碼后是什么樣子。
點擊“Browse…”分別選擇“unknow.gif”和“unknow1.gif”文件,點擊“submit”按紐后,文件將被上傳到服務(wù)器。
下面是服務(wù)器收到的數(shù)據(jù):
服務(wù)器收到的數(shù)據(jù)
這是一個POST請求。所以數(shù)據(jù)是放在請求體內(nèi),而不是請求頭內(nèi)。
這行指出這個請求是“multipart/form-data”格式的,且“boundary”是 “---------------------------7db15a14291cce”這個行瞎字符串。
不難想象,“boundary”是用來隔開表單中不同部分?jǐn)?shù)據(jù)的。例子中的表單就有 2 部分?jǐn)?shù)據(jù),用“boundary”隔開。“boundary”一般由系統(tǒng)隨機(jī)產(chǎn)生,但也可以簡單的用“-------------”來代替。
實際上,每部分?jǐn)?shù)據(jù)的開頭都是由"--"仔帶顫 + boundary開始,而不是由 boundary 開始。仔細(xì)看才能發(fā)現(xiàn)下面的開頭這段字符串實際上要比 boundary 多了個 “--”
緊接著 boundary 的是該部分?jǐn)?shù)據(jù)的描述。
接下來才是數(shù)據(jù)。
“GIF”gif格式圖片的文件頭,可見,unknow1.gif確實是gif格式圖片。
在請求的最后,則是 "--" + boundary + "--" 表明表單的結(jié)束。
需要注意的是,在html協(xié)議中,用 “\r\n” 換行,而不是 “\n”。
下面的代碼片斷演示如何構(gòu)造multipart/form-data格式數(shù)據(jù),并上傳圖片到服務(wù)器。
//---------------------------------------
// this is the demo code of using multipart/form-data to upload text and photos.
// -use WinInet APIs.
//
//
// connection handlers.
//
HRESULT hr;
HINTERNET m_hOpen;
HINTERNET m_hConnect;
HINTERNET m_hRequest;
//
// make connection.
//
...
//
// form the content.
//
std::wstring strBoundary = std::wstring(L"------------------");
std::wstring wstrHeader(L"Content-Type: multipart/form-data, boundary=");
wstrHeader += strBoundary;
HttpAddRequestHeaders(m_hRequest, wstrHeader.c_str(), DWORD(wstrHeader.size()), HTTP_ADDREQ_FLAG_ADD);
//
// "std::wstring strPhotoPath" is the name of photo to upload.
//
//
// uploaded photo form-part begin.
//
std::wstring strMultipartFirst(L"--");
strMultipartFirst += strBoundary;
strMultipartFirst += L"\r\nContent-Disposition: form-data; name=\"pic\"; filename=";
strMultipartFirst += L"\"" + strPhotoPath + L"\"";
strMultipartFirst += L"\r\nContent-Type: image/jpeg\r\n\r\n";
//
// "std::wstring strTextContent" is the text to uploaded.
//
//
// uploaded text form-part begin.
//
std::wstring strMultipartInter(L"\r\n--");
strMultipartInter += strBoundary;
strMultipartInter += L"\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n";
std::wstring wstrPostDataUrlEncode(CEncodeTool::Encode_Url(strTextContent));
// add text content to send.
strMultipartInter += wstrPostDataUrlEncode;
std::wstring strMultipartEnd(L"\r\n--");
strMultipartEnd += strBoundary;
strMultipartEnd += L"--\r\n";
//
// open photo file.
//
// ws2s(std::wstring)
// -transform "strPhotopath" from unicode to ansi.
std::ifstream *pstdofsPicInput = new std::ifstream;
pstdofsPicInput-open((ws2s(strPhotoPath)).c_str(), std::ios::binary|std::ios::in);
pstdofsPicInput-seekg(0, std::ios::end);
int nFileSize = pstdofsPicInput-tellg();
if(nPicFileLen == 0)
{
return E_ACCESSDENIED;
}
char *pchPicFileBuf = NULL;
try
{
pchPicFileBuf = new char[nPicFileLen];
}
catch(std::bad_alloc)
{
hr = E_FAIL;
}
if(FAILED(hr))
{
return hr;
}
pstdofsPicInput-seekg(0, std::ios::beg);
pstdofsPicInput-read(pchPicFileBuf, nPicFileLen);
if(pstdofsPicInput-bad())
{
pstdofsPicInput-close();
hr = E_FAIL;
}
delete pstdofsPicInput;
if(FAILED(hr))
{
return hr;
}
// Calculate the length of data to send.
std::string straMultipartFirst = CEncodeTool::ws2s(strMultipartFirst);
std::string straMultipartInter = CEncodeTool::ws2s(strMultipartInter);
std::string straMultipartEnd = CEncodeTool::ws2s(strMultipartEnd);
int cSendBufLen = straMultipartFirst.size() + nPicFileLen + straMultipartInter.size() + straMultipartEnd.size();
// Allocate the buffer to temporary store the data to send.
PCHAR pchSendBuf = new CHAR[cSendBufLen];
memcpy(pchSendBuf, straMultipartFirst.c_str(), straMultipartFirst.size());
memcpy(pchSendBuf + straMultipartFirst.size(), (const char *)pchPicFileBuf, nPicFileLen);
memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen, straMultipartInter.c_str(), straMultipartInter.size());
memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen + straMultipartInter.size(), straMultipartEnd.c_str(), straMultipartEnd.size());
//
// send the request data.
//
HttpSendRequest(m_hRequest, NULL, 0, (LPVOID)pchSendBuf, cSendBufLen)
一般可以通過scp命令,ftp服頌培務(wù),野爛唯git倉庫等多種方式向服務(wù)器傳輸文件
可以選擇自己熟悉歷清的方式上傳
可以基于支持無線視頻流云端推送的WiFi模塊來實現(xiàn):
方案構(gòu)成:無線視頻流云端推送方案由USB攝像頭,QCA9531 WiFi模塊、云端視頻服務(wù)器及接收視頻的手機(jī)組成。
方案設(shè)計:在網(wǎng)絡(luò)USB攝像頭的PCB板上,嵌入QCA9531 WiFi模塊,通過USB接口將攝像頭連接到WiFi模塊,WiFi模塊將攝像頭采集的視頻傳輸上傳到云端視頻服務(wù)器,并提供視頻播放地址,用戶通過手機(jī)端訪問播放地址即可實時播放視頻。
工作原理:用戶在云端架設(shè)好云端視頻服務(wù)器,通過WiFi模塊采集USB攝像頭視頻內(nèi)容,并將視頻數(shù)據(jù)上傳到云端視頻服務(wù)器;手機(jī)端通過訪問云端視頻服務(wù)器提供的播放地址,獲取視頻流,實時查看USB攝像頭采集的視頻。
硬件準(zhǔn)備:一臺電腦,一根網(wǎng)線,一個UVC免驅(qū)動攝像頭,一塊SKW99模塊,一塊SKW99 EVB板,DC適配器,兩根WiFi模塊天線
接線:攝像頭通過USB連接螞緩到WIFI模塊,WIFI通過無線中繼或WAN口或4G/5G模塊上網(wǎng)將攝像頭采集悶鋒模的數(shù)據(jù)上傳到服務(wù)器,下圖是方案演示接線圖,此處使用基粗無線中繼上網(wǎng),將攝像頭采集的數(shù)據(jù)上傳到服務(wù)器。
網(wǎng)頁配置:申請云平臺賬號(支持主流云平臺,本篇以騰訊云直播平臺為例),開通云直播,注冊推流域名,播放域名,并對域名進(jìn)行備案和解析;
在騰訊云直播上創(chuàng)建推流地址,并在WIFI模塊的對應(yīng)界面配置相應(yīng)信息,主要是攝像頭參數(shù)信息和視頻服務(wù)器信息;
使用VLC查看推流結(jié)果,若是使用騰訊云推流,則播放如圖一:使用騰訊云推流后,配置播放地址后的配置,按上圖的播放地址,在VLC的“網(wǎng)絡(luò)”中輸入上面的播放地址,如下圖進(jìn)行播放。
若是騰訊云直播服務(wù)器推流,可通過微信關(guān)注“騰訊云視頻云”小程序,打開小程序按下圖操作;
嵌入式RTMP/RTSP協(xié)議,可獨立推流到流媒體服務(wù)器;
UVC免驅(qū)攝像頭掛載,攝像頭可支持H.264/MJPG/YUYV視頻格式;
支持720P@30幀/130萬像素,本地視頻服支持1080P@30幀/200萬像素;
本文名稱:向騰訊云服務(wù)器傳文件格式 騰訊云服務(wù)器文件下載到本地
文章位置:http://chinadenli.net/article20/dspghjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站改版、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作、App開發(fā)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)