简体   繁体   English

从Java发送POST和FILE数据

[英]Sending POST and FILE data from Java

Currently, I have PHP form that accepts POST data as well as a FILE ($_POST / $_FILE). 目前,我有PHP表单接受POST数据以及FILE($ _POST / $ _FILE)。

How would I use this form within Java? 我如何在Java中使用此表单? (Android app) (Android应用)

Here's how you can send $_POST through Java (specifically in Android). 以下是通过Java发送$_POST (特别是在Android中)。 It shouldn't be too hard convert to $_FILE . 它不应该太难转换为$_FILE Everything from here is a bonus. 这里的一切都是奖金。

public void sendPostData(String url, String text) {

    // Setup a HTTP client, HttpPost (that contains data you wanna send) and
    // a HttpResponse that gonna catch a response.
    DefaultHttpClient postClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response;

    try {   

       // Make a List. Increase the size as you wish.
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

       // Add your form name and a text that belongs to the actual form.
       nameValuePairs.add(new BasicNameValuePair("your_form_name", text));

       // Set the entity of your HttpPost.
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       // Execute your request against the given url and catch the response.
       response = postClient.execute(httpPost);

       // Status code 200 == successfully posted data.
       if(response.getStatusLine().getStatusCode() == 200) {
          // Do something. Maybe you wanna get your response
          // and see what it contains, with HttpEntity class? 
       }

    } catch (Exception e) {
    }

}   

Sounds like you need the magic of a org.apache.http.entity.mime.MultipartEntity since you are mixing form fields with file fields. 听起来你需要org.apache.http.entity.mime.MultipartEntity的魔力,因为你将表单字段与文件字段混合在一起。

http://hc.apache.org/httpcomponents-client-ga/apidocs/org/apache/http/entity/mime/MultipartEntity.html http://hc.apache.org/httpcomponents-client-ga/apidocs/org/apache/http/entity/mime/MultipartEntity.html

File fileObject = ...;
MultiPartEntity entity = new MultiPartEntity();
entity.addPart("exampleField", new StringBody("exampleValue")); // probably need to URL encode Strings
entity.addPart("exampleFile", new FileBody(fileObject));
httpPost.setEntity(entity);

Download and include the Apache httpmime-4.0.1.jar and apache-mime4j-0.6.jar. 下载并包含Apache httpmime-4.0.1.jar和apache-mime4j-0.6.jar。 After that, sending files via post request is pretty easy. 之后,通过邮寄请求发送文件非常简单。

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://url.to.your/html-form.php");
try {
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            entity.addPart("file", new FileBody(new File("/sdcard/my_file_to_upload.jpg")));

            httpPost.setEntity(entity);

            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            Log.e(this.getClass().getSimpleName(), response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM