如何将名称值数据添加到 Android HTTP 文件上传 POST 请求
我有一个 Android 照片上传脚本,如果我使用 $_GET 读取一些数据,上传效果很好,我想使用所有 $_POST。问题是我需要附加数据来执行发布的请求,以包含基本名称值对以及文件上传。如果我让服务器使用 $_GET 并在 url 字符串中添加额外的数据,我就可以让它工作,但我想使用 post.有没有办法在文件上传中对服务器可以使用 $_POST 读取的其他发布数据进行编码。这是函数,注意 URL 字符串,我试图获取变量 api_key、session_key 和要在 POST 请求中发送的方法。
public void uploadPhoto(FileInputStream fileInputStream, String sessionKey) throws IOException, ClientProtocolException {
URL connectURL = new URL(API_URL +"?api_key=" +API_KEY+ "&session_key=" + sessionKey + "&method=" + ApiMethods.UPLOAD_PHOTO); //server ignores this data
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
HttpURLConnection conn = (HttpURLConnection) connectURL.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);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ "file.png" + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1028;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int 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);
/*
* dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary +
* twoHyphens + lineEnd);
*/
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush();
// Log.d(TAG, " dos5: " + dos.toString());
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String s = b.toString();
dos.close();
}
编辑 我认为每个添加的参数都需要类似以下内容
dos.writeBytes("--" + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data;name=api_key=" + lineEnd + lineEnd + API_KEY);
dos.writeBytes(lineEnd);
dos.writeBytes("--" + boundary + "--" + lineEnd);
I have an android photo upload script that uploads fine if I use $_GET to read some data, I want to use all $_POST. The problem is I need to append data do the posted request to include basic name value pairs along with the file upload. I can get this to work if I have the server use $_GET and just add the extra data in the url string, but I want to use post. Is there a way to encode other post data in the file upload that the server can read using $_POST. Heres the function, notice the URL string where, I'm trying to get the variables api_key,session_key, and method to be sent in the POST request.
public void uploadPhoto(FileInputStream fileInputStream, String sessionKey) throws IOException, ClientProtocolException {
URL connectURL = new URL(API_URL +"?api_key=" +API_KEY+ "&session_key=" + sessionKey + "&method=" + ApiMethods.UPLOAD_PHOTO); //server ignores this data
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
HttpURLConnection conn = (HttpURLConnection) connectURL.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);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ "file.png" + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1028;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int 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);
/*
* dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary +
* twoHyphens + lineEnd);
*/
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush();
// Log.d(TAG, " dos5: " + dos.toString());
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String s = b.toString();
dos.close();
}
EDIT
I'm thinking something like the following is needed for each added param
dos.writeBytes("--" + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data;name=api_key=" + lineEnd + lineEnd + API_KEY);
dos.writeBytes(lineEnd);
dos.writeBytes("--" + boundary + "--" + lineEnd);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些现有的开放库支持 mime 编码,这可能会节省您一些时间。我在一些我为发布而编写的内容中使用了这里的代码:
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
您可以从这里获取 apache-mime4j 和 httpmime 库:
http://code.google.com/ p/osmdroid/source/browse/trunk/osmdroid-google-wrapper/lib/?r=769
There are some existing open libraries that support mime encoding which might save you some time. I used the code from here in some stuff I hacked up for posting:
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
You can get the apache-mime4j and httpmime libraries from here:
http://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-google-wrapper/lib/?r=769