如何使用multipart将REQUEST值添加到HTTP POST方法以将文件上传到Android中的PHP服务器?

所以,我正在尝试将文件上传到我的PHP服务器。 我发现一些在线代码可以使用,但我还需要包含用户身份validation等内容的值以及服务器上应该上传文件的位置。 我对HTTP通信相对较新,我在下面找到的代码使用了我之前从未听说过的术语/代码,具体是multipart/form-data Content-TypeContent-Disposition 。 所以,如果有人能告诉我如何包含我需要的价值,完全提供一种不同的方法,或者只是向我解释这三个术语,就像我五岁那样,我会非常感激。 这是我的代码:

 public static void upload(String path, String section, Context c){ Log.i("path", path); HttpURLConnection conn = null; DataOutputStream dos = null; DataInputStream inStream = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; String responseFromServer = ""; String urlString = c.getString(R.string.server) + "upload.php"; try { // ------------------ CLIENT REQUEST FileInputStream fileInputStream = new FileInputStream(new File( path)); // open a URL connection to the Servlet URL url = new URL(urlString); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); // Allow Inputs conn.setDoInput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + path + "\"" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close streams Log.e("Debug", "File is written"); fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { Log.e("Debug", "error: " + ex.getMessage(), ex); } catch (IOException ioe) { Log.e("Debug", "error: " + ioe.getMessage(), ioe); } // ------------------ read the SERVER RESPONSE try { inStream = new DataInputStream(conn.getInputStream()); String str; while ((str = inStream.readLine()) != null) { Log.e("Debug", "Server Response " + str); } inStream.close(); } catch (IOException ioex) { Log.e("Debug", "error: " + ioex.getMessage(), ioex); } } 

**编辑:为了更清楚一点,我希望在我的PHP脚本中我可以访问$ _REQUEST [‘path’](可能=’/ documents /’或其他东西)等值,以及带有$ _FILES [‘uploadedfile’]的实际文件

 String urlToSendRequest = "https://example.net"; String targetDomain = "example.net"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpHost targetHost = new HttpHost(targetDomain, 80, "http"); HttpPost httpPost = new HttpPost(urlToSendRequest); // Make sure the server knows what kind of a response we will accept // httpPost.addHeader("Accept", "text/xml"); // Also be sure to tell the server what kind of content we are sending httpPost.addHeader("Content-Type", "application/xml"); StringEntity entity = new StringEntity("test", "UTF-8"); entity.setContentType("application/xml"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, context); Reader r = new InputStreamReader(response.getEntity().getContent());