HttpPost写正文以及请求参数
HttpPost write body as well as the request parameters
我正在使用 Apache 的 HttpPost。我正在尝试将文件上传到网络服务器,结果如下:
POST site/upload?uploadType=chunked&requestId={requestId} HTTP/1.1
Host:
Accept: application/xml
Authtoken:
Request Parameters
requestId The unique identifier for the current upload session.
Request Headers
Host The host name of the web server.
Accept The format of the response. Valid values are: application/xml or application/json.
Authtoken The authentication token received after successfully logging on.
FileEOF Specifies whether the end of file has reached with the current file chunk. Allowed values are 0 and 1. End of file means value 1.
Request Body
Include the contents of the file chunk in bytes.
我得到的是这样的:
HttpPost post = new HttpPost(url);
post.setHeader("Authtoken", params.get("token"));
String fileName = file.getName();
long offset = Long.parseLong(chunkOffset);
//...
post.setHeader("FileEOF", eof);
/*List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("requestId", requestId));
post.setEntity(new UrlEncodedFormEntity(postParameters));*/
fileInputStream.skip(offset);
fileInputStream.read(bytes);
post.setEntity(new ByteArrayEntity(bytes));
我在哪里写 HttpPost 的参数?
处理文件上传时,您将不得不使用 MultipartEntity
和 FileBody
。
示例:
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("someparameter1", new StringBody("Woody"));
entity.addPart("someparameter2", new StringBody("Woodpecker"));
File fileToSend = new File(filePath);
FileBody fileBody = new FileBody(fileToSend, "application/octet-stream");
entity.addPart("upload_file", fileBody);
httpPost.setEntity(entity);
使用 MultipartEntityBuilder
- 尝试以下操作:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
final File aFile = new File(fileName);
FileBody fileBody = new FileBody(file);
builder.addPart("file", fileBody);
builder.addTextBody("requestId", requestId);
final HttpEntity httpEntity = builder.build();
我正在使用 Apache 的 HttpPost。我正在尝试将文件上传到网络服务器,结果如下:
POST site/upload?uploadType=chunked&requestId={requestId} HTTP/1.1
Host:
Accept: application/xml
Authtoken:
Request Parameters
requestId The unique identifier for the current upload session.
Request Headers
Host The host name of the web server.
Accept The format of the response. Valid values are: application/xml or application/json.
Authtoken The authentication token received after successfully logging on.
FileEOF Specifies whether the end of file has reached with the current file chunk. Allowed values are 0 and 1. End of file means value 1.
Request Body
Include the contents of the file chunk in bytes.
我得到的是这样的:
HttpPost post = new HttpPost(url);
post.setHeader("Authtoken", params.get("token"));
String fileName = file.getName();
long offset = Long.parseLong(chunkOffset);
//...
post.setHeader("FileEOF", eof);
/*List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("requestId", requestId));
post.setEntity(new UrlEncodedFormEntity(postParameters));*/
fileInputStream.skip(offset);
fileInputStream.read(bytes);
post.setEntity(new ByteArrayEntity(bytes));
我在哪里写 HttpPost 的参数?
处理文件上传时,您将不得不使用 MultipartEntity
和 FileBody
。
示例:
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("someparameter1", new StringBody("Woody"));
entity.addPart("someparameter2", new StringBody("Woodpecker"));
File fileToSend = new File(filePath);
FileBody fileBody = new FileBody(fileToSend, "application/octet-stream");
entity.addPart("upload_file", fileBody);
httpPost.setEntity(entity);
使用 MultipartEntityBuilder
- 尝试以下操作:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
final File aFile = new File(fileName);
FileBody fileBody = new FileBody(file);
builder.addPart("file", fileBody);
builder.addTextBody("requestId", requestId);
final HttpEntity httpEntity = builder.build();