Base64转图片后存储到服务器,将服务器路径返回到前端

该代码段展示了一个Java工具类,用于将Base64编码的字符串转换为文件并存储到本地,同时配置了SpringMVC的资源处理器以映射到文件存储路径,支持跨域访问。主要涉及文件操作、日期处理、Base64解码以及SpringMVC的资源配置。
摘要由CSDN通过智能技术生成

工具类

/**
     * 资源映射路径 前缀
     */
    public String localFilePrefix="/huashu-statics";

    /**
     * 域名或本机访问地址
     */
    public String domain="http://www.huashudashi.com";

    /**
     * 上传文件存储在本地的根路径
     */
    private String localFilePath="D:/huashu/upload/uploadPath/";
    
public String base64ToFile(String base64) {
            File file = null;
            Date date = new Date();
            Calendar now = Calendar.getInstance();
            String datePath = "/"+now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH) + 1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/";
            //文件目录路径
            String filePath=this.localFilePath+datePath;
			//文件名
            String fileName = date.getTime()+".png";
            File  dir=new File(filePath);
            //创建文件目录
            if (!dir.exists() && !dir.isDirectory()) {
                dir.mkdirs();
            }
            BufferedOutputStream bos = null;
            java.io.FileOutputStream fos = null;
            try {
                byte[] bytes = Base64.decode(base64);
                file=new File(filePath+"/"+fileName);
                fos = new java.io.FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return this.domain+this.localFilePrefix+datePath+fileName;
        }

配置类:


import java.io.File;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用映射配置
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 资源映射路径 映射到 本地文件上传根路径 */
        registry.addResourceHandler(localFilePrefix + "/**")
                .addResourceLocations("file:" + localFilePath + File.separator);
    }
    
    /**
     * 开启跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping(localFilePrefix  + "/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 设置允许的方法
                .allowedMethods("GET");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值