この機能の使用に問題があります。length(); を取得します。アップロードする必要があるファイルの次のエラーが表示されます ->
予想よりも常にバイト数が多い
Exception : expected 589715 bytes but received 589840
java.io.IOException: expected 589715 bytes but received 589840
at libcore.net.http.FixedLengthOutputStream.write(FixedLengthOutputStream.java:39)
at java.io.DataOutputStream.write(DataOutputStream.java:98)
at com.androidexample.uploadtoserver.UploadToServer.uploadFile(UploadToServer.java:152)
at com.androidexample.uploadtoserver.UploadToServer$1.run(UploadToServer.java:62)
at java.lang.Thread.run(Thread.java:856)
これを使用してファイルのサイズを取得しました
int fixedLength = (int) fileInputStream.getChannel().size();
int total = (int) sourceFile.length();
動作する setFixedLengthStreamingMode(int) の例を正しく投稿できますか? この方法では問題しか見えません
ここに完全なコードがあります。はい、ファイルまたはいくつかのファイルを転送するためのものであり、それぞれが15 Mbを少しまたは超える可能性があり、一部の古いデバイスでメモリ不足になることがあります。
public int uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
runOnUiThread(new Runnable() {
public void run() {
/* messageText.setText("Source File not exist :"
+uploadFilePath + "" + uploadFileName);*/
}
});
return 0;
} else {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
int fixedLength = (int) fileInputStream.getChannel().size();
URL url = new URL(upLoadServerUri);
//int total = (int) sourceFile.length();
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setFixedLengthStreamingMode(fixedLength);
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
conn.setRequestProperty("mail", MAIL);
conn.setRequestProperty("OS", "1");
conn.setRequestProperty("LANG", "ES");
//conn.setChunkedStreamingMode(maxBufferSize);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + 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);
while (bytesRead > 0) {
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);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
// String serverResponseMessage = conn.getResponseMessage();
// Log.i("uploadFile", "HTTP Response is : "
// + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200) {
runOnUiThread(new Runnable() {
public void run() {
/*messageText.setText(msg);
Toast.makeText(UploadToServer.this, "File Upload Complete.",
Toast.LENGTH_SHORT).show();*/
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
}
}
}