首页 新闻 会员 周边 捐助

文件流转MultipartFile

0
[待解决问题]

文件流 byte数组 base64加密 如何转换成MultipartFile类型返回

dingnunu的主页 dingnunu | 菜鸟二级 | 园豆:204
提问于:2021-09-08 17:33
< >
分享
所有回答(2)
0

public class FileUtils {

public MultipartFile generateMultipartFile(String fileStr, String fileName, String filePath, Boolean decode) {
		this.getFile(fileStr, filePath, fileName, decode);
		File file = new File(filePath + fileName);
		MultipartFile multipartFile = null;
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			multipartFile = new MockMultipartFile(file.getName(), fileName, null, fileInputStream);
		} catch (IOException e) {
			log.info("异常信息:" + e);
		}
		return multipartFile;
	}


/**
 * 根据byte数组,生成文件
 */
private void getFile(String fileValue, String filePath, String fileName, Boolean decode) {
    byte[] bfile = Base64.getDecoder().decode(fileValue);
    if(null != decode && !decode){
        bfile = fileValue.getBytes();
    }
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
        File dir = new File(filePath);
        //判断文件目录是否存在
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        file = new File(filePath + fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bfile);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

}

dingnunu | 园豆:204 (菜鸟二级) | 2021-09-08 17:34
0

都有Byte[]了,直接转file

人间春风意 | 园豆:2780 (老鸟四级) | 2021-09-09 13:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册