Java 将网络图片URL 转为file文件

    /**
   * 将图片转为file
   *
   * @param url 图片url
   * @return File
   */
  private static File getFile(String url) throws Exception {
      //读取图片类型
      String fileName = url.substring(url.lastIndexOf("."),url.length());
      File file = null;

      URL urlfile;
      InputStream inStream = null;
      OutputStream os = null;
      try {
          file = File.createTempFile("new_url", fileName);
          //获取文件
          urlfile = new URL(url);
          inStream = urlfile.openStream();
          os = new FileOutputStream(file);

          int bytesRead = 0;
          byte[] buffer = new byte[8192];
          while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
              os.write(buffer, 0, bytesRead);
          }
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          try {
              if (null != os) {
                  os.close();
              }
              if (null != inStream) {
                  inStream.close();
              }

          } catch (Exception e) {
              e.printStackTrace();
          }
      }

      return file;
  }

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要将本地文件转为外部能访问的 URL,需要将该文件上传至一个能够提供文件访问服务的云存储平台,例如Amazon S3、Azure Blob Storage或者Google Cloud Storage等。以下是使用Amazon S3作为云存储平台的Java代码示例: ```java import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.PutObjectRequest; import java.io.File; public class UploadToS3 { private static String bucketName = "your-bucket-name"; private static String keyName = "your-file-name"; private static String uploadFileName = "path/to/your/local/file"; public static void main(String[] args) throws Exception { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFileName); s3client.putObject(new PutObjectRequest( bucketName, keyName, file)); String publicUrl = s3client.getUrl(bucketName, keyName).toString(); System.out.println("Public URL of the uploaded file: " + publicUrl); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } } ``` 这段代码会将本地文件上传至Amazon S3,并返回该文件的公共访问URL。你可以将该URL分享给外部用户,让其访问该文件。请注意,为了使用Amazon S3服务,你需要在AWS Console中创建一个S3 Bucket,并且配置相应的访问权限。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值