好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 hunanxingshen 于 2020-12-31 12:09 编辑
首页
点击图片放大
左右侧滑
长按分享保存
public void getEmoticonFBQ() throws IOException {
Map<String, String> map = new HashMap<>();
map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
map.put("Accept-Encoding", "gzip, deflate, br");
map.put("Accept-Language", "zh-CN,zh;q=0.9");
map.put("Cookie", "UM_distinctid=1768da8c14a322-04868af06cab59-c791039-384000-1768da8c14bb9b; PHPSESSID=vicpmeug9nmc8vks381l24mcrj; CNZZDATA1260546685=1968502580-1608693375-https%253A%252F%252Fwww.baidu.com%252F%7C1609329890");
map.put("Upgrade-Insecure-Requests", "1");
map.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
// 图片存储目录及图片名称
String url_pathYes = "/image/y" + format.format(new Date());
String url_pathNo = "/image/n" + format.format(new Date());
int num=1;
for (int ii = 1; ii <= 50; ii++) {
log.info("第" + ii + "页");
Document document = Jsoup.connect("https://www.fabiaoqing.com/biaoqing/lists/page/" + ii).headers(map).get();
Elements elements = document.body().select("img");
log.info("拿到图片准备循环");
if (num % 5 == 0) {
num=1;
url_pathYes = "/image/y" + format.format(new Date());
url_pathNo = "/image/n" + format.format(new Date());
getImages(elements,url_pathYes,url_pathNo);
}else{
getImages(elements,url_pathYes,url_pathNo);
num++;
}
}
}
private void getImages(Elements elements,String url_pathYes,String url_pathNo) {
for (int i = 1; i < elements.size(); i++) {
try {
String url = elements.get(i).attr("data-original").toString();
String alt = elements.get(i).attr("alt");
alt = alt.replace("/", "").replace(" ", "");
if (StringUtils.isEmpty(alt)) {
continue;
}
log.info("第" + i + "条数据:" + alt);
System.out.println(url);
URL u = new URL(url);
URLConnection con = u.openConnection();
InputStream is = con.getInputStream();
//转换成字节数组
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) {
bos.write(b, 0, n);
}
is.close();
bos.close();
//压缩转base64
byte[] compressionBytes = compressPicForScale(bos.toByteArray(), 15L, alt);
MultipartFile fileYes = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), compressionBytes);
String fileName = saveEmo(url_pathYes, url, alt, fileYes);
//没有压缩
MultipartFile fileNo = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), bos.toByteArray());
saveEmo(url_pathNo, url, alt, fileNo);
//保存数据库地址,用户访问
ImagesEntity im = new ImagesEntity();
im.setAlt(alt);
im.setUrl(url_pathNo + "/" + fileName);
im.setYUrl(url_pathYes + "/" + fileName);
im.setCreateTime(new Date());
imagesMapper.saveEntity(im);
//Thread.sleep(100);
} catch (Exception e) {
System.out.println("有异常");
e.printStackTrace();
}
}
}
private String saveEmo(String url_path, String url, String alt, MultipartFile file) throws IOException {
String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
String[] strArray = url.split("\\.");
String fileName = alt + "." + strArray[strArray.length - 1];
//图片保存路径
String savePath = staticPath + url_path;
log.info("图片保存地址:" + savePath);
// 访问路径=静态资源路径+文件目录路径
String visitPath = "static" + url_path;
log.info("图片访问uri:" + visitPath);
File saveFile = new File(savePath);
if (!saveFile.exists()) {
saveFile.mkdirs();
}
file.transferTo(new File(saveFile.getAbsolutePath(), fileName)); //将临时存储的文件移动到真实存储路径下
return fileName;
}
/**
* 根据指定大小压缩图片
*
* @Param imageBytes 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @param imageId 影像编号
* @Return 压缩质量后的图片字节数组
*/
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize, String imageId) {
if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
return imageBytes;
}
long srcSize = imageBytes.length;
double accuracy = getAccuracy(srcSize / 1024);
try {
while (imageBytes.length > desFileSize * 1024) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream)
.scale(accuracy)
.outputQuality(accuracy)
.toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
}
log.info("【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb", imageId, srcSize / 1024, imageBytes.length / 1024);
} catch (Exception e) {
log.error("【图片压缩】msg=图片压缩失败!", e);
}
return imageBytes;
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
double accuracy;
if (size < 900) {
accuracy = 0.75;
} else if (size < 2047) {
accuracy = 0.5;
} else if (size < 3275) {
accuracy = 0.4;
} else {
accuracy = 0.3;
}
return accuracy;
}
|
免费评分
-
查看全部评分
|