我需要将一些大文件发布到服务器。我能够上传 < 3 MB 的文件,但是当我上传 > 3 MB 的文件时,它显示了内部服务器异常和服务器响应代码“500”。最初,我认为这是服务器端问题,但我可以通过 iOS 应用程序(通过使用相同的 url)发布大文件,我需要在 android 中实现它。
这是我的代码——
public String postMediaDataToServer(String evidenceId, File file) {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "----WebKitFormBoundarysNDKBMUFqqvzYWGh";
Date date = new Date();
String timestamp = dateFormat.format(date);
String fileName = file.getName();
if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
fileName = "image_" + timestamp + ".jpg";
} else if (fileName.endsWith(".wav")) {
fileName = "audio_" + timestamp + ".wav";
} else if (fileName.endsWith(".mp4")) {
fileName = "video_" + timestamp + ".mp4";
} else if (fileName.endsWith(".3gp")) {
fileName = "video_" + timestamp + ".3gp";
}
String response = "error";
Log.e("Image filename", fileName);
Log.e("url", ApplicationData.getBaseUrl()
+ "Media");
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
// DataInputStream inputStream = null;
String pathToOurFile = fileName;
String urlServer = ApplicationData.getBaseUrl()
+ "Media";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1000*1024);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("X-Ecordia-Software", "ecordiApp");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ boundary);
List<Cookie> cookies = ApplicationData.getCookieStore()
.getCookies();
if (cookies.size() > 0)
connection.setRequestProperty("Cookie", cookies.get(0).getName() + "="
+ cookies.get(0).getValue());
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(lineEnd + twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"EvidenceIdentifier\""
+ lineEnd + lineEnd);
outputStream.writeBytes(evidenceId + lineEnd);
outputStream.writeBytes(lineEnd + twoHyphens + boundary + lineEnd);
if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
outputStream.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
+ fileName + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: image/jpeg" + lineEnd + lineEnd);
} else if (fileName.endsWith(".mp4")) {
outputStream.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
+ fileName + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: video/mp4" + lineEnd + lineEnd);
} else if (fileName.endsWith(".wav")) {
outputStream.writeBytes("Content-Disposition: form-data; name=\"File0\"; filename=\""
+ fileName + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: audio/wav" + lineEnd + lineEnd);
}
outputStream.writeBytes(lineEnd + twoHyphens + boundary + twoHyphens
+ lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.e("Image length", bytesAvailable + "");
try {
while (bytesRead > 0) {
try {
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
} catch (Exception e) {
e.printStackTrace();
response = "error";
return response;
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("Server Response Code ", "" + serverResponseCode);
Log.i("Server Response Message", serverResponseMessage);
if (serverResponseCode == 200) {
response = "true";
}
fileInputStream.close();
outputStream.flush();
outputStream.close();
outputStream = null;
} catch (Exception ex) {
// Exception handling
response = "error";
Log.e("Send file Exception", ex.getMessage() + "");
ex.printStackTrace();
}
return response;
如何解决这个问题?