文件分片上传(二)
文件分片合并
public void testMergeFile(String newFile,String newfile1) throws IOException{
String chunkFileFolderPath = newFile;
File chunkFileFolder = new File(chunkFileFolderPath);
File[] files = chunkFileFolder.listFiles();
List<File> fileList = Arrays.asList(files);
File mergeFile = new File(newfile1);
boolean newFile = mergeFile.createNewFile();
RandomAcessFile raf_write = new RandomAccessFile(mergeFile,"rw");
byte[] b = new byte[1024];
for(File chunkFile:fileList){
RandomAccessFile raf_read = new RandomAccessFile(chunkFile,"r");
int len = -1;
while((len = raf_read.read(b))!=-1){
raf_write.write(b,0,len);
}
raf_read.close();
}
raf_write.close();
delFolder(newfile);
}
public static void delFolder(String folderPath){
try{
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete();
}catch(Exception e){
e.printStackTrace();
}
}
public static void delAllFile(String path){
boolean flag = false;
File file = new File(path);
if(!file.exists()){
flag=false;
}
if(!file.isDirectory()){
flag = false;
}
String[] tempList = file.list();
File temp = null;
for(int i = 0;i<tempList.length;i++){
if(path.endsWith(File.separator)){
temp = new File(path + tempList[i]);
}else{
temp = new File(path + File.separator + tempList[i]);
}
if(temp.isFile()){
temp.delete();
}
if(temp.isDirectory()){
delAllFile(path + "/"+ tempList[i]);
delFolder(path +"/"+ tempList[i]);
flag = true;
}
}
}