[英]How to send chunk files in web api in android?
我发送的文件大于2mb。 为了能够发送此文件,我需要将此文件分成小块,每块2 mb [块]的大小,然后按块发送。 我已经将文件分割成较小的块并将其发送到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}
文件长度正在增加,而不是块长度
"ChunkLength":2097152,"FileLength":6291456
块的长度是恒定的-2 MB并且服务器返回的文件长度在增加,因为服务器可能将块连接到单个文件中-在最终文件的末尾附加每个新块,因此对于每个请求,您都会得到新的更大的文件长度作为响应,而块长度是恒定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.