我已經制作了一個servlet,它使用org.apache.commons.fileupload api上傳CSV文件,然後將其加載到MySQL表中。當從瀏覽器中的表單發佈到servlet時,這是完美的。但是,嘗試通過Tcl腳本http://www.tcl.tk/man/tcl/TclCmd/http.htm#M20發佈表單時失敗。從Tcl腳本發佈時出現MalformedStreamException
該servlet將引發以下MalformedStreamException
:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException:
Stream ended unexpectedly
該servlet在Tomcat 6.0.16版本託管。
連接由Tcl腳本成功完成,因爲它接收到HTTP/1.1 200 OK響應,並且該servlet確實將一些打印語句返回給客戶端Tcl腳本。但是,嘗試讀取輸入流時失敗。
Tcl的腳本:
package require http
proc upload { url csv } {
set boundary "-----WebKitFormBoundary[clock seconds][pid]"
set fid [open $csv r]
if {[catch {read $fid [file size $csv]} data]} {
return -code error $data
}
close $fid
set content {}
append content "--${boundary}\n"
append content "Content-Disposition: form-data; name=\"db\"\n\n"
append content "test\n"
append content "--${boundary}\n"
append content "Content-Disposition: form-data; name=\"table\"\n\n"
append content "testing\n"
append content "--${boundary}\n"
append content "Content-Disposition: form-data; name=\"file\"; filename=\"$csv\"\n"
append content "Content-Type: text/csv\n\n"
append content "$data\n"
append content "${boundary}--"
set headers "connection keep-alive"
set token [::http::geturl $url -keepalive 1 -headers $headers -query $content -type "multipart/form-data; boundary=$boundary"]
upvar 0 $token state
if {$state(http) == "HTTP/1.1 200 OK"} {
# no error reported in http headers
puts stdout $state(http)
puts stdout $state(body)
return 1
} else {
# error reported in http headers
puts stdout $state(http)
puts stdout $state(body)
return 0
}
}
set csv "data.csv"
set url "http://ecat:8080/MySqlImport/MySqlImportServlet"
set retVal [upload $url $csv]
你可以發佈你用來發布的tcl代碼嗎?很可能你沒有發佈多個請求,這是fileupload api所期望的。 – 2012-02-14 17:24:25
更新爲添加tcl腳本 – Daniel 2012-02-15 11:49:13
我認爲終止邊界是' - $ {boundary} - \ n'? – 2012-02-15 13:38:23