![](/img/trans.png)
[英]How to disable buffering in Java HttpURLConnection for multi-part form post?
[英]How to send multi-part form data in Java without parsing error?
我正在嘗試從Android應用程序向我的服務器API發送一些多部分表單數據。 這是我遇到解析問題的代碼。 我發布了三個值。 其中兩個是文本值,一個是文件。 在這三個中, 未正確解析填充文本值 。
我應該在代碼中解決什么才能正確發送所有值?
try
{
Log.e(Tag);
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// conn.setConnectTimeout();
// conn.setReadTimeout();
// 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=\"infill\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeInt(infill_);
dos.writeBytes(lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"material-type\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeInt(materialType);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
// Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
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);
// close streams
fileInputStream.close();
dos.flush();
Log.i("STL File Sent, Response code: "+String.valueOf(conn.getResponseCode()));
Log.i("STL File Sent, Response message : "+String.valueOf(conn.getResponseMessage()));
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 response=b.toString();
Log.i("STL Response + "+response);
dos.close();
// Set cost and time
getDataFromJsonResponse(response);
}
dos.writeInt(infill_);
這應該以文本而不是二進制形式發送。 HTML是文本協議。 完全沒有理由使用DataOutputStream
。
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
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);
}
您不需要所有這些。 只是:
byte[] buffer = new byte[8192];
int count;
while ((count = fileInputStream.read(buffer)) > 0)
{
dos.write(buffer, 0, count);
}
available()
幾乎沒有正確的用法,但這不是其中之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.