我发送的文件大于2mb。能够发送此文件我需要将此文件分成更小的块,每块大小为2mb [块]并按块发送。我已经将文件分成更小的块并将其发送到Web服务器,但问题是当我已经获得响应时,文件大小越来越大,同时通过chunk发送文件块。但是每个块的文件大小是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}
文件长度递增而不是块长度
答案 0 :(得分:0)
"ChunkLength":2097152,"FileLength":6291456
块长度是常量 - 服务器返回的2 MB和文件长度正在递增,因为服务器可能将块连接到单个文件中 - 将每个新块附加到最终文件的末尾,因此每个请求都会获得新的,响应的文件长度会更大而块长度是恒定的。