|
|
@@ -9,6 +9,7 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
|
|
import org.apache.poi.util.IOUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.mock.web.MockMultipartFile; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
@@ -16,7 +17,6 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile; |
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
@@ -228,21 +228,17 @@ public class ImageUtil { |
|
|
|
bytes[i] += 256; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
String newFileName = getNewFileName("jpg"); |
|
|
|
ByteArrayInputStream fileStream = new ByteArrayInputStream(bytes); |
|
|
|
String path = "/tmp"; |
|
|
|
File file = new File(path + "/" + newFileName); |
|
|
|
FileCopyUtils.copy(bytes, file); |
|
|
|
|
|
|
|
/*ByteArrayInputStream fileStream = new ByteArrayInputStream(bytes); |
|
|
|
Map<String,Integer> map = ImageUtil.fixPixel(fileStream, 1000); |
|
|
|
|
|
|
|
Integer h = map.get("h"); |
|
|
|
Integer w = map.get("w"); |
|
|
|
String path = "/tmp"; |
|
|
|
FileCopyUtils.copy(bytes, new File(path + "/" + newFileName)); |
|
|
|
|
|
|
|
String savePath = path + "/compress" + newFileName.split("\\?s=")[0]; |
|
|
|
|
|
|
|
File file = new File(path + "/" + newFileName); |
|
|
|
Thumbnails.of(file).size(w,h).outputQuality(0.7).toFile(savePath); |
|
|
|
Thumbnails.of(file).size(w,h).outputQuality(0.7).toFile(savePath);*/ |
|
|
|
|
|
|
|
return getMultipartFile(file); |
|
|
|
|
|
|
@@ -251,6 +247,31 @@ public class ImageUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public MultipartFile convertToMultipartFile(String base64String) { |
|
|
|
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder(); |
|
|
|
try { |
|
|
|
//Base64解码 |
|
|
|
byte[] bytes = decoder.decode(base64String); |
|
|
|
for (int i = 0; i < bytes.length; ++i) { |
|
|
|
if (bytes[i] < 0) {//调整异常数据 |
|
|
|
bytes[i] += 256; |
|
|
|
} |
|
|
|
} |
|
|
|
String newFileName = getNewFileName("jpg"); |
|
|
|
|
|
|
|
MultipartFile multipartFile = new MockMultipartFile( |
|
|
|
newFileName, // 设置文件名,可以根据实际需要修改 |
|
|
|
newFileName, // 设置原始文件名,可以根据实际需要修改 |
|
|
|
"image/jpeg", // 设置文件类型,可以根据实际需要修改 |
|
|
|
bytes); |
|
|
|
return multipartFile; |
|
|
|
} catch (Exception e) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public static MultipartFile getMultipartFile(File file) { |
|
|
|
FileItem item = new DiskFileItemFactory().createItem("file" |
|
|
|
, MediaType.MULTIPART_FORM_DATA_VALUE |
|
|
|