Java file chunking and merging

Source: Internet
Author: User

Use BASE64 encoding, and then truncate the string, supporting only small files
Small file filenames are random, so you want to write large file information and small file order to the first line of the small file
    <dependency>      <groupId>commons-codec</groupId>      <artifactid>commons-codec</ artifactid>      <version>1.6</version>    </dependency>

Importorg.apache.commons.codec.binary.Base64;ImportJava.io.*;ImportJava.util.HashMap;ImportJava.util.Map;/*** Created by LIWJ on 2016/8/23.*/ Public classFilesplit {/*** Split *@paramFileName File path *@paramnumber chunked file *@throwsException*/     Public voidSplitbynumber (String FileName,intNumber)throwsexception{File oldFile=NewFile (fileName); Bufferedinputstream in=NewBufferedinputstream (NewFileInputStream (oldFile)); String file=encode (in); intLength=file.length (); System.out.println ("String Length:" +length); intsize=length/Number ; intStart=0,end=size; Bufferedoutputstream out=NULL; File NewFile=NULL; String str_temp=NULL;  for(inti=0;i<number-1;i++) {str_temp=i+ "" +oldfile.getname () + "\ n"; Str_temp+=file.substring (start,end); NewFile=NewFile ("e:\\result\\" +randnumber () + ". File"); out=NewBufferedoutputstream (NewFileOutputStream (newFile));            Out.write (Str_temp.getbytes ());            Out.close (); Start+=size; End+=size; } str_temp=number-1+ "" +oldfile.getname () + "\ n"; Str_temp+=file.substring (start); NewFile=NewFile ("e:\\result\\" +randnumber () + ". File"); out=NewBufferedoutputstream (NewFileOutputStream (newFile));        Out.write (Str_temp.getbytes ());        Out.close (); return; }    /*** File Merging *@paramPath *@throwsException*/     Public voidMergebyname (String Path)throwsexception{File File=NewFile (path); File list[]=File.listfiles (); Map<String,String> map=NewHashmap<string, string>(); String NewFileName=NULL;  for(File f:list) {BufferedReader reader=NewBufferedReader (NewInputStreamReader (NewFileInputStream (f))); String Str_head=Reader.readline (); String ID=str_head.substring (0,str_head.indexof ("")); NewFileName=str_head.substring (Str_head.indexof ("") +1);            Map.put (Id,f.getabsolutepath ());        Reader.close (); } stringbuffer StringBuffer=NewStringBuffer ();  for(inti=0;i<list.length;i++) {File F=NewFile (Map.get (string.valueof (i))); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (NewFileInputStream (f)));            Reader.readline (); String Temp=NULL;  while((Temp=reader.readline ())! =NULL) {stringbuffer.append (temp);        } reader.close (); } Bufferedoutputstream out=NewBufferedoutputstream (NewFileOutputStream ("e:\\answer\\" +newfilename));        Out.write (Decode (stringbuffer.tostring ()));    Out.close (); }    /*** Code *@paramIn *@return     * @throwsIOException*/     PublicString encode (InputStream in)throwsioexception{byte[] data =New byte[In.available ()];        In.read (data); returnbase64.encodebase64string (data); }    /*** Decoding *@paramBase64str *@return     * @throwsIOException*/     Public byte[] Decode (String base64str)throwsioexception{returnbase64.decodebase64 (BASE64STR); }    /*** Random number *@return     */     PublicString Randnumber () {DoubleNumber=Math.random (); String Str=string.valueof (number); STR=str.replace (".", "" "); returnstr; }     Public Static voidMain (string[] args) {Try {            // chunking//new Filesplit (). Splitbynumber ("E:\\20160824134947.jpg", 10); //Merging//new Filesplit (). Mergebyname ("e:\\result\\");}Catch(Exception e) {e.printstacktrace (); }    }}

Java file chunking and merging

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.