如何在Android中随文件发送post数据?
在过去的几天里,我一直在尝试在Android上发布一个多部分表单,但无法发送字符串数据和文件。我使用下面的代码上传我的文件,它工作得很好。但是,在向请求中添加字符串数据时,这是不够的 假设我需要将如何在Android中随文件发送post数据?,android,forms,post,file-upload,Android,Forms,Post,File Upload,在过去的几天里,我一直在尝试在Android上发布一个多部分表单,但无法发送字符串数据和文件。我使用下面的代码上传我的文件,它工作得很好。但是,在向请求中添加字符串数据时,这是不够的 假设我需要将profile\u id=1234&text=这是我的文本&global=0附加到请求中。我该怎么做 String fileName = photoPath; HttpURLConnection conn = null; DataOutputStream dos = null; String line
profile\u id=1234&text=这是我的文本&global=0
附加到请求中。我该怎么做
String fileName = photoPath;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(new File(photoPath));
URL url = new URL(Util.URL_POST_FEED);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
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("file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
+ fileName + "\"" + 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);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(c, "File Upload Complete.",
Toast.LENGTH_SHORT).show();
}
});
}
InputStream stream = conn.getInputStream();
InputStreamReader isReader = new InputStreamReader(stream);
//put output stream into a string
BufferedReader br = new BufferedReader(isReader);
Log.d("Read", br.readLine());
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(c, "MalformedURLException",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(c, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}
检查。希望这会有帮助
它使用一个自定义的
多端口性
类,该类手动创建通过连接发送的请求主体。检查此@JaiSoni,谢谢,它工作得很好。你介意把它放在一个答案里,这样我就可以接受它了吗?可能是