base64文件上传

配置

tupian:
  uploadPath: d:/file
  url: http://192.168.8.69:${server.port}/files/
//文件映射类
@Component
@ConfigurationProperties(prefix = "图片")
public class StaticFile {

    private String uploadPath;
    private String url;

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
//项目路径配置
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private StaticFile staticFile;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/files/**")
                .addResourceLocations("file:///"+staticFile.getUploadPath()+"/");
    }
}

//文件上传代码
import org.springframework.util.Base64Utils;//base64转换工具类
import org.apache.commons.io.FileUtils;//文件IO工具类
  @Override
 public ResultBean fileUpLoad(String fileBase64) {
        String dataPrix = "";
        String data = "";
        String suffix = "";

        if (StringUtils.isEmpty(fileBase64)){
            return new ResultBean(500,"缺少必要参数");
        }else {
            String[] d = fileBase64.split("base64,");
            if(d != null && d.length == 2){
                dataPrix = d[0];
                data = d[1];
            }else{
                return new ResultBean(500,"base64参数不合法");
            }
        }

        if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){
            //data:image/jpeg;base64,base64编码的jpeg图片数据
            suffix = ".jpg";
        } else if("data:image/png;".equalsIgnoreCase(dataPrix)){
            //data:image/png;base64,base64编码的png图片数据
            suffix = ".png";
        }else if ("data:application/vnd.ms-excel;".equals(dataPrix)){
            suffix = ".xls";
        }else if ("data:application/msword;".equals(dataPrix)){
            suffix = ".doc";
        }else if ("data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;".equals(dataPrix)){
            suffix = ".docx";
        }else if ("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;".equals(dataPrix)){
            suffix = ".xlsx";
        }else{
            return new ResultBean(500,"文件只支持[.jpg,.png,.xls,.doc,.docx,.xlsx]格式的文件");
        }
        String uuid = UUIDUtils.getUUID();
        String fileName = uuid+suffix;
        try {
            String nowDate = DateUtils.getCurrentDate("yyyy-MM-dd");
            byte[] bs = Base64Utils.decodeFromString(data);
            FileUtils.writeByteArrayToFile(new File(staticFile.getUploadPath()+"/"+nowDate, fileName), bs);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("fileUrl",pathFileName);
            return new ResultBean(200,"文件接收成
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBean(500,"文件接收失败");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会跑的葫芦怪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值