|
@@ -1,10 +1,21 @@
|
|
|
package com.huimv.farm.damsubsidy.common.utils;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
import com.jcraft.jsch.*;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.io.OutputStream;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Component
|
|
|
public class UploadImage {
|
|
@@ -28,7 +39,6 @@ public class UploadImage {
|
|
|
*/
|
|
|
public static void sshSftp(byte[] bytes,String path,String fileName) throws Exception{
|
|
|
|
|
|
-
|
|
|
// 服务器保存路径
|
|
|
String filepath = basePath +path ;
|
|
|
Session session = null;
|
|
@@ -100,5 +110,45 @@ public class UploadImage {
|
|
|
System.out.println("上传成功!");
|
|
|
}
|
|
|
}
|
|
|
+ public static byte[] resizeImage(byte[] srcImgData, float reduceMultiple) throws IOException {
|
|
|
+ BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
|
|
|
+ int width = (int) (bi.getWidth() * reduceMultiple); // 源图宽度
|
|
|
+ int height = (int) (bi.getHeight() * reduceMultiple); // 源图高度
|
|
|
+ Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
+ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics g = tag.getGraphics();
|
|
|
+ g.setColor(Color.RED);
|
|
|
+ g.drawImage(image, 0, 0, null); // 绘制处理后的图
|
|
|
+ g.dispose();
|
|
|
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(tag, "JPEG", bOut);
|
|
|
+ return bOut.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String uploadImg( MultipartFile image ) {
|
|
|
+ String originalFilename = image.getOriginalFilename();
|
|
|
+ String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
|
|
|
+ String path = DateUtil.format(new Date(), "yyyy-MM");
|
|
|
+ try {
|
|
|
+ InputStream inputStream = image.getInputStream();
|
|
|
+ BufferedInputStream in = new BufferedInputStream(inputStream);
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
|
|
|
+
|
|
|
+ byte[] temp = new byte[1024];
|
|
|
+ int size = 0;
|
|
|
+ while ((size = in.read(temp)) != -1) {
|
|
|
+ out.write(temp, 0, size);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ byte[] content = out.toByteArray();
|
|
|
+ String imgname = UUID.randomUUID() + "." + filenameExtension;
|
|
|
+ UploadImage.sshSftp(content, path, imgname);
|
|
|
+ return "https://img.ifarmcloud.com/images/" + path + "/" + imgname;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "上传失败";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|