我用过:httmine 4.1 jar上传了很多图片我的代码:
private void postFile() {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url[type]);
Bitmap pic1 = ImageFile.decodeFile(currentPath, 180);
currentPath=ConstValue.MY_TEMP_DIR + "/search.jpg";
ImageFile.writePhotoJpg(pic1, currentPath);
File file = new File(currentPath);
// File file=new File("/mnt/sdcard/aaaa.txt");
FileBody cbFile = new FileBody(file, "image/jpeg");
//FileBody cbFile = new FileBody(file);
MultipartEntity mpEntity = new MultipartEntity();
try {
mpEntity.addPart("fileToUpload", cbFile);
//mpEntity.addPart( "fileToUpload",new FileBody(file));
mpEntity.addPart("sys", new StringBody("android"));
mpEntity.addPart("sex", new StringBody(sex));
mpEntity.addPart("ver", new StringBody(ConstValue.VERSION_NAME));
mpEntity.addPart("uid", new StringBody(ConstValue.UID));
mpEntity.addPart("page", new StringBody("1"));
mpEntity.addPart("offset", new StringBody("30"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
httppost.setEntity(mpEntity);
//Log.d("log", httppost.getRequestLine().toString());
//Log.d("Log", httppost.getEntity().toString());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
Log.i("log","111111111111111");
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Log.i("log","2222222222222");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Log.i("log","33333333333");
}
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
try {
final String response_str = EntityUtils.toString(resEntity);
Log.i("log",""+response_str);
if (!response_str.contains("@@@@")) {
ConstValue.COUPLE_MATCH_RESAULT = ApplyXmlUtil.parseMastersXML(response_str);
} else {
errerCode=0;
}
// Log.d("log", response_str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
}
httpclient.getConnectionManager().shutdown();
}
my php code:
$tmp_name = md5($uid.time().rand(0,20).microtime()).".jpg";
$tmp_path = WWW_DIR.'/upload/tmp/andriodimg/'.$tmp_name;
$tmp_path2 = WWW_DIR.'/upload/tmp/andriodimg/'.$tmp_name.'_2.jpg';
$MAX_SIZE =4000000; //1M
$FILE_MIMES = array('image/jpeg','image/jpg','image/pjpeg');
$xmlstr ='';
if (!empty($_FILES['fileToUpload']['tmp_name']) && $_FILES['fileToUpload']['tmp_name'] != 'none') {
if(!in_array($_FILES['fileToUpload']['type'],$FILE_MIMES)){
$xmlstr='<code>2</code>';
$xmlstr = "<".'?xml version="1.0" encoding="UTF-8"'."?>\r\n<body>\r\n".$xmlstr."</body>";
@header("Content-Type: text/xml; charset=utf-8");
echo $xmlstr;
exit;
}
if($_FILES['fileToUpload']['size'] > $MAX_SIZE){
$xmlstr='<code>6</code>';
$xmlstr = "<".'?xml version="1.0" encoding="UTF-8"'."?>\r\n<body>\r\n".$xmlstr."</body>";
@header("Content-Type: text/xml; charset=utf-8");
echo $xmlstr;
exit;
}
$data = @file_get_contents($_FILES['fileToUpload']['tmp_name']);
if(empty($data)){
$xmlstr='<code>3</code>';
$xmlstr = "<".'?xml version="1.0" encoding="UTF-8"'."?>\r\n<body>\r\n".$xmlstr."</body>";
@header("Content-Type: text/xml; charset=utf-8");
echo $xmlstr;
exit;
}
file_put_contents($tmp_path, $data);
list($width, $height, $type, $attr) = @getimagesize($tmp_path);
if($type!=2){
$xmlstr='<code>2</code>';
$xmlstr = "<".'?xml version="1.0" encoding="UTF-8"'."?>\r\n<body>\r\n".$xmlstr."</body>";
@header("Content-Type: text/xml; charset=utf-8");
echo $xmlstr;
exit;
}
if($height/$width > 5){
@unlink($tmp_path);
$xmlstr='<code>5</code>';
$xmlstr = "<".'?xml version="1.0" encoding="UTF-8"'."?>\r\n<body>\r\n".$xmlstr."</body>";
@header("Content-Type: text/xml; charset=utf-8");
echo $xmlstr;
exit;
}
$xmlstr .='<imgurl1>'.$website_default.$webdir."upload/tmp/andriodimg/".$tmp_name.'</imgurl1>';
}
有时代码很好,有时无法上传代码,你能给我一些建议帮我找错吗