用winsock通过http POST方式向服务器上传文件的问题
传输到服务器上的文件会出错,服务器端提示org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
如果我把传输文件内容替换成文本文件,就没有这个错误了,因此我怀疑是不是我的文件读取程序处需要进行什么处理??这一点我不太明白该怎么做,请赐教。
我的程序代码如下
[code]
Dim strHttp As String
Dim strBoundary As String
Dim strBody As String
Dim lngLength As Long
Dim filename as string
filename = "d:\abc.zip "
' create a boundary
strBoundary = "----adfasdf2342nasdfasdfasdf "
strBody = strBody & "-- " & strBoundary
strBody = strBody & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name= " "id " " "
strBody = strBody & vbCrLf
strBody = strBody & vbCrLf
strBody = strBody & "0001 "
strBody = strBody & vbCrLf
strBody = strBody & "-- " & strBoundary & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name= " "file " "; filename= " " " & _
filename & " " " " & vbCrLf
strBody = strBody & "Content-Type: application/zip " & vbCrLf
strBody = strBody & vbCrLf & getFileData(filename)
strBody = strBody & vbCrLf & "-- " & strBoundary & "-- " & vbCrLf
lngLength = Len(strBody)
strHttp = "POST /upload.jsp HTTP/1.0 " & vbCrLf
strHttp = strHttp & "Host: server " & vbCrLf
strHttp = strHttp & "Content-Type: multipart/form-data; boundary= " & strBoundary & vbCrLf
strHttp = strHttp & "Content-Length: " & lngLength & vbCrLf & vbCrLf
strHttp = strHttp & strBody
Winsock1.Protocol = sckTCPProtocol
Winsock1.RemoteHost = "server "
Winsock1.RemotePort = 80
Winsock1.SendData strHttp
Private Function getFileData(ByVal strPath As String)
Dim strReturn As String
Dim bin() As Byte
Dim lngLength As Long
lngLength = FileLen(strPath)
ReDim bin(lngLength - 1)
Open strPath For Binary As #1
Get #1, , bin
StrReturn = bin
getFileData = strReturn
Close #1
End Function
[/code]
承蒙赐教,不胜感激。
[解决办法]
来晚了~
[解决办法]
使用 WinInet API 比这方便,发布的时候不用带这个控件