Android multipart upload file, video

Source: Internet
Author: User
Tags base64 http post

When uploading images from Android, you can also compress,

But when uploading the video, there is no good compression method.

Then I thought of two ways to solve it.

1 is the traditional HTTP post upload, the content is file flow, but do not know how much endurance, the advantage is that this method has matured, file flow, the transfer of Base64 processing, memory overflow situation is much less.

2 is the Android side, multipart upload file.


The second is to fundamentally address the memory overflow approach

Its core idea is

1 The same file, multipart upload to use the same name, the background to receive the Base64 data stitching.

2 since it is segmented, pass a paragraph to pass the next paragraph, and complete stitching.


Each segment is passed, proving that the file is passed, and this is done on the Android side.

When I do, refer to article http://blog.csdn.net/defonds/article/details/8575893


This is written in Java. The flaw is that small files are not included in the file.

Below I enclose my complete code, uses is the WebService way to upload, the code may not be concise, hoped that everybody comments

/**
* Multipart upload video
*/
private void Sectionupload () {


File File = new file (Videopath);
int fileLen = 0;
try {
FileInputStream fis = new FileInputStream (file);


FileLen = Fis.available ();
} catch (FileNotFoundException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
} catch (IOException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}


31457280 30M
if (FileLen >= 31457280) {
Tishidialog (weiguiapiactivity.this, "Upload file cannot be greater than 30M");
Return
}


Asyncshowdialog.dialogshow (this);


New Thread () {
public void Run () {
try {
int blockindex = 1;
int blocknumber = 1;
String ext = Videopath
. substring (Videopath.lastindexof (".")); /


After you have a suffix, you have to save a picture name.
String fileName = tools.getcurrentutctime_no_space () + ext;
FileInputStream FIS = null;
Bytearrayoutputstream BAOs = null;
Now is going to get a path
File File = new file (Videopath);
FIS = new FileInputStream (file);
Randomaccessfile RAF = new Randomaccessfile (file, "R");//responsible for reading data


int fileLen = fis.available ();


Video format, size
Blocknumber = (int) (filelen/blocksize);
10704745
if (fileLen% blockSize! = 0) {
Blocknumber = Blocknumber + 1;
}
while (Blockindex <= blocknumber) {


Bufferedinputstream bis = new Bufferedinputstream (FIS);
Bis.read (buffer, Byteoffset, ByteCount)
BAOs = new Bytearrayoutputstream ();


byte[] buffer_1 = new byte[1024 * 1024];
byte[] buffer = new byte[1024];


String uploadbuffer = null;
Fis.read (buffer, byteoffset, byteCount);
Write is 1kb 1,kb


if (Blockindex = = 1) {
int n = 0;
Long readlength = 0;//The number of read bytes recorded
if (FileLen >= blockSize) {
while (Readlength <= blockSize-1024) {///most bytes read here


n = raf.read (buffer, 0, 1024);
Readlength + = 1024;
Baos.write (buffer, 0, N);
}
if (Readlength < blockSize) {//The remaining less than 1024
Bytes are read here
n = raf.read (buffer, 0, (int) (BlockSize-
Readlength));
n = raf.read (buffer, 0, 1024);
Baos.write (buffer, 0, N);
}


} else {
while (Readlength <= fileLen-1024) {///most bytes read here


n = raf.read (buffer, 0, 1024);
Readlength + = 1024;
Baos.write (buffer, 0, N);
}
}


} else if (Blockindex < Blocknumber) {//is neither the first block nor the last piece
Raf.seek (BlockSize * (blockIndex-1));//skip before [block number * Fixed size
] bytes
Fis.skip (BlockSize * (blockIndex-1));
int n = 0;
Long readlength = 0;//The number of read bytes recorded
while (Readlength <= blockSize-1024) {///most bytes read here
n = raf.read (buffer, 0, 1024);
Readlength + = 1024;
Baos.write (buffer, 0, N);
}
if (readlength <= blockSize) {//The remaining less than 1024 bytes are read here
n = raf.read (buffer, 0,
(int) (Blocksize-readlength));
Baos.write (buffer, 0, N);
}
} else {//last piece
Raf.seek (BlockSize * (blockIndex-1));//skip before [block number * Fixed size
] bytes
int n = 0;
while ((n = raf.read (buffer, 0, 1024))! =-1) {
Baos.write (buffer, 0, N);
}
}


while ((count = fis.read (buffer)) >= 0) {
Baos.write (buffer, 0, count);
// }
try {
if (BAOs! = null) {
Baos.flush ();
Baos.close ();
}
if (FIS! = null) {


Fis.close ();
}


} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Uploadbuffer = new String (Base64.encode (BAOs
. Tobytearray ()));


Baos.flush ();


Does it need to write a loop to upload


try {
Actually, it's a memory leak.
Uploadbuffer = new String (Base64.encode (BAOs
. Tobytearray ()));
} catch (OutOfMemoryError e) {
TODO auto-generated Catch block
Toast.maketext (Weiguiapiactivity.this, "Memory Leak", 0)
. Show ();
E.printstacktrace ();
}


This piece of memory leaks easily.


String test = Baos.tobytearray (). toString ();


list<wsparam> params = new arraylist<wsparam> ();


int size = Uploadbuffer.length ();


Params.add (New Wsparam ("filename", filename));
Params.add (New Wsparam ("Data", Uploadbuffer));


Params.add (New Wsparam ("pwd", mydata.md5pwd));


String strresult = null;
try {
connection2php connection = new connection2php ();
Object result = null;
try {
result = Connection.callwebservice (
Mydata.webserviceurl, "Uploadvideo",
params);


} catch (Exception e) {


E.printstacktrace ();
}


if (result = = null) {
Tools.log ("Server not Open");
strresult = "";
Upload errors, re-upload.
Continue
} else {
strresult = Result.tostring ();
Commonjsonparser parser = new Commonjsonparser ();
Map map = Parser.parse (strresult);
String videoname = map.get ("filename")
. toString ();


A value that takes only the first paragraph
if (Blockindex = = 1) {
Sb_video = sb_video.append (Videoname + "#");
}


}


} catch (Exception e) {
E.printstacktrace ();
}


blockindex++;


}


//
Message msg = Handler.obtainmessage ();
Msg.what = 1;
Bundle bundle = new bundle ();
Bundle.putstring ("Result", "upload success");
Msg.setdata (bundle);
Handler.sendmessage (msg);





} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally {


}
};


}.start ();
}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.