使用HttpPut使文件上载请求无效

时间:2014-04-27 17:41:31

标签: android apache upload laravel-4 put

我正在使用此代码向服务器发送文件上传请求(后端的PHP),

public String makeFilePutUploadRequest(String url,File file,List<BasicNameValuePair> basicNameValuePairs) throws ClientProtocolException, IOException {
    String responseString = null;

        HttpPut httput = new HttpPut(url);
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        //
        if (basicNameValuePairs.size() > 0) {
            for (BasicNameValuePair nameValuePair : basicNameValuePairs) {
                multipartEntityBuilder.addTextBody(nameValuePair.getName(), nameValuePair.getValue());
            }
        }
        // file
        multipartEntityBuilder.addPart("upfile", new FileBody(file));
        httput.setEntity(multipartEntityBuilder.build());
        HttpResponse response = httpClient.execute(httput);
        responseString = EntityUtils.toString(response.getEntity());

    //      Log.i(LOG_TAG, "response string recieved::" + responseString);
    return responseString;
}

但在服务器端收到请求但未设置参数。

我在后端使用Laravel 4.1来处理这个请求,如果我使用Input::get('param')来获取值,我就会变为空。并Input::hasFile('upfile')返回false

1 个答案:

答案 0 :(得分:1)

使用PUT方法发送params我这样做:

    HttpPut httput = new HttpPut();


    StringBuffer params = new StringBuffer();
    for (BasicNameValuePair nameValuePair : basicNameValuePairs) {
        params.append(nameValuePair.getName())
              .append("=")
              .append(nameValuePair.getValue())
              .append("&");
    }

    StringBuffer urlWithParams = new StringBuffer(ServiceURL);
    urlWithParams.append("?").append(params.toString().substring(0,params.length()-1));

    httput.setURI(URI.create(urlWithParams.toString()));
    httput.addHeader("Content-Type", "application/json"); 

...