每当我使用带有上传文件的请求主体的POST发出请求时,我都会遇到这种奇怪的情况,我的标题部分除了contentType之外还包含任何内容,每当我尝试读取响应时它都会给我EOFException,下面是我的代码任何帮助将不胜感激。
public static Response executeFWUploadRequest(Context context, String fileName, String version,
String checksum, File payload, int retry) {
if (CommonUtils.isConnectedToWiFi(context)) {
HttpURLConnection urlConnection = null;
String boundary = "+++++";
String LINE_FEED = "\n";
try {
URL url = new URL(CommonUtils.UPLOAD_URL);
CommonUtils.writeLog("HTTP Client REQUEST URL = " + url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(METHOD_POST);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty(Tag.CONTENT_TYPE_TAG, "multipart/form-data; boundary=" + boundary);
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setConnectTimeout(TimeOut);
urlConnection.setReadTimeout(TimeOut);
OutputStream outputStream = urlConnection.getOutputStream();
BufferedOutputStream dos = new BufferedOutputStream(outputStream);
dos.flush();
String partition = "--" + boundary;
dos.write(LINE_FEED.getBytes());
dos.write(partition.getBytes());
dos.write(LINE_FEED.getBytes());
String disp3 = "Content-Disposition: form-data; name=\"" + Tag.FILE_LENGTH_FIELD + "\"" + LINE_FEED + LINE_FEED + "value = \"" + payload.length() + "\"";
dos.write(disp3.getBytes());
dos.write(LINE_FEED.getBytes());
dos.write(partition.getBytes());
dos.write(LINE_FEED.getBytes());
String dis1 = "Content-Disposition: form-data; name=\"" + Tag.VERSION_TAG + "\"" + LINE_FEED + LINE_FEED + "value = \"" + version + "\"";
dos.write(dis1.getBytes());
dos.write(LINE_FEED.getBytes());
dos.write(partition.getBytes());
dos.write(LINE_FEED.getBytes());
String disp2 = "Content-Disposition: form-data; name=\"" + Tag.CHECKSUM_TAG + "\"" + LINE_FEED + LINE_FEED + "value = \"" + checksum + "\"";
dos.write(disp2.getBytes());
dos.write(LINE_FEED.getBytes());
dos.write(partition.getBytes());
dos.write(LINE_FEED.getBytes());
String disp4 = "Content-Disposition: form-data; name=\"file \"; " + Tag.FILE_NAME_TAG + "=\"" + payload.getName() + "\"";
dos.write(disp4.getBytes());
dos.write(LINE_FEED.getBytes());
String disp5 = Tag.CONTENT_TYPE_TAG + ": " + Tag.STREAM_CONTENT_TYPE_VALUE;
dos.write(disp5.getBytes());
dos.write(LINE_FEED.getBytes());
dos.write(LINE_FEED.getBytes());
FileInputStream inputStream = new FileInputStream(payload);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
dos.write(buffer, 0, bytesRead);
}
dos.write(LINE_FEED.getBytes());
dos.write((partition + "--").getBytes());
dos.flush();
dos.close();
//tried with and with out below 2 lines
outputStream.close();
urlConnection.connect();
//This line gives error its method to read response with its patch
return readData(urlConnection,0);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
} else {
CommonUtils.writeLog("HTTP Client : Wifi connection is not available");
}
return null;
}
private static final int MAX_RETRY = 5;
private static Response readData(HttpURLConnection urlConnection, int retry) {
Response objResponse = null;
try {
objResponse = new Response();
String content;
if (retry < MAX_RETRY) {
objResponse.setResponseCode(urlConnection.getResponseCode());
if (objResponse.getResponseCode() == 200) {
content = convertInputStreamToString(new BufferedInputStream(urlConnection
.getInputStream()));
objResponse.setResponseContent(content);
} else {
content = convertInputStreamToString(new BufferedInputStream(urlConnection
.getErrorStream()));
objResponse.setResponseContent(content);
}
}
} catch (EOFException eof) {
eof.printStackTrace();
readData(urlConnection, retry + 1);
} catch (IOException e) {
e.printStackTrace();
}
return objResponse;
}
这里返回的Response类型是自定义POJO类,它存储响应代码和响应内容。