手记:复制,网络下载图片,复制文件

本文介绍了两种文件复制的方法:一种是使用Java NIO的管道模式进行高效复制;另一种是利用Apache Commons IO库中的FileUtils工具类实现。此外,还详细讲解了如何使用Java从网络上下载图片并保存到本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、通过管道模式复制文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Date;

public class CopyUtil {
public static void main(String[] args) {

File f1 = new File("D:\\test\\111");
try {
int a=0;
do {
forTransfer(f1);
System.out.println(a++);
} while (true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@SuppressWarnings("resource")
public static long forTransfer(File f1) throws IOException{
File f2 = new File("D:\\test\\"+System.currentTimeMillis());
        long time=new Date().getTime();
        int length=2097152;
        FileInputStream in=new FileInputStream(f1);
        FileOutputStream out=new FileOutputStream(f2);
        FileChannel inC=in.getChannel();
        FileChannel outC=out.getChannel();
        int i=0;
        while(true){
            if(inC.position()==inC.size()){
                inC.close();
                outC.close();
                return new Date().getTime()-time;
            }
            if((inC.size()-inC.position())<20971520)
                length=(int)(inC.size()-inC.position());
            else
                length=20971520;
            inC.transferTo(inC.position(),length,outC);
            inC.position(inC.position()+length);
            i++;
        }
    }
}

 

或者通过apache的FileUtils工具类复制一个文件

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

 

File pdfOld = new File("D:/zipDown/oldpdf.pdf");

File dest = new File("D:/zipDown/oldpdf.pdf");

// 判断文件父目录是否存在
if (!dest.getParentFile().exists()) {
    dest.getParentFile().mkdir();
}
try {
    FileUtils.copyFile(pdfOld, dest);
} catch (IOException e) {
    e.printStackTrace();
}

 

2、从网络下载图片

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class QyWxUtil {


/**
* @Deprecated 从网络下载图片到本地
* @param agentId
* @param urlStr
* @return
* @throws IOException
*/
public static String downLoadLogo(String agentId, String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

// 得到输入流
InputStream inputStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
// 获取自己数组
byte[] getData = bos.toByteArray();

// 文件保存位置
String savePath = "D:\\systemInfo\\agent_logo";
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + agentId + ".png");
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
return "/systemInfo/agent_logo/" + agentId + ".png";
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值