Java HTTPURLConnection缓冲区错误
我试图上传一张照片并将JSON字符串传递给服务器 我的猜测是,我计算的内容长度不正确。但是我不确定我的数学问题在哪里 有人能帮我追查我的错误吗 我收到以下错误: E/PhotoUpload:错误:预期为14591字节,但收到15183字节 java.net.ProtocolException:预期为14591字节,但在收到15183字节 com.android.okhttp.internal.http.HttpConnection$fixedlingthsink.write(HttpConnection.java:311) 位于com.android.okio.RealBufferedSink.flush(RealBufferedSink.java:154) 在 com.android.okio.RealBufferedSink$1.flush(RealBufferedSink.java:137) 在java.io.FilterOutputStream.flush(FilterOutputStream.java:88)中 flush(DataOutputStream.java:63)位于 com.mycompany.myapp.TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:381) 在 TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:273) E/PhotoUpload:错误:预期为14591字节,但收到15183字节 java.net.ProtocolException:预期为14591字节,但在收到15183字节 com.android.okhttp.internal.http.HttpConnection$fixedlingthsink.write(HttpConnection.java:311) 位于com.android.okio.RealBufferedSink.flush(RealBufferedSink.java:154) 在 com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:769) 在 com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405) 在 com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349) 在 com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:203) 在 com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 在 com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) 在 com.mycompany.myapp.TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:393) 在 TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:273) 在android.os.AsyncTask$2.call(AsyncTask.java:292)Java HTTPURLConnection缓冲区错误,java,android,android-asynctask,httpurlconnection,Java,Android,Android Asynctask,Httpurlconnection,我试图上传一张照片并将JSON字符串传递给服务器 我的猜测是,我计算的内容长度不正确。但是我不确定我的数学问题在哪里 有人能帮我追查我的错误吗 我收到以下错误: E/PhotoUpload:错误:预期为14591字节,但收到15183字节 java.net.ProtocolException:预期为14591字节,但在收到15183字节 com.android.okhttp.internal.http.HttpConnection$fixedlingthsink.write(HttpConnec
公共类BGUploadImage扩展异步任务{
int maxBufferSize=1*256*1024;
int headerSize=89;
字符串URL字符串=”http://www.mywebsite.com/someapi";
int fileSize=0;
int infoSize=0;
私人互联网i;
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的void onProgressUpdate(整型…值){
}
@凌驾
受保护的整数doInBackground(字符串…文件){
整数ret=0;
HttpURLConnection conn=null;
DataOutputStream dos=null;
DataInputStream inStream=null;
字符串exsistingFileName=文件[0];
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“------------------------------------14737809831466499882746641449”;
字符串json=JSONCalls.uploadImageJSON(thisCallKey);
int字节读取,字节可用,缓冲区大小;
字节[]缓冲区;
试一试{
Log.i(“照片上传”,“开始上传文件:“+文件[0]);
FileInputStream FileInputStream=newfileinputstream(新文件(exsistingFileName));
URL=新URL(URL字符串);
conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(真);
连接设置输出(真);
conn.SETUSECHACHES(假);
fileSize=fileInputStream.available();
infoSize=fileSize+headerSize+file[0].length();
conn.setFixedLengthStreamingMode(信息大小);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
dos=新的DataOutputStream(conn.getOutputStream());
//JSON部分
写字节(两个连字符+边界+行结束);
writeBytes(“内容处置:表单数据;name='params'”+lineEnd);
dos.writeBytes(json);
dos.writeBytes(lineEnd);
//媒体部分
写字节(两个连字符+边界+行结束);
writeBytes(“内容处置:表单数据;名称='UploadedFile';文件名='”+文件[0]+“'”+行结束);
dos.writeBytes(lineEnd);
//创建最大大小的缓冲区
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
buffer=新字节[bufferSize];
//读取文件并将其写入表单。。。
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
int-bytesSent=0;
而(字节读取>0){
如果(字节数>0){
int pg=(bytesSent*100)/infoSize;
出版进度(pg);
}
bytesSent+=缓冲区大小;
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
//发送文件数据后所需的多部分表单数据。。。
dos.writeBytes(lineEnd);
写字节(两个连字符+边界+两个连字符+行结束);
//合流
fileInputStream.close();
dos.flush();
dos.close();
}捕获(格式错误){
public class BGUploadImage extends AsyncTask<String, Integer, Integer> {
int maxBufferSize = 1 * 256 * 1024;
int headerSize = 89;
String urlString = "http://www.mywebsite.com/someapi";
int fileSize = 0;
int infoSize = 0;
private int i;
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {
}
@Override
protected Integer doInBackground(String... file) {
Integer ret = 0;
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName = file[0];
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "---------------------------14737809831466499882746641449";
String json = JSONCalls.uploadImageJSON(thisCallKey);
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
try {
Log.i("PhotoUpload", "Start uploading file: " + file[0]);
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
fileSize = fileInputStream.available();
infoSize = fileSize + headerSize + file[0].length();
conn.setFixedLengthStreamingMode(infoSize);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
//JSON PART
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name='params'" + lineEnd);
dos.writeBytes(json);
dos.writeBytes(lineEnd);
//MEDIA PART
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name = 'UploadedFile'; filename = '" + file[0] + "' " + 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);
int bytesSent = 0;
while (bytesRead > 0) {
if (bytesSent > 0) {
int pg = (bytesSent * 100) / infoSize;
publishProgress(pg);
}
bytesSent += bufferSize;
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
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
Log.e("PhotoUpload", "error: " + ex.getMessage(), ex);
ret = 1;
} catch (IOException ioe) {
Log.e("PhotoUpload", "error: " + ioe.getMessage(), ioe);
ret = 1;
}
try {
inStream = new DataInputStream(conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
Log.i("PhotoUpload", "Server Response" + str);
}
inStream.close();
} catch (IOException ioex) {
Log.e("PhotoUpload", "error: " + ioex.getMessage(), ioex);
ret = 1;
}
return ret;
}
@Override
protected void onPostExecute(Integer r) {
}
}
}
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
int bytesSent = 0;
while (bytesRead > 0) {
if (bytesSent > 0) {
int pg = (bytesSent * 100) / infoSize;
publishProgress(pg);
}
bytesSent += bufferSize;
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 = in.read(buffer)) > 0)
{
out,write(buffer, 0, count);
}