用于从多个部件接收图像和文本的php
我正在尝试通过MultipartEntity上传一个图像和一些文本。 我可以上传和接收图像,但当我尝试添加Stringbody时,我似乎无法接收它。 这是我的android代码用于从多个部件接收图像和文本的php,php,android,multipartentity,Php,Android,Multipartentity,我正在尝试通过MultipartEntity上传一个图像和一些文本。 我可以上传和接收图像,但当我尝试添加Stringbody时,我似乎无法接收它。 这是我的android代码 imports ETC... public void oncreate(){ ..... nameValuePairs.add(new BasicNameValuePair("image", exsistingFileName)); nameValuePairs.add(new BasicNameValuePair("
imports ETC...
public void oncreate(){
.....
nameValuePairs.add(new BasicNameValuePair("image", exsistingFileName));
nameValuePairs.add(new BasicNameValuePair("title", "title"));
}
public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
System.out.println("post - if");
// If the key equals to "image", we use FileBody to transfer the data
entity.addPart( nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
System.out.println("post - else");
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
System.out.println("post - done" + entity);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
} catch (IOException e) {
e.printStackTrace();
}
}
导入等。。。
public void oncreate(){
.....
添加(新的BasicNameValuePair(“图像”,exsistingFileName));
添加(新的BasicNameValuePair(“标题”、“标题”);
}
public void post(字符串url,列表nameValuePairs){
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
试一试{
MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
对于(int index=0;index
还有我的php:
<?php
$uploads_dir = 'uploads/';
$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"]["title"];
move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>
我一直在思考关于多方当事人的其他问题,但似乎找不到答案。我尝试只发送Stringbody,但也没有成功。我认为问题出在服务器端(在PHP中),但欢迎提出任何建议
这是我在这里的第一个问题-请随意评论形式和清晰度:-)试着这样做
ByteArrayBody bab1 = bab11;
HttpClient httpClient = new DefaultHttpClient();
httpPost = new HttpPost("link.php?api_name=api");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// this is for String
try {
reqEntity.addPart("udid", new StringBody(UDID));
}
catch (Exception e)
{
}
// this is for image upload
try
{
reqEntity.addPart("file1", bab1);
} catch (Exception e)
{
}
// this is for video upload
try {
if (stPath1 != null) {
Log.e("path 1", stPath1);
Log.v("stDocType1", "video");
File file = new File(stPath1);
FileBody bin = new FileBody(file);
reqEntity.addPart("file1", bin);
}
} catch (Exception e) {
}
httpPost.setEntity(reqEntity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpClient.execute(httpPost, responseHandler);
ByteArrayBody bab1=bab11;
HttpClient HttpClient=新的DefaultHttpClient();
httpPost=newhttppost(“link.php?api_name=api”);
多方实体REQUENTITY=新多方实体(
HttpMultipartMode.BROWSER_兼容);
//这是给弦的
试一试{
请求实体添加部分(“udid”,新的StringBody(udid));
}
捕获(例外e)
{
}
//这是图像上传
尝试
{
请求实体添加部分(“文件1”,bab1);
}捕获(例外e)
{
}
//这是用来上传视频的
试试{
if(stPath1!=null){
Log.e(“路径1”,stPath1);
Log.v(“stDocType1”、“视频”);
文件文件=新文件(stPath1);
FileBody bin=新的FileBody(文件);
请求实体添加部分(“文件1”,bin);
}
}捕获(例外e){
}
httpPost.setEntity(reqEntity);
ResponseHandler ResponseHandler=新BasicResponseHandler();
response=httpClient.execute(httpPost,responseHandler);
问题在于我使用了php。
当您收到Stringbody时,只有一个参数(与filebody相反)。因此,我删除了$uploadtitle=$\u文件[“title”][“title”]中的第二个参数;它成功了
<?php
$uploads_dir = 'uploads/';
$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"];
move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>
如果你有同样的问题,我希望这会有所帮助 您好,请回答我们的问题。关于它的几个问题:-)1。bap1是通过ByteArrayoutputStream转换的图像吗?2.您从哪里获得stDocType1?我刚刚更新了第一个问题的答案和好友是的,stPath1是一个视频路径使用函数,用于将Uri转换为PathHi Rstar抱歉响应太晚。不得不做一些其他的工作:-)我仍然有接收文件的问题。他们似乎上传,我能够收到图像。但不是文本,也不是两者。