java-根据URL获取网络文件并转换成Base64编码工具类

根据URL获取网络文件并转换成Base64编码工具类


import com.google.common.base.Strings;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;

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


/**
 * @author zhujialin
 * @date 2020/7/28
 */
public class ConverUtils {
    /**
     * 根据文件url获取文件并转换为base64编码
     *
     * @param srcUrl 文件地址
     * @param requestMethod 请求方式("GET","POST")
     * @return 文件base64编码
     */
    public static String netSourceToBase64(String srcUrl,String requestMethod) {
        ByteArrayOutputStream outPut = new ByteArrayOutputStream();
        byte[] data = new byte[1024 * 8];
        try {
            // 创建URL
            URL url = new URL(srcUrl);
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(requestMethod);
            conn.setConnectTimeout(10 * 1000);

            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                //连接失败/链接失效/文件不存在
                return null;
            }
            InputStream inStream = conn.getInputStream();
            int len = -1;
            while (-1 != (len = inStream.read(data))) {
                outPut.write(data, 0, len);
            }
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outPut.toByteArray());
    }

    /**
     * 把base64转化文件流
     *
     * @param base64 base64
     * @return byte[] 文件流
     */
    public static byte[] decryptByBase64(String base64) {

        if (Strings.isNullOrEmpty(base64)) {
            return null;
        }
        return Base64.decodeBase64(base64.substring(base64.indexOf(",") + 1));
    }

    /**
     * inputStream转化为byte[]数组
     * @param input InputStream
     * @return byte[]
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java工具类,提供了将文件、图片、PDF、URL转换为Base64编码的方法,以及将Base64编码转换为文件的方法: ```java import java.io.*; import java.net.URL; import java.util.Base64; public class PdfUtil { /** * 将文件转换为Base64编码 * @param file 文件对象 * @return Base64编码字符串 */ public static String fileToBase64(File file) { String base64 = null; InputStream in = null; try { in = new FileInputStream(file); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * 将图片转换为Base64编码 * @param imgUrl 图片地址 * @return Base64编码字符串 */ public static String imageToBase64(String imgUrl) { String base64 = null; InputStream in = null; try { URL url = new URL(imgUrl); in = url.openStream(); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * 将PDF文件转换为Base64编码 * @param file PDF文件对象 * @return Base64编码字符串 */ public static String pdfToBase64(File file) { String base64 = null; InputStream in = null; try { in = new FileInputStream(file); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * 将Base64编码保存为文件 * @param base64 Base64编码字符串 * @param filePath 文件保存路径 * @return 文件对象 */ public static File base64ToFile(String base64, String filePath) { File file = null; OutputStream out = null; try { byte[] bytes = Base64.getDecoder().decode(base64); file = new File(filePath); out = new FileOutputStream(file); out.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return file; } } ``` 使用方法如下: ```java // 将文件转换为Base64编码 File file = new File("example.pdf"); String base64 = PdfUtil.fileToBase64(file); // 将图片转换为Base64编码 String imgUrl = "https://example.com/image.png"; String base64 = PdfUtil.imageToBase64(imgUrl); // 将PDF文件转换为Base64编码 File pdfFile = new File("example.pdf"); String base64 = PdfUtil.pdfToBase64(pdfFile); // 将Base64编码保存为文件 File file = PdfUtil.base64ToFile(base64, "example.pdf"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值