网络文件转换

例如网络图片文件转换可以使用的如下:

package utils;
 
import com.xx.util.file.FileUtils;
import org.apache.commons.codec.binary.Base64;
 
import java.io.*;
import java.net.*;
 
/**
 * 文件工具
 * @author zmd
 */
public class FileUtil {
    /**
     * 网络文件URL转换成FileInputStream
     * @param netUrl
     * @return
     */
    public static FileInputStream netUrlToFileFileInputStream(String netUrl){
        FileInputStream fileInputStream = null;
        try {
            URL url = new URL(netUrl);
            HttpURLConnection  connection = (HttpURLConnection) url.openConnection();
 
            // 设置请求方法为HEAD,这样服务器只返回状态码,不会传输内容
            connection.connect();
            InputStream inputStream  = connection.getInputStream();
            fileInputStream = convertToFileInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileInputStream;
    }
 
    /**
     * inputStream 转换成 FileInputStream
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static FileInputStream convertToFileInputStream(InputStream inputStream) throws IOException {
        File tempFile = File.createTempFile("temp", ".jpeg");
        tempFile.deleteOnExit();
 
        try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
 
        return new FileInputStream(tempFile);
    }
 
    /**
     * 获取网络文件大小 kb
     * @param netUrl
     * @return
     */
    public static Long getImageSize(String netUrl){
        HttpURLConnection connection = null;
        try {
            URL url = new URL(netUrl);
            connection = (HttpURLConnection) url.openConnection();
 
            // 设置请求方法为HEAD,这样服务器只返回状态码,不会传输内容
            connection.setRequestMethod("HEAD");
            connection.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取图片字节长度,即大小
        assert connection != null;
        return connection.getContentLengthLong();
    }
 
    /**
     * 网络文件转换成File
     * @param netUrl
     * @return
     */
    public static File getFileByNetUrl(String netUrl){
        File tempFile = null;
        try {
            tempFile = File.createTempFile("temp", ".jpeg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (tempFile != null){
            tempFile.deleteOnExit();
 
            try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                FileInputStream inputStream = netUrlToFileFileInputStream(netUrl);
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(tempFile.getAbsolutePath());
        }
        return tempFile;
    }
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞流银河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值