7,763
社区成员
发帖
与我相关
我的任务
分享
String filepath = "D:\\mypic.jpg";//图片路径
String POST_URL ="http://file.chinadatapay.com/img/upload?appkey=2712f8c6f0ce9390b9edafc53e9d65b4";
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(POST_URL);
FileBody fileBody = new FileBody(new File(filepath));
MultipartEntity entity = new MultipartEntity();
entity.addPart("data", fileBody);
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
System.out.println("返回结果:" + responseString);
//你需要根据出错的原因判断错误信息,并修改
httpclient.getConnectionManager().shutdown();
Public charset, bomLength, boundary
Private stream
Private Sub Class_Initialize()
Me.charset = "UTF-8"
Me.bomLength = 3
Set stream = CreateObject("ADODB.Stream")
stream.Mode = 3
stream.Type = 1
stream.Open
Me.boundary = "----WebKitFormBoundaryomATwYZzgiwmJSgy" ' 随便写一个,够复杂就行
End Sub
Public Function GetFileData(path)
With CreateObject("ADODB.Stream")
.Type = 1
.Mode = 3
.Open
.LoadFromFile path
.Position = 0
GetFileData = .Read(-1)
.Close
End With
End Function
Public Function StringFromBytes(bytes, cSet)
With CreateObject("ADODB.Stream")
.Type = 1
.Open
.Write bytes
.Position = 0
.Type = 2
.charset = cSet
StringFromBytes = .ReadText()
.Close
End With
End Function
Public Function GetBytes(str, cSet, bomLength)
With CreateObject("ADODB.Stream")
.Type = 2
.Mode = 3
.charset = cSet
.Open
.Position = 0
.WriteText str
.Position = 0
.Type = 1
.Position = bomLength
GetBytes = .Read(-1)
.Close
End With
End Function
Private Sub WriteText(value)
stream.Write GetBytes(value, Me.charset, Me.bomLength)
End Sub
Public Function Size()
Size = stream.Size + Len(Me.boundary)
End Function
Public Function GetData()
stream.Position = 0
With CreateObject("ADODB.Stream")
.Type = 1
.Mode = 3
.Open
.Position = 0
.Write stream.Read(-1)
.Write GetBytes("--" & Me.boundary & "--", Me.charset, Me.bomLength)
.Position = 0
GetData = .Read(-1)
.Close
End With
stream.Position = stream.Size
End Function
Public Sub AddTextEntity(name, value)
WriteText "--" & Me.boundary & vbCrLf & "Content-Disposition: form-data; name=""" & name & """" & vbCrLf & vbCrLf & value & vbCrLf
End Sub
Public Sub AddFileEntity(name, filePath, mime)
WriteText "--" & Me.boundary & vbCrLf
WriteText "Content-Disposition: form-data; name=""" & name & """; filename=""" & filePath & """" & vbCrLf
WriteText "Content-Type: " & mime & vbCrLf & vbCrLf
stream.Write GetFileData(filePath)
WriteText vbCrLf
End Sub
Private Sub Class_Terminate()
stream.Close
Set stream = Nothing
End Sub
Private Sub Command1_Click()
Dim filePath, POST_URL, multidata As Multipart, xhr As Variant
filePath = "C:\16.jpg"
POST_URL = "http://file.chinadatapay.com/img/upload"
Set multidata = New Multipart
multidata.AddTextEntity "appkey", "2712f8c6f0ce9390b9edafc53e9d65b4"
multidata.AddFileEntity "data", filePath, "image/jpeg"
Set xhr = CreateObject("WinHttp.WinHttpRequest.5.1")
xhr.Open "POST", POST_URL, False
xhr.SetRequestHeader "Content-Length", multidata.Size()
xhr.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & multidata.boundary
xhr.Send multidata.GetData()
MsgBox xhr.ResponseText
End Sub
{
"code":"system_error",
"data":null,
"msg":"Request processing error",
"stacktrace":"Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
"success":true
}
若是运行正确,应该是这样的:
{
"code": "10000",
"data": "3710595ea2fb4f6699770197fa9b10ba",
"msg": "上次成功",
"stacktrace": null,
"success": true
}
Private Enum DataEnum
ResponseText = 1
ResponseBody = 2
End Enum
Private Sub cmdTestFile_Click()
On Error GoTo Err1
Dim fn As Integer
Dim fbuf() As Byte
Dim Upload_File As String
Upload_File = "d:\zhou.jpg"
'--------打开Adodb.stream 流读取二进制文件-------
fn = FreeFile()
ReDim fbuf(FileLen(Upload_File) - 1)
Open Upload_File For Binary As #fn
Get #fn, , fbuf
Close #fn
'这句是核心,可能就错在这里。
Dim strValue As String '提交图片后,返回字符串
strValue = PostDataFile("http://file.chinadatapay.com/img/upload", "&appkey=2712f8c6f0ce9390b9edafc53e9d65b4&data=" & ToHexString(fbuf), ResponseText)
Exit Sub
Err1:
MsgBox Err.Description
End Sub
Private Function ToHexString(ByRef buf() As Byte) As String
Dim I As Long, j As Long
Dim nlen As Long
Dim tmpHex As String
Dim HexStr As String
Dim tmpbuf() As Byte
nlen = (UBound(buf) + 1) * 2
ReDim tmpbuf(nlen - 1)
j = 0
For I = 0 To UBound(buf)
HexStr = Hex(buf(I))
If Len(HexStr) = 1 Then HexStr = "0" & HexStr
tmpbuf(j) = Asc(Mid(HexStr, 1, 1))
j = j + 1
tmpbuf(j) = Asc(Mid(HexStr, 2, 1))
j = j + 1
Next
ToHexString = StrConv(tmpbuf, vbUnicode)
End Function
Private Function PostDataFile(ByVal strurl As String, ByVal StrData As String, ByVal DataStic As DataEnum) As Variant
On Error GoTo Err:
Dim xmlhttp As Object
Dim DataS As String
Dim DataB() As Byte
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "POST", strurl, True
xmlhttp.SetRequestHeader "Content-Length", Len(PostDataFile)
xmlhttp.SetRequestHeader "Content-Type", "multipart/form-data"
xmlhttp.Send strurl & StrData
Do Until xmlhttp.ReadyState = 4
DoEvents
Loop
'-----------------------------函数返回
Select Case DataStic
Case ResponseText
'--------------------------------直接返回字符串
DataS = xmlhttp.ResponseText
PostDataFile = DataS
Case ResponseBody
'--------------------------------直接返回二进制
DataB = xmlhttp.ResponseBody
PostDataFile = DataB
Case ResponseBody + ResponseText
'---------------------------二进制转字符串[直接返回字串出现乱码时尝试]
DataS = BytesToStr(xmlhttp.ResponseBody)
PostDataFile = DataS
Case Else
'--------------------------------无效的返回
PostDataFile = ""
End Select
'------------------------------------释放空间
Set xmlhttp = Nothing
Exit Function
Err:
MsgBox Err.Description, "PostDataFile"
PostDataFile = ""
End Function
SUCCESS RESPONSE:
{
"code": "10000",
"data": "3710595ea2fb4f6699770197fa9b10ba",
"msg": "上次成功",
"stacktrace": null,
"success": true
}
而我的vb6_picload.rar 代码,也不知道怎么能让图片上传成功,所以代码就是错的,它运行后返回值如下:
{"code":"system_error","data":null,"msg":"Request processing error",
"stacktrace":"Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
"success":true}
[/quote]
我在6楼就是说,让你把VB6代码执行后的反馈结果贴出来。
看样子,应该是没有把图片的“具体数据”发出去,只是发了个“文件路径”的那些文本信息。
我也不清楚如何用这个对象,去实现“文件数据上传”。
目前我帮不上忙。 SUCCESS RESPONSE:
{
"code": "10000",
"data": "3710595ea2fb4f6699770197fa9b10ba",
"msg": "上次成功",
"stacktrace": null,
"success": true
}
而我的vb6_picload.rar 代码,也不知道怎么能让图片上传成功,所以代码就是错的,它运行后返回值如下:
{"code":"system_error","data":null,"msg":"Request processing error",
"stacktrace":"Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
"success":true}