|
@@ -0,0 +1,184 @@
|
|
|
+package com.huimv.admin.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 org.springframework.util.StringUtils;
|
|
|
+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;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class UploadImage {
|
|
|
+
|
|
|
+ @Value("${img.basePath}")
|
|
|
+ private String basePath;
|
|
|
+
|
|
|
+ @Value("${img.ip}")
|
|
|
+ private String ip ;
|
|
|
+// private String ip = "36.134.209.211";
|
|
|
+// private String ip = "192.168.0.13";
|
|
|
+
|
|
|
+ @Value("${img.user}")
|
|
|
+ private String user ;
|
|
|
+
|
|
|
+// private String password = "!Hm537e@1";
|
|
|
+
|
|
|
+ @Value("${img.password}")
|
|
|
+ private String password ;
|
|
|
+
|
|
|
+ @Value("${img.port}")
|
|
|
+ public Integer port ;
|
|
|
+
|
|
|
+ @Value("${img.url}")
|
|
|
+ public String url ;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 利用JSch包实现SFTP上传文件
|
|
|
+ * @param bytes 文件字节流
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void sshSftp(byte[] bytes,String path,String fileName) throws Exception{
|
|
|
+
|
|
|
+ // 服务器保存路径
|
|
|
+ String filepath = basePath +path ;
|
|
|
+ Session session = null;
|
|
|
+ Channel channel = null;
|
|
|
+
|
|
|
+ JSch jSch = new JSch();
|
|
|
+
|
|
|
+ if(port <=0){
|
|
|
+ //连接服务器,采用默认端口
|
|
|
+ session = jSch.getSession(user, ip);
|
|
|
+ }else{
|
|
|
+ //采用指定的端口连接服务器
|
|
|
+ session = jSch.getSession(user, ip ,port);
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果服务器连接不上,则抛出异常
|
|
|
+ if (session == null) {
|
|
|
+ throw new Exception("session is null");
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置登陆主机的密码
|
|
|
+ session.setPassword(password);//设置密码
|
|
|
+ //设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
|
+ session.setConfig("userauth.gssapi-with-mic","no");
|
|
|
+ session.setConfig("StrictHostKeyChecking", "no");
|
|
|
+ //设置登陆超时时间
|
|
|
+ session.connect(30000);
|
|
|
+
|
|
|
+ OutputStream outstream = null;
|
|
|
+ try {
|
|
|
+ //创建sftp通信通道
|
|
|
+ channel = (Channel) session.openChannel("sftp");
|
|
|
+ channel.connect(1000);
|
|
|
+ ChannelSftp sftp = (ChannelSftp) channel;
|
|
|
+
|
|
|
+ //进入服务器指定的文件夹
|
|
|
+ sftp.cd(basePath);
|
|
|
+
|
|
|
+ SftpATTRS attrs = null;
|
|
|
+ try {
|
|
|
+ attrs = sftp.stat(filepath);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ }
|
|
|
+ if (attrs == null) {
|
|
|
+ sftp.mkdir(filepath);
|
|
|
+ System.out.println(("创建子目录:" + filepath));
|
|
|
+ }
|
|
|
+ sftp.cd(filepath);
|
|
|
+
|
|
|
+ //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换一下流就可以了
|
|
|
+ outstream = sftp.put(fileName);
|
|
|
+ outstream.write(bytes);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ //关流操作
|
|
|
+ if (outstream != null) {
|
|
|
+ outstream.flush();
|
|
|
+ outstream.close();
|
|
|
+ }
|
|
|
+ if (session != null) {
|
|
|
+ session.disconnect();
|
|
|
+ }
|
|
|
+ if (channel != null) {
|
|
|
+ channel.disconnect();
|
|
|
+ }
|
|
|
+ System.out.println("上传成功!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //上传压缩的图片
|
|
|
+ public String getImageCom(MultipartFile image) throws IOException {
|
|
|
+ //获取文件输入流
|
|
|
+ InputStream inputStream = image.getInputStream();
|
|
|
+ String originalFilename = image.getOriginalFilename();
|
|
|
+ String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
|
|
|
+ String path = DateUtil.format(new Date(), "yyyy-MM");
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 把图片读入到内存中
|
|
|
+ BufferedImage bufImg = ImageIO.read(inputStream);
|
|
|
+ // 压缩代码,存储图片文件byte数组
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ //防止图片变红,这一步非常重要
|
|
|
+ BufferedImage bufferedImage = new BufferedImage(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
|
|
|
+ bufferedImage.createGraphics().drawImage(bufImg,0,0, Color.WHITE,null);
|
|
|
+ //先转成jpg格式来压缩,然后在通过OSS来修改成源文件本来的后缀格式
|
|
|
+ ImageIO.write(bufferedImage,"jpg",bos);
|
|
|
+ byte[] bytes = bos.toByteArray();
|
|
|
+ String imgname = UUID.randomUUID() + "." + filenameExtension;
|
|
|
+ sshSftp(bytes, path, imgname);
|
|
|
+ return url+ path + "/" + imgname;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "上传失败";
|
|
|
+ } finally {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //上传原图
|
|
|
+ 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;
|
|
|
+ sshSftp(content, path, imgname);
|
|
|
+ return url + path + "/" + imgname;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "上传失败";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|