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

评论

上级 90a99718
......@@ -577,18 +577,18 @@ public class OrderController extends Controller {
// return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Already reviewed this product!");
// }
List<String> urls = new ArrayList<>();
if (comment.getFiles() != null) {
for (MultipartFile file : comment.getFiles()) {
byte[] bytes = PicUtils.compressPicForScale(file.getBytes(), 30);
// ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
String urlName = UUID.randomUUID() + ".jpg";
String url = OssUtil.upload(bytes, urlName, "comment");
urls.add(url);
}
String strUrl = urls.stream().collect(Collectors.joining(";"));
comment.setUrls(strUrl);
}
// List<String> urls = new ArrayList<>();
// if (comment.getFiles() != null) {
// for (MultipartFile file : comment.getFiles()) {
// byte[] bytes = PicUtils.compressPicForScale(file.getBytes(), 30);
//// ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
// String urlName = UUID.randomUUID() + ".jpg";
// String url = OssUtil.upload(bytes, urlName, "comment");
// urls.add(url);
// }
// String strUrl = urls.stream().collect(Collectors.joining(";"));
// comment.setUrls(strUrl);
// }
comment.setItemId(itemId);
comment.setUser(user);
......
......@@ -2,18 +2,21 @@ package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.base.Result;
import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.example.afrishop_v3.models.BASE64DecodedMultipartFile;
import com.example.afrishop_v3.util.HttpClientUtil;
import com.example.afrishop_v3.util.OssUtil;
import com.example.afrishop_v3.util.PicUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
/**
......@@ -39,16 +42,22 @@ public class UploadController {
return result;
}
// @Async
@PostMapping("/uploadFile1")
public Result uploadFile1(@RequestBody String[] strImg) throws Exception {
Result<List<String>> result = new Result<>();
List<String> urlList = new ArrayList<>();
for (int i = 0; i < strImg.length; i++) {
String url = sendFile(strImg[i], api);
System.out.println(url);
// String url = sendFile(strImg[i], api);
byte[] by = BASE64DecodedMultipartFile.base64ToFile(strImg[i]);
byte[] bytes = PicUtils.compressPicForScale(by, 30);
// ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
long l = System.currentTimeMillis();
String urlName = UUID.randomUUID() + "_" + String.valueOf(l).substring(6) + ".jpg";
String url = OssUtil.upload(bytes, urlName, "comment");
urlList.add(url);
}
logger.info("图片url:" + urlList);
result.setData(urlList).setMessage(ResultCodeEnum.SUCCESS.getDesc());
return result;
}
......
package com.example.afrishop_v3.models;
import cn.hutool.core.codec.Base64;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.*;
/**
* @Auther: wudepeng
* @Date: 2020/03/22
* @Description:
*/
public class BASE64DecodedMultipartFile implements MultipartFile {
private final byte[] imgContent;
private final String header;
public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
this.imgContent = imgContent;
this.header = header.split(";")[0];
}
@Override
public String getName() {
return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
}
@Override
public String getOriginalFilename() {
return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
}
@Override
public String getContentType() {
return header.split(":")[1];
}
@Override
public boolean isEmpty() {
return imgContent == null || imgContent.length == 0;
}
@Override
public long getSize() {
return imgContent.length;
}
@Override
public byte[] getBytes() throws IOException {
return imgContent;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(imgContent);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(imgContent);
}
public static MultipartFile base64ToMultipart(String base64) {
try {
String[] baseStrs = base64.split(",");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
b = decoder.decodeBuffer(baseStrs[1]);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
return new BASE64DecodedMultipartFile(b, baseStrs[0]);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
// public static File base64ToFile(String base64) {
// if(base64==null||"".equals(base64)) {
// return null;
// }
// byte[] buff= Base64.decode(base64);
// File file=null;
// FileOutputStream fout=null;
// try {
// file = File.createTempFile("tmp", null);
// fout=new FileOutputStream(file);
// fout.write(buff);
// } catch (IOException e) {
// e.printStackTrace();
// }finally {
// if(fout!=null) {
// try {
// fout.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// return file;
// }
public static byte[] base64ToFile(String base64) {
if (base64 == null || "".equals(base64)) {
return null;
}
byte[] bytes = Base64.decode(base64);
return bytes;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论