大文件 分块上传
1 验证文件分块是否存在
2 验证文件是否存在如果存在就复制到对应目录下
查询数据库中是否有对应Md5 的文件对象
将文件复制到目标路径,并创建新的文件对象
3 上传文件 分块上传
4 合并文件
获取目录中的所有文件分片
创建一个文件输出流,用于将文件分片内容写入合并后的文件。
获取文件输出流的通道,用于进行文件操作 (FileChannel)。
使用循环遍历文件分片列表,并且将每个文件分片的内容写入到合并后文件中。
// 输出流
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
FileChannel outChannel = fileOutputStream.getChannel();
// 合并
FileChannel inChannel;
for (File file : fileList) {
inChannel = new FileInputStream(file).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inChannel.close();
}
// 关闭流
fileOutputStream.close();
outChannel.close();
文件类型转换
doc docx ofd 转成pdf
documents4j
public static void word2pdf(String wordFilePath, String pdfFilePath) throws IOException {
try (
InputStream docxInputStream = new FileInputStream(wordFilePath);
OutputStream outputStream = new FileOutputStream(pdfFilePath)
) {
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outputStream)
.as(DocumentType.PDF).execute();
// 关闭
converter.shutDown();
} catch (FileNotFoundException e) {
log.error("文件未找到: " + e.toString());
} catch (IOException e) {
log.error("IO异常: " + e.toString());
} catch (Exception e) {
log.error("word转pdf失败: " + e.toString());
}
}
解压ZIP
SevenZip
public static void extractFile(String inputFilePath, String targetFilePath) {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(inputFilePath), "r");
IInArchive archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile))) {
int[] in = new int[archive.getNumberOfItems()];
for (int i = 0; i < in.length; i++) {
in[i] = i;
}
archive.extract(in, false, new ExtractCallback(archive, targetFilePath + "/"));
} catch (Exception e) {
System.out.println("解压失败::" + e);
}
}
下载远程文件到本地服务器
public static void downloadRemoteZip(String remoteZipUrl, String localFilePath) {
HttpURLConnection connection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL(remoteZipUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
fileOutputStream = new FileOutputStream(new File(localFilePath));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
} else {
System.out.println("Failed to download. HTTP response code: " + connection.getResponseCode());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
访问文件服务器资源
WebMvcConfigurer
http://192.168.0.52:8011/file-1/file/resource/networkDisk/320503/3205030008/cecp.txt
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resource/**").addResourceLocations(diskUrl);
WebMvcConfigurer.super.addResourceHandlers(registry);
}