Android 为什么它在httpUrlConnection上返回内部服务器错误?
我正在开发一个可以向web服务器发送不同文件的应用程序。我还想发送大文件,要做到这一点,我需要将文件分块。但当我将文件发送到服务器时,不会上传任何内容。我不知道我发送文件的方式是否有错误,它在我的响应中给出了错误500(内部服务器错误)。我认为服务器不是问题所在,因为当我使用multiPartEntity上传文件时,它可以工作,但当我使用BufferedInputStream和DataOutputStream时,它不工作。请帮助我,告诉我我的代码有什么问题,为什么它不能发送我的文件。到目前为止,我得到的是:Android 为什么它在httpUrlConnection上返回内部服务器错误?,android,httpurlconnection,dataoutputstream,Android,Httpurlconnection,Dataoutputstream,我正在开发一个可以向web服务器发送不同文件的应用程序。我还想发送大文件,要做到这一点,我需要将文件分块。但当我将文件发送到服务器时,不会上传任何内容。我不知道我发送文件的方式是否有错误,它在我的响应中给出了错误500(内部服务器错误)。我认为服务器不是问题所在,因为当我使用multiPartEntity上传文件时,它可以工作,但当我使用BufferedInputStream和DataOutputStream时,它不工作。请帮助我,告诉我我的代码有什么问题,为什么它不能发送我的文件。到目前为止,
String samplefile = "storage/sdcard0/Pictures/Images/picture.jpg";
File mFile = new File(samplefile);
int mychunkSize = 2048 * 1024;
final long size = mFile.length();
final long chunks = size < mychunkSize? 1: (mFile.length() / mychunkSize);
int chunkId = 0;
try {
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(mFile));
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "-------------------------acebdf13572468";// random data
for (chunkId = 0; chunkId < chunks; chunkId++) {
URL url = new URL(urlString);
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(20000 /* milliseconds */);
conn.setConnectTimeout(20000 /* milliseconds */);
// 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");
String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP);
conn.setRequestProperty("Authorization", "Basic "+encoded);
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);
String param1 = ""+chunkId;
String param2 = ""+chunks;
String param3 = mFile.getName();
String param4 = samplefile;
// Send parameter #file
dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + param3 + "\"" + lineEnd); // filename is the Name of the File to be uploaded
dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
dos.writeBytes(lineEnd);
// Send parameter #chunks
dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes("Content-Length: " + param2.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(param2 + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
// Send parameter #name
dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes("Content-Length: " + param3.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(param3 + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
byte[] buffer = new byte[mychunkSize];
stream.read(buffer);
dos.write(buffer);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush();
dos.close();
}
} catch (Exception e) {
Log.e("Error Uploading Files", e.toString());
}
String samplefile=“storage/sdcard0/Pictures/Images/picture.jpg”;
File mFile=新文件(samplefile);
int mychunkSize=2048*1024;
最终长尺寸=mFile.length();
最终长块=大小
您可以使用代码。它是一个J2ME类,用于通过HTTP POST多部分请求处理文件上载
希望这有帮助。如果您愿意,可以使用此库。它很容易实现,并且可以完成您需要的所有工作
请看另一个类似的问题[1]:@Chronos先生Chronos如果你在那里,请检查我的代码thanks@NewDroidDev你是怎么解决这个问题的?我也有同样的问题。上载文件的第一个区块时,我收到500个内部服务器错误。我需要将我的文件分块发送。你认为我的代码有什么错?为什么它不向服务器发出请求?