AJ_Report(6)

本文主要探讨2021年SC@SDUSC活动中,AJ_Report项目的FileUtil.java源码,该文件位于...\AJ_report\report-master\report-core\src\main\java\com\anjiplus\template\gaea\business\util。
摘要由CSDN通过智能技术生成

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值