关于Java如何下载在线图片并存入服务器后删除本地照片

是否存在返回值根据自己的情况定义

private List<Structure> saveFile(List<Structure> files) {
    List<Structure> results = new ArrayList<>();
    if (com.alibaba.nacos.common.utils.CollectionUtils.isNotEmpty(files)) {
      for (Structure file : files) {
        Structure structure = new Structure();
        results.add(structure);
        structure.setFileName(file.getFileName());
        structure.setFileDescription("fileId");
        structure.setFileSize(file.getFileSize());
        // 替换为你想要保存的路径和文件名
        String savePath = "D:\\lu\\save\\" + file.getFileName(); 

        try {
        //下载图片并保存
          URL url = new URL(file.getFilePath());
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setRequestMethod("GET");
          connection.setDoOutput(true);
          connection.connect();

          if (connection.getResponseCode() == 200) {
            InputStream inputStream = connection.getInputStream();
            OutputStream outputStream = new FileOutputStream(savePath);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
              outputStream.write(buffer, 0, length);
            }
            outputStream.close();
            inputStream.close();
            System.out.println("Image downloaded and saved to " + savePath);
             //开始上传服务器
          	File photo = new File(savePath);
         	 byte[] photoBytes = new byte[(int) photo.length()];
         	 try (FileInputStream fis = new FileInputStream(photo);
             	 ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            	int bytesRead;
            	while ((bytesRead = fis.read(photoBytes)) != -1) {
             	 bos.write(photoBytes, 0, bytesRead);
            	}
            	//这里根据自己的上传接口参数进行操作
            	String upload =
                	MinioUtils.upload(
                    	new ByteArrayInputStream(bos.toByteArray()),
                    	...
                    	file.getFileName());
            	if (upload != null) { 
            	//如果有返回值代表上传成功,根据自己的上传接口返回值进行判断
              	System.out.println(upload);
              	//根据自己的文件访问参数设置存入参数
              	structure.setFilePath(upload);
              	// 删除本地照片文件
              	photo.delete();
              	System.out.println("Photo deleted after successful upload.");
            	} else {
              	System.out.println("Failed to upload photo.");
            	}
          	}
          } else {
            System.out.println(
                "Image download failed with response code " + connection.getResponseCode());
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    //这一步可以直接存入数据,也可作为file字段的处理方法
    return results;
  }
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值