Commit 65dbfc3c authored by shiyu's avatar shiyu

更新图片url

parent 88f442b6
...@@ -19,9 +19,13 @@ import org.springframework.stereotype.Component; ...@@ -19,9 +19,13 @@ import org.springframework.stereotype.Component;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.IntStream; import java.util.stream.IntStream;
...@@ -106,37 +110,36 @@ public class SynCoinJob { ...@@ -106,37 +110,36 @@ public class SynCoinJob {
CoinRequestDto dto = new CoinRequestDto(); CoinRequestDto dto = new CoinRequestDto();
dto.setPage(i); dto.setPage(i);
dto.setLimit(100); dto.setLimit(100);
PageInfo<Item> pageInfo2 = itemDao.findByPage(coinRequestDto); List<Item> itemList = itemDao.findListByPage(dto);
List<Item> itemList = pageInfo2.getList();
itemList.parallelStream().forEach(item -> { itemList.parallelStream().forEach(item -> {
String imageUrl = item.getImages(); String imageUrl = item.getImages();
if (StringUtils.hasLength(imageUrl)) { if (StringUtils.hasLength(imageUrl)) {
String[] images = imageUrl.split(";"); String[] images = imageUrl.split(";");
String newImages = ""; StringBuffer stringBuffer = new StringBuffer();
for (String image : images) { Arrays.stream(images).parallel().forEach(image -> {
if (StringUtils.hasLength(newImages)) { if (stringBuffer.length() != 0) {
newImages = newImages + ";"; stringBuffer.append(";");
} }
try { try {
URL url = new URL(image); URL url = new URL(image);
InputStream inputStream = url.openStream(); InputStream inputStream = url.openStream();
BufferedImage bufferedImage = ImageIO.read(inputStream); BufferedImage bufferedImage = ImageIO.read(inputStream);
String keyName = CommConsts.UPLOAD_PRE_DIRECTORY + CommonUtils.getUUID() + "_" + bufferedImage.getWidth() + "x" + bufferedImage.getHeight() + "." + MediaTypeEnum.IMAGE_JPG.getExt(); String keyName = CommConsts.UPLOAD_PRE_DIRECTORY + CommonUtils.getUUID() + "_" + bufferedImage.getWidth() + "x" + bufferedImage.getHeight() + "." + MediaTypeEnum.IMAGE_JPG.getExt();
InputStream imageStream = bufferedImageToInputStream(bufferedImage, MediaTypeEnum.IMAGE_JPG.getExt());
InputStream imageStream = url.openStream();
qiniuStorage.store(imageStream, 1, MediaTypeEnum.IMAGE_JPG.getMime(), keyName); qiniuStorage.store(imageStream, 1, MediaTypeEnum.IMAGE_JPG.getMime(), keyName);
String urlOfSize = qiniuStorage.generateUrl(keyName); String urlOfSize = qiniuStorage.generateUrl(keyName);
logger.info(">>>>>>>>>>>>> id : {} ; new URL = {}", item.getId(), urlOfSize); logger.info(">>>>>>>>>>>>> id : {} ; new URL = {}", item.getId(), urlOfSize);
newImages = newImages + urlOfSize; stringBuffer.append(urlOfSize);
} catch (Exception e) { } catch (Exception e) {
newImages = newImages + image; stringBuffer.append(image);
logger.error("更新图片 url : {}", e); logger.error("更新图片错误 id: {} , url : {}, error :{}", item.getId(), image, e);
}
} }
});
Item updateItem = new Item(); Item updateItem = new Item();
updateItem.setId(item.getId()); updateItem.setId(item.getId());
updateItem.setImages(newImages); updateItem.setImages(stringBuffer.toString());
itemDao.update(updateItem); itemDao.update(updateItem);
logger.info(">>>>>>>>>>>>> id : {} ; 更新图片地址成功 !!!", item.getId());
} }
}); });
logger.info(">>>>>>>>>>>>>>>>> 更新商品图片尺寸的页码: {}, 更新成功 <<<<<<<<<<<<<<<<<", i); logger.info(">>>>>>>>>>>>>>>>> 更新商品图片尺寸的页码: {}, 更新成功 <<<<<<<<<<<<<<<<<", i);
...@@ -148,4 +151,21 @@ public class SynCoinJob { ...@@ -148,4 +151,21 @@ public class SynCoinJob {
} }
/**
* 将BufferedImage转换为InputStream
*
* @param image 数据源
* @param type 图片格式
* @return
*/
private static InputStream bufferedImageToInputStream(BufferedImage image, String type) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
ImageIO.write(image, type, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
logger.error("bufferedImageToInputStream 出错,原因: {}", e);
}
return null;
}
} }
...@@ -134,7 +134,7 @@ spring: ...@@ -134,7 +134,7 @@ spring:
username: owner username: owner
password: VS1xaA37hyu1wJSw password: VS1xaA37hyu1wJSw
initial-size: 10 initial-size: 10
max-active: 100 max-active: 200
min-idle: 10 min-idle: 10
max-wait: 600000 max-wait: 600000
pool-prepared-statements: true pool-prepared-statements: true
......
...@@ -133,7 +133,7 @@ spring: ...@@ -133,7 +133,7 @@ spring:
username: quanku username: quanku
password: 3N0W8i0b1Wm35MJ5 password: 3N0W8i0b1Wm35MJ5
initial-size: 10 initial-size: 10
max-active: 100 max-active: 200
min-idle: 10 min-idle: 10
max-wait: 60000 max-wait: 60000
pool-prepared-statements: true pool-prepared-statements: true
......
...@@ -17,6 +17,8 @@ public interface ItemDao { ...@@ -17,6 +17,8 @@ public interface ItemDao {
List<Item> findList(CoinRequestDto coinRequestDto); List<Item> findList(CoinRequestDto coinRequestDto);
List<Item> findListByPage(CoinRequestDto coinRequestDto);
PageInfo<Item> findByPage(CoinRequestDto coinRequestDto); PageInfo<Item> findByPage(CoinRequestDto coinRequestDto);
Item findDetails(Long id); Item findDetails(Long id);
......
...@@ -50,6 +50,16 @@ public class ItemDaoImpl implements ItemDao { ...@@ -50,6 +50,16 @@ public class ItemDaoImpl implements ItemDao {
return itemMapper.selectByExample(example); return itemMapper.selectByExample(example);
} }
@Override
public List<Item> findListByPage(CoinRequestDto coinRequestDto) {
ItemExample example = new ItemExample();
ItemExample.Criteria criteria = example.createCriteria();
example.setOrderByClause("create_time asc");
PageHelper.startPage(coinRequestDto.getPage(), coinRequestDto.getLimit());
return itemMapper.selectByExampleWithBLOBs(example);
}
@Override @Override
public PageInfo<Item> findByPage(CoinRequestDto coinRequestDto) { public PageInfo<Item> findByPage(CoinRequestDto coinRequestDto) {
ItemExample example = new ItemExample(); ItemExample example = new ItemExample();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment