Springboot后端实现图片上传

1、图片存储

1.1 单文件Base64存文件服务器

  • 配置中心添加文件路径配置

    file:
      face-path: E:/data/face/
      face-server: http://192.168.1.174:8081/face/
    
  • 配置nginx.conf

    server {
        listen       8081;
        server_name  localhost;
    
        location / {  
            root   E:/data/;
            autoindex  on;
        }
    }
    
  • 工具类方法

    /**
         * 输入转化后的(或者前端传递的)base64和要生成的文件位置
         * @param base64
         * @param filePath
         * @return
         */
    public static Boolean decryptByBase64(String base64, String filePath) {
        if (base64 == null && filePath == null) {
            return false;
        }
        try {
            Files.write(Paths.get(filePath), Base64.getMimeDecoder().decode(base64), StandardOpenOption.CREATE);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    //参数1:Base64字节编码;参数2:文件名;参数3:图片存储实际路径;参数4:图片服务器路径
    public static String decryptByBase64(String base64Str, String fileName, String filePath, String fileServerPath) {
        Boolean result = decryptByBase64(base64Str, filePath+fileName);
        if (result) {
            return fileServerPath+fileName;
        }
        return null;
    }
    
  • 使用

    @Value("${file.face-path}")
    String fileFacePath;
    @Value("${file.face-server}")
    String faceFileServerPath;
    
    {
        String base64str = secretRelatedPersonInfo.getImgPhoto();
        if(base64str!=null && base64str!=""){
            String[] base64 = base64str.split(",");
            //参数1:Base64字节编码;参数2:文件名;参数3:图片存储实际路径;参数4:图片服务器路径
            String filepath = FileUtil.decryptByBase64(base64[1], secretRelatedPersonInfo.getImgKey(), fileFacePath, faceFileServerPath);
            secretRelatedPersonInfo.setFilePath(filepath);//库中存入路径
        }
    }
    

1.2 文件流传递多张图片

  • 实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
class T{
    //用于存储图片的列表String输出
    private String img;
    //用于接收多个/单个文件流
    private MultipartFile[] files;
    //...其他变量
}
  • 具体实现
file.path=E:\\data\\file\\
file.server=http://localhost:8088/file/
@Value("${file.path}")
String path;

@Value("${file.server}")
String serverPath;

@Override
public void save(T t) {
    if (t.getFiles()!=null){
        StringBuilder builder = new StringBuilder();
        List<String> imgs = new ArrayList<String>()
        try {
            for (int i = 0; i < t.getFiles().length; i++) {
                // 获取文件名称
                String fileName = t.getFiles()[i].getOriginalFilename();
                // 生成新文件名
                String generateFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileName.substring(fileName.lastIndexOf("."));
                // 拼接后得到图片完整url
                String distFileAddress = serverPath + generateFileName;
                builder.append(distFileAddress+",");
                //将图片信息url存储到list中
                imgs.add(distFileAddress);
                //保存文件,路径必须是绝对路径
                t.getFiles()[i].transferTo(new File(path + generateFileName));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值