|
@@ -0,0 +1,128 @@
|
|
|
+package com.miaxis.test;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.miaxis.ZzjsApplication;
|
|
|
+import com.miaxis.car.domain.CarBrandInfo;
|
|
|
+import com.miaxis.car.service.ICarBrandInfoService;
|
|
|
+import com.miaxis.common.core.domain.Response;
|
|
|
+import com.miaxis.common.enums.FileUploadTypeEnum;
|
|
|
+import com.miaxis.feign.service.ICarService;
|
|
|
+import com.qcloud.cos.COSClient;
|
|
|
+import com.qcloud.cos.model.ObjectMetadata;
|
|
|
+import com.qcloud.cos.model.PutObjectRequest;
|
|
|
+import com.qcloud.cos.model.PutObjectResult;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wwl
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2021/5/21 11:51
|
|
|
+ */
|
|
|
+@SpringBootTest(classes = ZzjsApplication.class)
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+public class UpdateCarTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICarService carService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICarBrandInfoService carBrandInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private COSClient cosClient;
|
|
|
+
|
|
|
+ private static String bucketName="t1-1305573081";
|
|
|
+ private static String path="https://t1-1305573081.file.myqcloud.com";
|
|
|
+ private static String preffix="t1";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取第三方汽车品牌接口
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void carBrand(){
|
|
|
+ String result = carService.car("brand","0,1,2,3","300");
|
|
|
+ JSONObject jsonString = JSONObject.parseObject(result);
|
|
|
+ List<Map> info = (List<Map>)jsonString.get("info");
|
|
|
+ ArrayList<CarBrandInfo> carBrandInfos = new ArrayList<>();
|
|
|
+ for (Map map : info) {
|
|
|
+ carBrandInfos.add(JSON.parseObject(JSON.toJSONString(map), CarBrandInfo.class));
|
|
|
+ }
|
|
|
+// carBrandInfoService.saveBatch(carBrandInfos);
|
|
|
+ System.out.println(carBrandInfos);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test1() throws Exception {
|
|
|
+
|
|
|
+ List<CarBrandInfo> list = carBrandInfoService.list(new QueryWrapper<CarBrandInfo>().like("img", "car"));
|
|
|
+
|
|
|
+ for (CarBrandInfo carBrandInfo : list) {
|
|
|
+ URL httpUrl = new URL(carBrandInfo.getImg());
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+ conn.setConnectTimeout(5 * 1000);
|
|
|
+ //通过输入流获取图片数据
|
|
|
+ InputStream inStream = conn.getInputStream();
|
|
|
+ //从输入流中获取得到图片的二进制数据
|
|
|
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while( (len=inStream.read(buffer)) != -1 ){
|
|
|
+ outStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ byte[] btImg = outStream.toByteArray();
|
|
|
+ InputStream byteArrayInputStream = new ByteArrayInputStream(btImg);
|
|
|
+ int length = btImg.length;
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况
|
|
|
+ objectMetadata.setContentLength(length);
|
|
|
+ // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
|
|
+ String bucketName = this.bucketName;
|
|
|
+
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
+ int month=cal.get(Calendar.MONTH)+ 1;
|
|
|
+ int day=cal.get(Calendar.DATE);
|
|
|
+
|
|
|
+ //存储的文件名
|
|
|
+ String storagefileName = System.currentTimeMillis() + ".png";
|
|
|
+ // 指定要上传到 COS 上的路径
|
|
|
+ String key = "/"+this.preffix+"/"+FileUploadTypeEnum.CAR_BRAND.getFileType()+"/"+year+"/"+month+"/"+day+"/"+storagefileName;
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, byteArrayInputStream,objectMetadata);
|
|
|
+ PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
+ String fileUrl=this.path + putObjectRequest.getKey();
|
|
|
+ inStream.close();
|
|
|
+ System.out.println("--------------"+fileUrl);
|
|
|
+ System.out.println("--------------"+key);
|
|
|
+
|
|
|
+ //更新汽车品牌logo
|
|
|
+ carBrandInfo.setImg(fileUrl);
|
|
|
+ carBrandInfo.setImgPath(key);
|
|
|
+ carBrandInfoService.updateById(carBrandInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|