我有这种形式,上传图像到使用HttpPost服务器。我这样做是使用HTML形式,但它不与Android的HttpPost工作。它的反应:你没有选择要上传的文件。似乎文件字段尚未发送
< HTML>
< HEAD>
<冠军>上传表格及LT; /标题>
< /头>
<身体GT;
<形式的行动=http://192.168.0.151/index.php/upload/uploadFile方法=后接收字符集=utf-8ENCTYPE =的multipart / form-data的>
<输入类型=文件名称=userfile的大小=20/>
< BR />< BR />
<输入类型=提交值=上传/>
< /形式GT;
< /身体GT;
< / HTML>
客户端code
最后HttpClient的客户端=新DefaultHttpClient();
最后HttpPost后=新HttpPost(HTTP://+主机+/上传/ uploadFile);
post.addHeader(加密类型,多部分/表单数据);
最后的名单,其中,的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(userfile的,/mnt/sdcard/Download/Photos/icecream.png));
尝试 {
最后HttpEntity请求=新UrlEn codedFormEntity(namevaluepairs中);
post.setEntity(要求);
最后的Htt presponse响应= client.execute(后);
//得到响应体。
最后弦乐responseBody = EntityUtils.toString(response.getEntity());
的System.out.println(响应体:+ responseBody);
}赶上(最终UnsupportedEncodingException E){
e.printStackTrace();
}赶上(最终ClientProtocolException E){
e.printStackTrace();
}赶上(最终IOException异常E){
e.printStackTrace();
}
Controller类
类上传扩展是CI_Controller {
功能__construct()
{
父:: __结构();
$这个 - >负载>帮手(阵列('形式','链接'));
}
公共职能showForm(){
$这个 - >负载>查看('upload_form');
}
公共职能uploadFile(){
//标题为XML允许输出。
标题(内容类型:文本/ xml的,字符集= UTF-8);
$配置['upload_path'] ='./uploads/';
$配置['allowed_types'] ='GIF | JPG | PNG;
$配置['MAX_SIZE'] ='2048';
$配置['max_width'] ='2048';
$配置['max_height'] ='2048';
$这个 - >负载>库('上传',$配置);
如果($这个 - >!upload-> do_upload())
{
//获得错误信息。
$错误= $这个 - > upload->的display_errors();
// prepare模板。
$ XMLDATA =的file_get_contents(TEMPLATE_XML_DIRupload_result.xml。);
$ XMLDATA = str_replace函数({} IS_SUCCESSFUL,0,$ XMLDATA);
$ XMLDATA = str_replace函数({}错误,$错误,$ XMLDATA);
回声$ XMLDATA;
}
其他
{
$数据=阵列('upload_data'=> $这个 - > upload->数据());
// prepare模板。
$ XMLDATA =的file_get_contents(TEMPLATE_XML_DIRupload_result.xml。);
$ XMLDATA = str_replace函数({} IS_SUCCESSFUL,1,$ XMLDATA);
$ XMLDATA = str_replace函数({}错误,,$ XMLDATA);
回声$ XMLDATA;
}
}
}
............
更新解决方案(它适合我的code)。请记住,httpmime-4.2.1.jar添加到您的构建路径。
公共无效后(最后的字符串URL,最终的名单,其中,的NameValuePair> namevaluepairs中){
最终的HttpClient的HttpClient =新DefaultHttpClient();
最后的HttpContext localContext =新BasicHttpContext();
最后HttpPost httpPost =新HttpPost(URL);
尝试 {
最后MultipartEntity实体=新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
对于(INT指数= 0;指数< nameValuePairs.size();指数++){
如果(nameValuePairs.get(指数).getName()。equalsIgnoreCase(userfile的)){
//如果密钥等于userfile的,我们使用FileBody传输数据
entity.addPart(nameValuePairs.get(指数).getName(),新FileBody(新文件(nameValuePairs.get(指数).getValue())));
} 其他 {
//普通字符串数据
entity.addPart(nameValuePairs.get(指数).getName(),新StringBody(nameValuePairs.get(指数).getValue()));
}
}
httpPost.setEntity(实体);
最后的Htt presponse响应= httpClient.execute(httpPost,localContext);
最后弦乐responseBody = EntityUtils.toString(response.getEntity());
的System.out.println(响应体:+ responseBody);
}赶上(最终IOException异常E){
e.printStackTrace();
}
}
解决方案
我只是碰到了与图像张贴这个答案。 这是否帮助?
I have this form for uploading images to the server using HttpPost. I did it using a HTML form but it doesn't work with Android's HttpPost. It responses: "You did not select a file to upload". It seems that file field hasn't been sent.
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<form action="http://192.168.0.151/index.php/upload/uploadFile" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Client code
final HttpClient client = new DefaultHttpClient();
final HttpPost post = new HttpPost("http://" + hostName + "/upload/uploadFile");
post.addHeader("enctype", "multipart/form-data");
final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userfile", "/mnt/sdcard/Download/Photos/icecream.png"));
try {
final HttpEntity request = new UrlEncodedFormEntity(nameValuePairs);
post.setEntity(request);
final HttpResponse response = client.execute(post);
// Get response body.
final String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("RESPONSE BODY: " + responseBody);
} catch (final UnsupportedEncodingException e) {
e.printStackTrace();
} catch (final ClientProtocolException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
Controller class
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function showForm() {
$this->load->view('upload_form');
}
public function uploadFile() {
// Header for xml outputing.
header('Content-type: text/xml; charset=utf-8');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
// Get error message.
$error = $this->upload->display_errors();
// Prepare template.
$xmlData = file_get_contents(TEMPLATE_XML_DIR . "upload_result.xml");
$xmlData = str_replace("{IS_SUCCESSFUL}", 0, $xmlData);
$xmlData = str_replace("{ERROR}", $error, $xmlData);
echo $xmlData;
}
else
{
$data = array('upload_data' => $this->upload->data());
// Prepare template.
$xmlData = file_get_contents(TEMPLATE_XML_DIR . "upload_result.xml");
$xmlData = str_replace("{IS_SUCCESSFUL}", 1, $xmlData);
$xmlData = str_replace("{ERROR}", "", $xmlData);
echo $xmlData;
}
}
}
.................
Update solution (it works for my code). Remember to add "httpmime-4.2.1.jar" to your Build Path.
public void post(final String url, final List<NameValuePair> nameValuePairs) {
final HttpClient httpClient = new DefaultHttpClient();
final HttpContext localContext = new BasicHttpContext();
final HttpPost httpPost = new HttpPost(url);
try {
final MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("userfile")) {
// If the key equals to "userfile", we use FileBody to transfer the data
entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
final HttpResponse response = httpClient.execute(httpPost, localContext);
final String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("RESPONSE BODY: " + responseBody);
} catch (final IOException e) {
e.printStackTrace();
}
}
解决方案
I just ran into this answer relating to image posting. Does this help?