How to send parameters with a file in android

I upload an image in android. Currently, my code only downloads a file, but I also want to send some parameter. I try to follow

FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); //Sending data dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(globalUID); 

and on the server side I use php. this is how i try to get this parameter

 $param = $_POST["paramName"]; target_path1 = "./places_photos/" . $param; 

But my current code is loading the file, but it is not sending parameters. How can I send parameters and how to get them on the server side?

Update

Currently, the image is saved in the places_photos directory, which is mentioned in the $target_path1 variable. I want to save this image in the user directory, and this directory is called the user ID. But, unfortunately, I am not getting a server-side user id. How can I send the user ID to the server along with the file?

+6
source share
5 answers

After that, another End line should be added:

 dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(globalUID); dos.writeBytes(lineEnd); <-- add this 

Also make sure that you add the end of the multipart border (a line starting with '-'). In your case, add the following:

 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
0
source

Do you want to β€œmake” php to know your file name? if your answer is yes, so you can make some changes to your code.

(I assume that you created a buffer of maximum size and read the file and wrote it to the form)

The first change you do not need is to send paramName:

 //Sending data dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(globalUID); 

just do this:

 dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

and in your php change your code:

 $param = $_POST["paramName"]; 

to

 $param = basename( $_FILES['uploaded_file']['name']); 

you can check this article

0
source

Set a cookie with a user ID in it. On the server, you can get a cookie and check the user ID. (Although note that this is not safe and does not skip the userid parameter)

0
source

Your code is a bit confusing, double check your variables ... for example, your target_path1 does not have $ in front, check if any of the other segment variables returns any valid output.

If all else fails, you can rename the downloaded file β€” including the parameters in the file name and rename it again on the server side after extracting the parameters.

0
source

it is very simple ... 100% working solution

use UTF-8 to send the parameter to url. add this line

  // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL("http://your fileaddress.php***?yourphpsideparametername="+URLEncoder.encode(yourperameter, "UTF-8")*** ); 

and then add this line to the connection request

  conn.setRequestMethod("POST"); ***conn.setRequestProperty("Accept-Charset", "UTF=8");*** conn.setRequestProperty("Connection", "Keep-Alive"); 

add this line to php code

  $yourperameter =$_GET['yourparameter']; $file_path = "doc/".$yourperameter."/"; $file_path = $file_path . basename( $_FILES['uploaded_file']['name']); $title =$_FILES['title']; //$descr=$file_path . basename( $_FILES['uploaded_file']['desc']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { echo "success"; } else{ echo "fail"; } 
0
source

Source: https://habr.com/ru/post/918792/


All Articles