Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

java - Post values and upload Image to php server in android

I am trying to upload image from android phone to php server with additional values,the method is post my php file look like this

if($_POST['val1']){
  if($_POST['val2']){
     if($_FILE['image']){
      ...... 
    }
  }
}else{
echo "Value not found";
}

I am doing is

URL url=new URL("http://www/......../myfile.php");
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");//Enable http POST
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+"****");
connection.setRequestProperty("uploaded_file", imagefilePath);
DataOutputStream ostream = new DataOutputStream( con.getOutputStream());
String res=("Content-Disposition: form-data; name="val1""+val1+"****"+
"Content-Disposition: form-data; name="val2""+val2+"****"
"Content-Disposition: form-data; name="image";filename="" + imagefilePath +"""+"****");
outputStream.writeBytes(res);

my actual problem is values are not posting so first if condition get false and else section is executed that is it give value not found please help me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have found solution of my problem. I am making mistake at line string res=(" .... "); I have not adding " " after every content.I have add it and my code working properly

link is very useful for me : http://wiki.forum.nokia.com/index.php/HTTP_Post_multipart_file_upload_in_Java_ME

Thank you sir for helping me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...