Java 如何使用multipart将文件上传到Android中的PHP服务器,向HTTP POST方法添加请求值?
所以,我试图将一个文件上传到我的PHP服务器。我在网上找到了一些有用的代码,但是我还需要包括一些值,比如用户身份验证以及文件应该上传到服务器上的什么位置。我对HTTP通信比较陌生,我在下面找到的代码使用了我以前从未听说过的术语/代码,Java 如何使用multipart将文件上传到Android中的PHP服务器,向HTTP POST方法添加请求值?,java,php,android,http,Java,Php,Android,Http,所以,我试图将一个文件上传到我的PHP服务器。我在网上找到了一些有用的代码,但是我还需要包括一些值,比如用户身份验证以及文件应该上传到服务器上的什么位置。我对HTTP通信比较陌生,我在下面找到的代码使用了我以前从未听说过的术语/代码,多部分/表单数据内容类型和内容配置。因此,如果有人能告诉我如何包含我需要的值,提供一个完全不同的方法,或者像我五岁那样向我解释这三个术语,我将不胜感激。这是我的密码: public static void upload(String path, String sec
多部分/表单数据
内容类型
和内容配置
。因此,如果有人能告诉我如何包含我需要的值,提供一个完全不同的方法,或者像我五岁那样向我解释这三个术语,我将不胜感激。这是我的密码:
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/'或其他东西)这样的值,以及实际的带有$_文件的文件['uploadedfile']字符串urlToSendRequest=”https://example.net";
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("<input>test</input>", "UTF-8");
entity.setContentType("application/xml");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, context);
Reader r = new InputStreamReader(response.getEntity().getContent());
字符串targetDomain=“example.net”;
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpHost targetHost=新的HttpHost(targetDomain,80,“http”);
HttpPost HttpPost=新的HttpPost(urlToSendRequest);
//确保服务器知道我们将接受哪种响应
//addHeader(“Accept”、“text/xml”);
//还要确保告诉服务器我们正在发送什么类型的内容
addHeader(“内容类型”、“应用程序/xml”);
StringEntity=新StringEntity(“测试”、“UTF-8”);
entity.setContentType(“应用程序/xml”);
httpPost.setEntity(实体);
HttpResponse response=httpClient.execute(httpPost,context);
Reader r=新的InputStreamReader(response.getEntity().getContent());
你能解释一下吗?比如,我会如何将这与我想做的事情联系起来?我没有上传任何XML。(除非是所选的文件类型)