Java实现端到端多文件上传

描述:

        新需求,公司需要将一体机的多张图片通过java后端上传到远程服务器的后台管理系统。

客户端实现

@GetMapping("uploadImg")
public static String uploadImg(){
        // 读取图片
        String imageLocation1 = "D:\\1.png";
        String imageLocation2 = "D:\\2.png";
        String imageLocation3 = "D:\\3.png";
        FileSystemResource fileSystemResource1 = new FileSystemResource(imageLocation1);
        FileSystemResource fileSystemResource2 = new FileSystemResource(imageLocation2);
        FileSystemResource fileSystemResource3 = new FileSystemResource(imageLocation3);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", fileSystemResource1);
        form.add("file", fileSystemResource2);
        form.add("file", fileSystemResource3);

        // 上传图片  YmlValueConfig.getHttpUrl():第三方服务器地址
        String url = YmlValueConfig.getHttpUrl() + "/file/uploadFile";
        String res = RestTemplateUtils.httpUploadImg(url, form);
        log.info(res);
        return res;
}

 服务器接口

    //上传文件
    @PostMapping("/file/uploadFile")
    @ResponseBody
    public Result uploadFile(@RequestParam("file")  MultipartFile[] fileList ) {
        log.info("fileList:[{}]", fileList);
        String filePathName = null;
        try {
            File dir = new File(imagePath);
            if (!dir.exists()) {
                dir.mkdirs();
                dir.setWritable(true, false);
            }
            for (MultipartFile mfile : fileList) {
            String fileName = mfile.getOriginalFilename();
            filePathName = imagePath + fileName;
            FileCopyUtils.copy(mfile.getBytes(), new File(filePathName));
            }
        } catch (IOException e) {
            log.error("upload error:[{}]", e.getMessage());
            return Result.success(HttpStatus.ERROR, "上传失败");
        }
        return Result.success(HttpStatus.SUCCESS, "上传成功");
    }

简单实现,仅供参考

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值