我正在发送大于 2mb 的文件。为了能够发送这个文件,我需要把这个文件分成每块 2mb 大小的小块 [chunks] 并按块发送。我已经将文件分成较小的块并将其发送到 Web 服务器,但问题是当我已经收到响应时,文件大小越来越大,同时逐块发送文件。但是每个块的文件大小为 2mb 所以我'我想知道为什么会发生这种情况。预期的文件长度是我发送的特定块的长度,而块长度是已经发送的所有块的加法大小。到目前为止,我在这里:
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;
int ordername = 0;
int bytesRead = 0, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 2 * 1024 * 1024;
if (size > maxBufferSize){ //if file size is greater than 2mb
try {
splitFiles = ChunkFiles.splitFile(mFile);
String chunkedfiles="";
int i = 0;
for (i=0; i < splitFiles.size(); i++) {
chunkedfiles = splitFiles.get(i);
try {
File theChunkFiles = new File(chunkedfiles);
FileInputStream stream = new FileInputStream(theChunkFiles);
bytesAvailable = stream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
int bufferlength = buffer.length;
// read file and write it into form...
bytesRead = stream.read(buffer, 0, bufferSize);
ordername++;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "-------------------------acebdf13572468";// random data
String param3 = mFile.getName();
String param4 = samplefile;
String chunkFileName = theChunkFiles.getName();
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// 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);
conn.setChunkedStreamingMode(0);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + chunkFileName + "\"" + lineEnd); // filename is the Name of the File to be uploaded
dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
dos.writeBytes(lineEnd);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = stream.read(buffer, 0, bufferSize);
}
var = bufferSize;
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
// Send parameter #chunks
dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(chunkId + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
// Send parameter #name
dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes("ForFiddlerTest19VIDEO_20130711_151315_-2073548175.mp4" + lineEnd);
// send multipart form data necesssary after file data...
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
chunkId++;
is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
String s=b.toString();
Log.i("Response",s);
//stream.close();
is.close();
dos.close();
} catch (MalformedURLException ex) {
System.out.println("From CLIENT REQUEST:" + ex);
}catch (IOException ioe) {
System.out.println("From CLIENT REQUEST:" + ioe);
}catch (Exception e) {
Log.e("From CLIENT REQUEST:", e.toString());
}
}
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我得到的示例响应:
{"Filename":"ForFiddlerTest19VIDEO_20130711_151315_-2073548175.mp4","ChunkId":2,"ChunkLength":2097152,"FileLength":6291456}
文件长度增加而不是块长度