java根据图片地址上传图片到服务器或者本地

public static void main(String[] args) throws IOException {
    long start = System.currentTimeMillis();
    //图片地址
    String imageUrl = "https://lmg.jj20.com/up/allimg/4k/s/02/21092423260Q119-0-lp.jpg";
    URL img = new URL(imageUrl);
    HttpURLConnection httpURLConnection = (HttpURLConnection) img.openConnection();
    httpURLConnection.setRequestMethod("GET");
    httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");
    httpURLConnection.setUseCaches(false);//设置不要缓存
    httpURLConnection.setDoInput(true);
    httpURLConnection.connect();
    int responseCode = httpURLConnection.getResponseCode();
    if (responseCode == 200) {
        //        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
        BufferedImage image = ImageIO.read(httpURLConnection.getInputStream());
        // 宽度
        int width = image.getWidth();
        // 高度
        int height = image.getHeight();
        int ratio = width / 384;
        width = 384;
        height = height / ratio;
        Image resultingImage = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
        BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
        ImageIO.write(outputImage, "jpg", new File("D:/liuruibin/image/95663947839277389973291999506439419366740169122091841215215819072613560609894.jpg"));
    }
    httpURLConnection.disconnect();
    long end = System.currentTimeMillis();
    System.out.println((end - start));
}

你可能感兴趣的:(java,服务器,开发语言)