跨境派

跨境派

跨境派,专注跨境行业新闻资讯、跨境电商知识分享!

当前位置:首页 > 卖家故事 > File、Base64、MultipartFile之间相互转换

File、Base64、MultipartFile之间相互转换

时间:2024-04-18 15:50:19 来源:网络cs 作者:胡椒 栏目:卖家故事 阅读:

标签: 相互  转换 
阅读本书更多章节>>>>

文件转base64字符串

/** * file转换为base64 * 注意:这里转换为base64后,是不包含文件head头的 */public static String fileToBase64(File file) {    Base64.Encoder base64 = Base64.getEncoder();    String base64Str = null;    try (FileInputStream fis = new FileInputStream(file);         ByteArrayOutputStream bos = new ByteArrayOutputStream()) {        byte[] b = new byte[1024];        int n;        while ((n = fis.read(b)) != -1) {            bos.write(b, 0, n);        }        base64Str = base64.encodeToString(bos.toByteArray());    } catch (IOException e) {        e.printStackTrace();    }    return base64Str;}

base64字符串转文件

/** * base64转化为file,并保存到指定路径下 */public static void base64ToFile(String base, String path) {    if (StringUtils.isBlank(base)) {        return;    }    Base64.Decoder decoder = Base64.getDecoder();    try (OutputStream out = new FileOutputStream(path)) {        byte[] bytes = decoder.decode(base);        for (int i = 0; i < bytes.length; ++i) {            if (bytes[i] < 0) {                bytes[i] += 256;            }        }        out.write(bytes);        out.flush();    } catch (IOException e) {        e.printStackTrace();    }}

base64转化为file流

/** * base64转化为file流 */public static File base64ToFile(String base64) {    if (base64 == null || "".equals(base64)) {        return null;    }    byte[] buff = Base64.getDecoder().decode(base64);    File file;    try {        file = File.createTempFile("tmp", null);    } catch (IOException e) {        e.printStackTrace();        return null;    }    try (FileOutputStream out = new FileOutputStream(file)) {        out.write(buff);    } catch (IOException e) {        e.printStackTrace();    }    return file;}

base64转MultipartFile

import org.springframework.web.multipart.MultipartFile;import java.io.*;import java.util.Base64;/** * @author 笑小枫 */public class Base64DecodedMultipartFile implements MultipartFile {    private final byte[] imgContent;    private final String header;    private final String fileName;    public Base64DecodedMultipartFile(byte[] imgContent, String header, String fileName) {        this.imgContent = imgContent;        this.header = header.split(";")[0];        this.fileName = fileName;    }    @Override    public String getName() {        return fileName + "." + header.split("/")[1];    }    @Override    public String getOriginalFilename() {        return fileName + "." + header.split("/")[1];    }    @Override    public String getContentType() {        return header.split(":")[1];    }    @Override    public boolean isEmpty() {        return imgContent == null || imgContent.length == 0;    }    @Override    public long getSize() {        return imgContent.length;    }    @Override    public byte[] getBytes() {        return imgContent;    }    @Override    public InputStream getInputStream() {        return new ByteArrayInputStream(imgContent);    }    @Override    public void transferTo(File dest) throws IOException, IllegalStateException {        try (FileOutputStream fos = new FileOutputStream(dest)) {            fos.write(imgContent);        }    }    /**     * base64转multipartFile     **/    public static MultipartFile base64Convert(String base64, String header, String fileName) {        Base64.Decoder decoder = Base64.getDecoder();        byte[] b = decoder.decode(base64);        //取索引为1的元素进行处理        for (int i = 0; i < b.length; ++i) {            if (b[i] < 0) {                b[i] += 256;            }        }        // 处理过后的数据通过Base64DecodeMultipartFile转换为MultipartFile对象        return new Base64DecodedMultipartFile(b, header, fileName);    }}

byte数组转MultiPartFile

POM导入
<dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpclient</artifactId>    <version>4.5.5</version></dependency><dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-test</artifactId>      <version>RELEASE</version></dependency>
代码部分
byte[] bytes = message.getPacket();InputStream inputStream = new ByteArrayInputStream(bytes);MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);

MultipartFile转化为byte数组

byte[] bytes= file.getBytes();
阅读本书更多章节>>>>

本文链接:https://www.kjpai.cn/gushi/2024-04-18/160085.html,文章来源:网络cs,作者:胡椒,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

文章评论