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