java怎样保存图片_使用Java保存风景图片

1.下载依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.feiyangedu.sample

hellodep

0.0.1-SNAPSHOT

jar

UTF-8

1.8

1.8

1.8

javax.mail

mail

1.4

com.fasterxml.jackson.core

jackson-databind

2.9.0

com.fasterxml.jackson.datatype

jackson-datatype-jsr310

2.9.0

mysql

mysql-connector-java

8.0.17

com.zaxxer

HikariCP

3.3.1

org.apache.poi

poi

3.17

org.apache.poi

poi-ooxml

3.17

org.testng

testng

6.14.3

compile

io.rest-assured

rest-assured

4.1.1

compile

org.hamcrest

hamcrest-all

1.3

test

org.hamcrest

hamcrest-core

1.3

test

org.apache.httpcomponents

httpclient

4.5.8

org.jsoup

jsoup

1.10.3

2.解析图片地址

https://www.ivsky.com/tupian/shenqiu_luoye_v58110/

c3d2ff60c2bd1a3f3b77fe82248277cb.png

确定图片元素路径,以用于解析。

注意需要将//img.ivsky.com/img/tupian/t/201909/09/shenqiu_luoye.jpg替换为pre。因为t是缩略图,而pre是原图。

3.示例

package com.testerhome;

import io.restassured.response.Response;

import org.apache.http.HttpConnection;

import org.apache.http.HttpEntity;

import org.apache.http.HttpStatus;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

import org.testng.annotations.Test;

import java.io.*;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import static io.restassured.RestAssured.given;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.greaterThan;

public class GetScenery {

@Test

public void getAutumn(){

String des = "autumn";

String url = "https://www.ivsky.com/tupian/shenqiu_luoye_v58110/";

File desDir = new File(des);

if(!desDir.exists() || !desDir.isDirectory()){

desDir.mkdir();

}

ArrayList imgUrls = getPic(url);

for(int i=0;i

downPic(imgUrls.get(i),des);

System.out.println(imgUrls.get(i));

}

}

public ArrayList getPic(String url){

ArrayList result = new ArrayList<>();

String select = "body > div:nth-child(4) > div.left > ul";

CloseableHttpClient httpClient = HttpClients.createDefault();

CloseableHttpResponse response ;

HttpGet request = new HttpGet(url);

request.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");

try{

response = httpClient.execute(request);

HttpEntity httpEntity = response.getEntity();

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

String html = EntityUtils.toString(httpEntity, "gbk");

Document document = Jsoup.parse(html);

Elements pics = document.select("div.left>ul.pli>li");

for(Element pic:pics){

Elements img = pic.select("div.il_img>a>img");

String imgUrl = "http:"+img.attr("src");

//t是缩略图,pre是大图

result.add(imgUrl.replaceAll(".?t/","/pre/"));

}

}

}catch (ClientProtocolException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}

return result;

}

public void downPic(String url,String dir){

String fileName = dir + url.substring(url.lastIndexOf("/"));

try {

FileOutputStream fop = new FileOutputStream(fileName);

URL imgUrl = new URL(url);

HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();

InputStream fip = conn.getInputStream();

File file = new File(fileName);

byte[] buffer = new byte[2048];

for(;;){

int hasRead = fip.read(buffer);

if(hasRead <0){

break;

}

fop.write(buffer,0,hasRead);

}

}catch (Exception e){

e.printStackTrace();

}

}

}

27bdbd5d2ea417f1da84bf0c33aaa863.png

标签:Java,String,http,保存,风景图片,org,apache,import,com

来源: https://www.cnblogs.com/csj2018/p/12389858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值