2021SC@SDUSC
源码地址:…\AJ_report\report-master\report-core\src\main\java\com\anjiplus\template\gaea\business\util\FileUtil.java
源码内容:
package com.anjiplus.template.gaea.business.util;
import com.anji.plus.gaea.code.ResponseCode;
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.zip.*;
/**
* Created by raodeming on 2021/8/23.
*/
@Slf4j
public class FileUtil {
//链接url下载图片
public static void downloadPicture(String urlPath, String path) {
URL url = null;
try {
url = new URL(urlPath);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(path);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
log.info("链接下载图片:{},临时路径:{}", urlPath, path);
} catch (IOException e) {
log.error("根据链接下载失败", e);
throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage());
}
}
/**
* 复制文件
*
* @param source
* @param dest
* @throws IOException
*/
public static void copyFileUsingFileChannels(File source, File dest) {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} catch (IOException e) {
log.error("复制文件失败", e);
throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.getMessage());
} finally {
try {
inputChannel.close();
outputChannel.close();
} catch (IOException e) {
log.error("", e);
throw BusinessExceptionBuilder.build(ResponseCode.FAIL_CODE, e.