Java reads network pictures and stores them locally (and solves Server returned HTTP response code: 403 for URL error)

The specific steps for reading network pictures and storing them locally are as follows:

1. Create a folder for saving pictures

My save path: D:\images

2. Create a new createLocalImage class

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;
        HttpURLConnection httpUrl = null;
        URL url = null;
        int BUFFER_SIZE = 1024;
        byte[] buf = new byte[BUFFER_SIZE];
        int size = 0;
        String filePath = "";
        try {
            System.out.println("原始图片URL为:" + destUrl);
            String[] fileNameArray = destUrl.split("\\/");
            if (fileNameArray.length > 1) {
                String fileName = fileNameArray[fileNameArray.length - 1];
                filePath = "images/" + fileName;
                File file = new File(basePath + filePath);
                if (!file.exists()) {
                    url = new URL(destUrl);
                    httpUrl = (HttpURLConnection) url.openConnection();
                    httpUrl .setRequestProperty("User-Agent", "Mozilla/4.76");
                    httpUrl.connect();
                    bis = new BufferedInputStream(httpUrl.getInputStream());
                    fos = new FileOutputStream(basePath + filePath);
                    while ((size = bis.read(buf)) != -1) {
                        fos.write(buf, 0, size);
                    }
                    fos.flush();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
                bis.close();
                httpUrl.disconnect();
            } catch (IOException e) {
            } catch (NullPointerException e) {
            }
        }
        return filePath;
    }
}

After running, it is found that the picture has been successfully generated:
insert image description here
3. Some small partners may encounter the error of Server returned HTTP response code: 403 for URL when they do their own functions . The reason is that the security settings of the server do not accept Java programs as client access

just need this line of code

httpUrl = (HttpURLConnection) url.openConnection();

join later

httpUrl .setRequestProperty("User-Agent", "Mozilla/4.76");

That's it, and the careful friends also found that I added it to the above code.
The principle is to deceive the server by setting the User-Agent.

Guess you like

Origin blog.csdn.net/poker_zero/article/details/129063525