【发布时间】:2012-06-25 03:33:08
【问题描述】:
我在 android 中上传图片。目前我的代码只上传文件,但我也想发送一些参数。我正在尝试关注
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);
在服务器端,我使用的是 php。这是我尝试获取该参数的方式
$param = $_POST["paramName"];
target_path1 = "./places_photos/" . $param;
但我当前的代码确实上传文件但它不发送参数。如何发送参数以及如何在服务器端获取它们?
更新
目前,图像保存在places_photos 变量中提到的$target_path1 目录中。我想要的是将该图像保存在用户的目录中,并且该目录被命名为用户 ID。但不幸的是,我没有在服务器端获得用户 ID。如何将用户 ID 连同文件一起发送到服务器?
【问题讨论】: