提交 73027637 authored 作者: 吴德鹏's avatar 吴德鹏

图片压缩

上级 24529114
......@@ -125,6 +125,8 @@ public class TbCfStationItem {
private Integer sort;
private String handled;
@JsonIgnore
@ManyToOne
@JoinColumn(columnDefinition = "template", name = "template")
......@@ -504,4 +506,11 @@ public class TbCfStationItem {
return itemDescritionId;
}
public String getHandled() {
return handled;
}
public void setHandled(String handled) {
this.handled = handled;
}
}
......@@ -5,9 +5,11 @@ import com.example.afrishop_v3.models.TbCfStationItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
......@@ -37,7 +39,7 @@ public interface TbCfStationItemRepository extends PagingAndSortingRepository<Tb
Page<TbCfStationItem> findByItemCategory(String itemName, Pageable pageable);
List<TbCfStationItem> findAllByItemDescritionId(String id,Sort sort);
List<TbCfStationItem> findAllByItemDescritionId(String id, Sort sort);
@Query(value = "select i from #{#entityName} i inner join AdvertisementItem a on i.itemId=a.itemId where a.adId=?1 order by a.sort")
Page<TbCfStationItem> getAdvertisementItem(String adId, Pageable pageable);
......@@ -49,4 +51,12 @@ public interface TbCfStationItemRepository extends PagingAndSortingRepository<Tb
List<TbCfStationItem> findAllByItemIdIn(String[] ids);
@Query(value = "select i from #{#entityName} i where i.handled is null ")
List<TbCfStationItem> queryItemsAll();
@Modifying
@Query(value = "update TbCfStationItem set handled='Y',itemImg=:itemImg where itemId=:itemId")
@Transactional
int updateItmeImg(@Param("itemImg") String itemImg, @Param("itemId") String itemId);
}
//package com.example.afrishop_v3;
//
//import com.example.afrishop_v3.models.Country;
//import com.example.afrishop_v3.models.JsonTag;
//import com.example.afrishop_v3.models.TbCfOrder;
//import com.example.afrishop_v3.models.Token;
//import com.example.afrishop_v3.repository.CountryRepository;
//import com.example.afrishop_v3.repository.TbCfOrderRepository;
//import com.example.afrishop_v3.repository.TokenRepository;
//import com.example.afrishop_v3.util.IdUtil;
//import com.google.gson.JsonObject;
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;
//import org.junit.Test;
//import org.junit.runner.Result;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.http.ResponseEntity;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.web.client.RestTemplate;
//
//import java.text.SimpleDateFormat;
//import java.time.LocalDateTime;
//import java.util.*;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.Future;
//import java.util.concurrent.locks.ReentrantLock;
//
//@RunWith(SpringRunner.class)
//@SpringBootTest
//public class AfrishopV3ApplicationTests {
//
// RestTemplate restTemplate = new RestTemplate();
//
// @Test
// public void contextLoads() {
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// String now = format.format(new Date());
// System.out.println(now);
// }
//
// private String url = "http://localhost:8099/afrishop/discover/bonus/saveNetworkMarketing";
// @Autowired
// private TbCfOrderRepository orderRepository;
//
//// post35789572fbf643cdab55901d1dc511a6
//// userInfo00297bf300ed45e092a5182dececad21
//// productSharervv32GmG4q
//// amount0.9
//// orderId008272b1fb994aef9b199449f46d5aa5
//
// @Test
// public void test1() {
//
// Optional<TbCfOrder> byId = orderRepository.findById("12a9d7d1042348278c97980a00fc67c4");
// JSONObject json = new JSONObject();
// json.put("userInfo", "32dcda5a709c4265a6f01686ae9b1c1c");
// json.put("amount", byId.get().getItemsPrice());
// json.put("orderId", "12a9d7d1042348278c97980a00fc67c4");
// System.out.println(json);
// ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity(url, json, Result.class);
// System.err.println(resultResponseEntity);
// }
//
// @Autowired
// private TokenRepository tokenRepository;
//
// @Autowired
// private CountryRepository countryRepository;
//
//
// @Test
// public void test2() {
// List list = new ArrayList();
// list.add(2);
// list.add(1);
// list.add(3);
// list.forEach(System.out::println);
//
// }
//
// private ReentrantLock reentrantLock = new ReentrantLock();
//
// @Test
// public void testImport() {
// ExecutorService executorService = Executors.newCachedThreadPool();
//
// for (int i = 0; i < 20; i++) {
// executorService.execute(() -> importCountry());
// }
//
// }
//
// @Test
// public void importCountry() {
//
// String url = "https://api.jisuapi.com/country/query?name=&continent=非洲&language=&iscountry=&appkey=732afb9ddae9eef8";
// ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
// String body = forEntity.getBody();
// System.err.println(body);
// JSONObject jsonObject = JSONObject.fromObject(body);
// String status = jsonObject.getString("status");
// if ("0".equals(status)) {
// JSONArray jsonArray = jsonObject.getJSONArray("result");
// for (int i = 0; i < jsonArray.size(); i++) {
// Country country = new Country();
// JSONObject object = jsonArray.getJSONObject(i);
// String ename = object.getString("ename");
// String cname = object.getString("cname");
// String areaCode = object.getString("areacode");
// String nationalFlag = object.getString("nationalflag");
// country.setAreaCode(areaCode);
// country.setCountryCname(cname);
// country.setCountryName(ename);
// country.setNationalFlag(nationalFlag);
// country.setId(IdUtil.createIdbyUUID());
// countryRepository.save(country);
//
// }
// }
//
//
// }
//
//
//}
package com.example.afrishop_v3;
import com.example.afrishop_v3.models.*;
import com.example.afrishop_v3.repository.CountryRepository;
import com.example.afrishop_v3.repository.TbCfOrderRepository;
import com.example.afrishop_v3.repository.TbCfStationItemRepository;
import com.example.afrishop_v3.repository.TokenRepository;
import com.example.afrishop_v3.util.IdUtil;
import com.example.afrishop_v3.util.OssUtil;
import com.example.afrishop_v3.util.PicUtils;
import com.google.gson.JsonObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AfrishopV3ApplicationTests {
RestTemplate restTemplate = new RestTemplate();
@Test
public void contextLoads() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String now = format.format(new Date());
System.out.println(now);
}
private String url = "http://localhost:8099/afrishop/discover/bonus/saveNetworkMarketing";
@Autowired
private TbCfOrderRepository orderRepository;
// post35789572fbf643cdab55901d1dc511a6
// userInfo00297bf300ed45e092a5182dececad21
// productSharervv32GmG4q
// amount0.9
// orderId008272b1fb994aef9b199449f46d5aa5
@Test
public void test1() {
Optional<TbCfOrder> byId = orderRepository.findById("12a9d7d1042348278c97980a00fc67c4");
JSONObject json = new JSONObject();
json.put("userInfo", "32dcda5a709c4265a6f01686ae9b1c1c");
json.put("amount", byId.get().getItemsPrice());
json.put("orderId", "12a9d7d1042348278c97980a00fc67c4");
System.out.println(json);
ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity(url, json, Result.class);
System.err.println(resultResponseEntity);
}
@Autowired
private TokenRepository tokenRepository;
@Autowired
private CountryRepository countryRepository;
@Test
public void test2() {
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
list.forEach(System.out::println);
}
private ReentrantLock reentrantLock = new ReentrantLock();
@Test
public void testImport() {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
executorService.execute(() -> importCountry());
}
}
@Test
public void importCountry() {
String url = "https://api.jisuapi.com/country/query?name=&continent=非洲&language=&iscountry=&appkey=732afb9ddae9eef8";
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
String body = forEntity.getBody();
System.err.println(body);
JSONObject jsonObject = JSONObject.fromObject(body);
String status = jsonObject.getString("status");
if ("0".equals(status)) {
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.size(); i++) {
Country country = new Country();
JSONObject object = jsonArray.getJSONObject(i);
String ename = object.getString("ename");
String cname = object.getString("cname");
String areaCode = object.getString("areacode");
String nationalFlag = object.getString("nationalflag");
country.setAreaCode(areaCode);
country.setCountryCname(cname);
country.setCountryName(ename);
country.setNationalFlag(nationalFlag);
country.setId(IdUtil.createIdbyUUID());
countryRepository.save(country);
}
}
}
private volatile int count = 1;
@Autowired
private TbCfStationItemRepository tbCfStationItemRepository;
@Test
public void testImg() {
List<TbCfStationItem> list = tbCfStationItemRepository.queryItemsAll();
try {
for (TbCfStationItem item : list) {
System.out.println("压缩数量:" + count++);
System.err.println("商品ID:" + item.getItemId());
String itemImg = item.getItemImg();
String[] urlArr = itemImg.split(";");
List<String> strList = new ArrayList<>();
for (String url : urlArr) {
InputStream stream = getImageStream(url);
if (stream == null) {
continue;
}
byte[] bytes = new byte[0];
try {
bytes = toByteArray(stream);
byte[] by = PicUtils.compressPicForScale(bytes, 70);
long l = System.currentTimeMillis();
String urlName = UUID.randomUUID() + "-" + String.valueOf(l).substring(6) + ".jpg";
String compressedUrl = OssUtil.upload(by, urlName, "afrishop_new");
strList.add(compressedUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
tbCfStationItemRepository.updateItmeImg(strList.stream().collect(Collectors.joining(";")), item.getItemId());
}
} catch (Exception e) {
} finally {
}
}
/**
* 通过URL获取网络图片
* 获取网络图片流
*
* @param url 传入的 URL 必须是以 http:// 开头的,因为我们使用了 HttpURLConnection
* @return 输入流
*/
public static InputStream getImageStream(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return connection.getInputStream();
}
} catch (IOException e) {
System.out.println("获取网络图片出现异常,图片路径为:" + url);
e.printStackTrace();
}
return null;
}
/**
* 将 InputStream 流转换为 byte[]
*
* @param input InputStream
* @return byte[]
* @throws IOException IO流异常
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论