Java 读取网络图片存储到本地(并解决Server returned HTTP response code: 403 for URL报错)

本文介绍了如何使用Java将网络图片保存到本地,并解决了遇到的HTTP 403错误问题。主要步骤包括创建保存图片的文件夹,编写createLocalImage类,以及通过设置User-Agent解决403错误。
摘要由CSDN通过智能技术生成

读取网络图片存储到本地具体步骤如下:

1.创建用于保存图片的文件夹

我的保存路径:D:\images

2.新建 createLocalImage 类

package com.cn.beauty.task;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class createLocalImage {
    // 需要保存到本地的根路径
    private static String basePath = "D:/";
 
    public static void main(String[] args) {
        // 网页图片路径
        String destUrl = "https://lmg.jj20.com/up/allimg/1114/041621122252/210416122252-1-1200.jpg";
        String filePath = createLocalImageMethod(destUrl);
        System.out.println("生成的相对文件路径为" + filePath);
    }
 
    private static String createLocalImageMethod(String destUrl) {
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
     
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是 Java 实现文件下载的代码: ```java import java.io.*; import java.net.*; public class FileDownloader { public static void main(String[] args) { String fileUrl = "http://example.com/file.pdf"; // 文件的 URL 地址 String saveDir = "/home/user/downloads/"; // 文件保存的目录 try { URL url = new URL(fileUrl); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); // 检查服务器响应是否为 OK if (responseCode == HttpURLConnection.HTTP_OK) { // 获取文件名和文件大小 String fileName = ""; String disposition = httpConn.getHeaderField("Content-Disposition"); if (disposition != null) { int startIndex = disposition.indexOf("filename="); if (startIndex > 0) { fileName = disposition.substring(startIndex + 10, disposition.length() - 1); } } else { fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length()); } int fileSize = httpConn.getContentLength(); // 创建输入流和输出流 InputStream inputStream = httpConn.getInputStream(); FileOutputStream outputStream = new FileOutputStream(saveDir + fileName); // 读取文件,并将文件保存到本地磁盘 byte[] buffer = new byte[4096]; int bytesRead = -1; int totalBytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); totalBytesRead += bytesRead; } outputStream.close(); inputStream.close(); System.out.println("File downloaded successfully!"); } else { System.out.println("Error: Server returned response code " + responseCode); } httpConn.disconnect(); } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); } } } ``` 请注意,如果要下载的文件需要身份验证或需要代理服务器,则需要先设置合适的 HTTP 请求头部和代理服务器。在这个示例代码中没有包含这些情况的处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

零

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

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

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

打赏作者

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

抵扣说明:

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

余额充值