fix:图片插入

上级 7c6122d8
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -104,6 +104,18 @@ public class PicInfoController {
}
/**
* 通过url新增图片
*
* @param url
* @return
*/
@PostMapping(value = "/insertByBaiduUrl")
public Result insertByBaiduUrl(@RequestParam String url) {
return Result.ok(this.picInfoService.insertByBaiduUrl(url));
}
/**
* 修改数据
*
......
package com.kwan.springbootkwan.entity.pic;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class BaiduPic implements Serializable {
private Integer listNum;
private String gsm;
private Integer isNeedAsyncRequest;
private List<PicDataInfo> data;
private String queryEnc;
private String bdSearchTime;
private Integer displayNum;
private String queryExt;
private String bdFmtDispNum;
private String bdIsClustered;
}
\ No newline at end of file
package com.kwan.springbootkwan.entity.pic;
import lombok.Data;
import java.io.Serializable;
import java.lang.Integer;
import java.lang.Object;
import java.lang.String;
import java.util.List;
@Data
public class PicDataInfo implements Serializable {
private Integer is_gif;
private String di;
private Integer isCommodity;
private String imgCollectionWord;
private String largeTnImageUrl;
private String type;
private String currentIndex;
private Integer hasLarge;
private String strategyAssessment;
private String adPicId;
private String fromJumpUrl;
private Integer height;
private String hasAspData;
private String middleURL;
private String is;
private String source_type;
private Integer spn;
private Integer isAspDianjing;
private Object face_info;
private String shituToken;
private String bdFromPageTitlePrefix;
private String simid;
private String fromPageTitleEnc;
private String fromURLHost;
private Integer isCopyright;
private String fromPageTitle;
private String filesize;
private Object resourceInfo;
private String bdSourceName;
private String personalized;
private String adType;
private String hasThumbData;
private String thumbURL;
private Object commodityInfo;
private String imgType;
private String os;
private String bdSrcType;
private String bdImgnewsDate;
private Integer pageNum;
private Object xiangshi_info;
private String token;
private String cs;
private Integer bdSetImgNum;
private Object simid_info;
private String fromURL;
private String objURL;
private Integer width;
private String pi;
private Integer partnerId;
private String hoverURL;
private List<Object> replaceUrl;
}
\ No newline at end of file
......@@ -11,6 +11,20 @@ import com.kwan.springbootkwan.entity.PicInfo;
*/
public interface PicInfoService extends IService<PicInfo> {
/**
* 根据目录插入图片
*
* @param path
* @return
*/
boolean insertByPath(String path);
/**
* 根据百度图片路径获取图片
*
* @param url
* @return
*/
boolean insertByBaiduUrl(String url);
}
package com.kwan.springbootkwan.service.impl;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kwan.springbootkwan.entity.PicInfo;
import com.kwan.springbootkwan.entity.pic.BaiduPic;
import com.kwan.springbootkwan.entity.pic.PicDataInfo;
import com.kwan.springbootkwan.mapper.PicInfoMapper;
import com.kwan.springbootkwan.service.PicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -13,6 +19,7 @@ import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
/**
......@@ -21,6 +28,7 @@ import java.util.Objects;
* @author makejava
* @since 2023-08-09 12:44:03
*/
@Slf4j
@Service("picInfoService")
public class PicInfoServiceImpl extends ServiceImpl<PicInfoMapper, PicInfo> implements PicInfoService {
......@@ -37,23 +45,55 @@ public class PicInfoServiceImpl extends ServiceImpl<PicInfoMapper, PicInfo> impl
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath)) {
for (Path filePath : directoryStream) {
if (Files.isRegularFile(filePath)) {
final String picName = filePath.getFileName().toString();
String picPath = PRE_FIX + picName;
final String picNameOrigin = filePath.getFileName().toString();
String picPath = PRE_FIX + picNameOrigin;
final String picName = picNameOrigin.split(".")[0];
this.insertPic(picPath, picName);
}
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 插入图片
*
* @param picPath
* @param picName
*/
private void insertPic(String picPath, String picName) {
QueryWrapper<PicInfo> wrapper = new QueryWrapper<>();
wrapper.eq("pic_url", picPath);
wrapper.eq("is_delete", 0);
final PicInfo pic = picInfoMapper.selectOne(wrapper);
if (Objects.nonNull(pic)) {
continue;
return;
}
PicInfo picInfo = new PicInfo();
picInfo.setPicName(picName.split(".")[0]);
picInfo.setPicName(picName);
picInfo.setPicUrl(picPath);
picInfoMapper.insert(picInfo);
}
@Override
public boolean insertByBaiduUrl(String url) {
HttpResponse response;
try {
response = HttpUtil.createGet(url).execute();
String responseBody = response.body();
ObjectMapper objectMapper = new ObjectMapper();
BaiduPic baiduPic = objectMapper.readValue(responseBody, BaiduPic.class);
final List<PicDataInfo> data = baiduPic.getData();
for (PicDataInfo datum : data) {
final String hoverURL = datum.getHoverURL();
final String picName = datum.getFromPageTitle().split(".")[0];
this.insertPic(hoverURL, picName);
}
return true;
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册