- 前言
- 一、废话少说,直接上代码
- 二.测试用例
- 总结
前言
我需要把一个大文件上传到网盘上,由于网盘禁止上传大于4G的文件,所以我想通过Java程序来把大文件分割为小文件后,再上传,等需要的时候,通过程序把文件进行合并即可。
一、废话少说,直接上代码代码如下:
package com.xm.file;
import java.io.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class FileSplitAndCombine {
private static int THOUSAND_TWENTY_FOUR= 1024; // one thousand and twenty-four
public static void splitFile(String sourcePath, String desPath, String newName) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(sourcePath)))) {
for (int i = 0; ; i++) {
String filePath = desPath + File.separatorChar +newName+"_" +i;
if (singleSplitFile(bis, filePath, THOUSAND_TWENTY_FOUR)) break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean singleSplitFile(BufferedInputStream bis, String filePath,int partitionSize) throws IOException {
int length = partitionSize * THOUSAND_TWENTY_FOUR * THOUSAND_TWENTY_FOUR;
byte[] buffer = new byte[THOUSAND_TWENTY_FOUR];
int tempLength;
System.out.println(nowTime() + "----->>>> begin split " + filePath);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)))) {
while ((tempLength = bis.read(buffer)) != -1) {
bos.write(buffer, 0, tempLength);
if ((length = length - tempLength) <= 0) break;
}
}
System.out.println(nowTime() + "<<<<----- yet finish split" + filePath);
return tempLength < 0;
}
public static void combineFile(String sourcePath, String desPath) {
List sortedFileNames = sortFilesName(desPath);
if (sortedFileNames == null) return;
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(sourcePath)))) {
for (String sortedFileName : sortedFileNames) {
copyFile(bos, desPath + File.separatorChar + sortedFileName);
}
System.out.println("all finish...");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyFile(BufferedOutputStream bos, String des) throws IOException {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(des)))) {
System.out.println(nowTime()+ "------>>>>"+des + "开始合并");
byte[] bytes = new byte[1024];
int length;
while ((length = bis.read(bytes)) != -1) {
bos.write(bytes, 0, length);
}
System.out.println(nowTime()+ "<<<<------"+des + "合并完成");
}
}
private static List sortFilesName(String fileDir) {
File file = new File(fileDir);
File[] files = file.listFiles();
if (null == files || files.length == 0) {
return null;
}
return Arrays.stream(files).filter(File::isFile).map(File::getName)
.sorted(String::compareTo).collect(Collectors.toList());
}
private static String nowTime(){
return new Date().toLocaleString();
}
}
二.测试用例
代码如下:
package com.xm;
import com.xm.file.FileSplitAndCombine;
public class FileSplitAndCombineTest {
public static void main(String[] args) {
//文件分割
splitTest();
//文件合并
combineTest();
}
private static void splitTest() {
String sourcePath = "E:\fileSplitTest\source.zip";
String desPath = "E:\fileSplitTest";
String newName = "split";
FileSplitAndCombine.splitFile(sourcePath, desPath, newName);
}
private static void combineTest() {
String sourcePath = "E:\fileSplitTest\combine.zip";
String desPath = "E:\fileSplitTest";
FileSplitAndCombine.combineFile(sourcePath, desPath);
}
}
总结
代码比较简单,希望能够帮助到有需要的人,如果您有任何疑问都可以通过评论联系我。



