相关疑难解决方法(0)

通过Http POST请求从Android上传文件和其他数据

这是从Android发布文件的简单方法.

String url = "http://yourserver.com/upload.php";
File file = new File("myfileuri");
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...

} catch (Exception e) {
    e.printStackTrace();
}  
Run Code Online (Sandbox Code Playgroud)

我想要做的是POST在我的请求中添加更多变量.我怎么做?在POST请求中上传纯字符串时,我们使用URLEncodedFormEntity.

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Run Code Online (Sandbox Code Playgroud)

而在上传文件时,我们使用InputStreamEntity.

另外,我如何专门上传此文件$_FILES['myfilename']

java post android http-post

15
推荐指数
1
解决办法
5万
查看次数

Android:上传文件,同时填写POST正文

我确实使用MultipartEntity将File发送到服务器,它在$_FILES超全局中正确显示

但我还需要填写POST正文以供阅读 php://stdin

我怎样才能做到这一点?

下面的当前片段:

ByteArrayOutputStream bos = new ByteArrayOutputStream(); // stream to hold image
bm.compress(CompressFormat.JPEG, 75, bos); //compress image
byte[] data = bos.toByteArray(); 
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("REMOTE ADDRESS");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE); // is this one causing trouble?
reqEntity.addPart("image", bab); // added image to request
// tried this with no luck
// reqEntity.addPart("", new StringBody("RAW DATA HERE")); 
postRequest.setEntity(reqEntity); // set the multipart entity …
Run Code Online (Sandbox Code Playgroud)

php java android file-upload multipartentity

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×2

java ×2

file-upload ×1

http-post ×1

multipartentity ×1

php ×1

post ×1