![](/img/trans.png)
[英]My base64 encoded byte[] stream has extra characters after sent through a http response
[英]python cgi http response adds extra byte to content
我一直在使用python cgi腳本從數據庫獲取文件,但一切正常,但是由於某種原因,似乎在文件中添加了一個額外的字節。 在數據庫上,大小為10,265字節,但是在http響應中,Content-Length為10,266,問題似乎出在http響應本身。 問題在於所提供的文件是.jar,並且被Java應用程序使用,由於該額外的字節,Java應用程序隨后無法使用類加載器加載它們。 用於從服務器下載內容的代碼段為:
def printFileHeader():
print 'Content-Type: text/plain;charset=utf-8'
print
def downloadAddon(addon_id):
dbConn = sqlite3.connect("addons.db")
dbCursor = dbConn.cursor()
dbCursor.execute("SELECT addon_file FROM uploaded WHERE id="+addon_id)
blobl = dbCursor.fetchone()
blobl = blobl[0]
printFileHeader()
print blobl
然后,使用請求的id調用downloadAddon()
函數,但是無論我從哪里獲取文件(數據庫或直接文件中的blob),即使服務器端文件正常,http響應在內容中始終具有該額外字節。 歡迎任何幫助。
PS。 我知道標頭不是正確的文件標頭,但出於測試目的,我以這種方式放置了標頭。
我設法通過在標頭中提供文件的內容長度來“解決”該問題,因此代碼現在如下所示:
def printFileHeader(size):
print("Content-Disposition: attachment; filename=addon.jar")
print("Content-Type: application/octet-stream")
print("Content-Length: "+str(size))
print
def downloadAddon(addon_id):
dbCursor.execute("SELECT addon_file FROM uploaded WHERE id="+addon_id)
blobl = dbCursor.fetchone()
blobl = blobl[0]
printFileHeader(len(blobl))
print(blobl)
這解決了問題,但是我仍然不明白為什么,所以仍然歡迎任何解釋。 同樣,在檢查修復前后的響應時,這里是文件的最后6個字節:
之前(帶有額外的字節) : AAAAAK
之后 : AAAAA=
關於為什么的任何解釋都值得贊賞
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.