业务逻辑
- 前端将网络图片地址发过来我们将起下载下来
- 将图片转成BufferedImage调整大小,这里是等比例调整
- 将file转成MultipartFile上传到腾讯云
- 将腾讯云地址返回
工具类
package com.zrt.demo.service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
public class ConvertToMultipartFile implements MultipartFile {
private byte[] fileBytes;
String name;
String originalFilename;
String contentType;
boolean isEmpty;
long size;
public ConvertToMultipartFile(byte[] fileBytes, String name, String originalFilename, String contentType,
long size) {
this.fileBytes = fileBytes;
this.name = name;
this.originalFilename = originalFilename;
this.contentType = contentType;
this.size = size;
this.isEmpty = false;
}
@Override
public String getName() {
return name;
}
@Override
public String getOriginalFilename() {
return originalFilename;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public boolean isEmpty() {
return isEmpty;
}
@Override
public long getSize() {
return size;
}
@Override
public byte[] getBytes() throws IOException {
return fileBytes;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(fileBytes);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(fileBytes);
}
}
控制层代码
package com.zrt.demo.controller;
import com.zrt.demo.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: zqf
* @Date: 2021/12/11/20:46
* @Description:
*/
@RestController
@RequestMapping("/image")
public class ImageController {
@Autowired
private ImageService imageService;
@GetMapping("/upload")
public String imageUpload(@RequestParam("url") String url,
@RequestParam("widht") Integer widht,
@RequestParam("height") Integer height) {
return imageService.iamgeUpload(url,widht,height);
}
}
(COS的工具类看我另一篇文章)
业务层代码
package com.zrt.demo.service;
import com.zrt.demo.config.TDCOS.CosUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
/**
* @Author: zqf
* @Date: 2021/12/11/20:50
* @Description:
*/
@Service
@Slf4j
public class ImageService {
private final String BUCKET_FILE_PATH = "my-cos/one";
/**
* 将网络图片改变大小后上传到存到cos上面去
* @param imageUrl 网络图片的地址
* @param widht 要修改的图片的宽度
* @param height 要修改的图片的高度
* @return cos返回的地址
*/
public String iamgeUpload(String imageUrl,Integer widht,Integer height) {
ByteArrayOutputStream out = null;
try {
//获取图片的连接和BufferedImage对象(BufferedImage是操作图片的一个类,可以改变图片的大小)
//(这个地方,老大说用连接池,暂时没改)
URL url = new URL(imageUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(5 * 1000);
BufferedImage image = ImageIO.read(url);
//这里相当于先给一个初始化的大小为我们需要改变大小的一个架子,设置类型为RGB
BufferedImage tag = new BufferedImage(widht, height, BufferedImage.TYPE_INT_RGB);
//操作我们获取的图片对象
tag.getGraphics().drawImage(image.getScaledInstance(widht,height, Image.SCALE_SMOOTH),0,0,null);
//获取字节输出流和图片输出流
out = new ByteArrayOutputStream();
ImageIO.write(tag,"png",out);
//ByteArrayOutputStream 转化为 byte[]
byte[] imageByte = out.toByteArray();
//将 byte[] 转为 MultipartFile
MultipartFile multipartFile = new ConvertToMultipartFile(imageByte, "my_image", "png", "png", imageByte.length);
//上传到腾讯云cos,返回图片url
return CosUtils.uploadFile(multipartFile,BUCKET_FILE_PATH);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
有问题请大家提出来,或者能整合那个连接池的告诉我一下。