赞
踩
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency>
- /**
- * @description:服务器常量类
- */
- public class ServerConstant {
- // 端口号
- public static final int PORT = 22;
- // 用户名
- public static final String USER = "root";
- // 密码
- public static final String PASSWORD = "xingzhou";
- // ip地址
- public static final String IP = "192.168.168.121";
- // 服务器保存路径
- public static final String FILEPATH = "../usr/image/";
- // 服务器访问域名(没有域名可使用ip地址访问)
- public static final String RETURN_URL = "https://xz.xingzhou.com.cn/image/";
- }
- /**
- * @description:上传图片工具类
- */
- public class UploadUtils{
- /**
- * SFTP上传图片
- * @param bytes 图片字节流
- * @param fileName 图片名
- * @throws Exception
- * @return
- */
- public static String sftpImages(byte[] bytes, String fileName) throws Exception{
- String state = null;
- Session session = null;
- Channel channel = null;
- JSch jSch = new JSch();
- if(ServerConstant.PORT <=0){
- //连接服务器,默认端口
- session = jSch.getSession(ServerConstant.USER, ServerConstant.IP);
- }else{
- //连接服务器我们可以指定root用户、ip、端口号来上传图片
- session = jSch.getSession(ServerConstant.USER, ServerConstant.IP ,ServerConstant.PORT);
- }
- //session等于null说明服务器没连接,则抛出异常
- if (session == null) {
- throw new Exception("session is null");
- }
- //设置服务器登陆的密码
- session.setPassword(ServerConstant.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(ServerConstant.FILEPATH);
-
- //上传一个文件到服务器
- outstream = sftp.put(fileName);
- outstream.write(bytes);
- //访问路径http://192.168.168.121/image/拼接上图片名称.jpg
- state = ServerConstant.RETURN_URL + fileName;
- System.out.println("上传图片成功");
- } catch (Exception e) {
- e.printStackTrace();
- state = "上传失败";
- } finally {
- //关闭流
- if (outstream != null) {
- outstream.flush();
- outstream.close();
- }
- //断开服务器
- if (session != null) {
- session.disconnect();
- }
- //关闭信道
- if (channel != null) {
- channel.disconnect();
- }
- }
- return state;
- }
-
- /**
- * 将图片转换成字节流
- * @param images
- * @return
- * @throws Exception
- */
- public static byte[] imagesBytes(MultipartFile images) throws Exception {
- //将图片转换成文件名
- FileInputStream file = new FileInputStream(new File(imgSrc.getOriginalFilename()));
- //从一个输入流中读取一定数量的字节,并将这些字节存储到其缓冲作用的数组bytes中。
- byte[] bytes = new byte[file.available()];
- file.read(bytes);
- file.close();
- return bytes;
- }
- }
- /**
- * 上传图片
- * @param images
- * @return
- */
- @PostMapping("/uploadImage")
- public RespDTO upload(@RequestParam("images") MultipartFile images) {
- RespDTO respDTO = new RespDTO();
- String succeed = null;
- try {
- // 将图片转字节流方法
- byte[] bytes = UploadImage.image2Bytes(images);
- // 调用上传图片方法
- succeed = UploadImage.sshSftp(bytes, UUIDUtil.getUUID()+".jpg");
- respDTO.setObject(succeed);
- } catch (Exception e) {
- e.printStackTrace();
- respDTO.failed(succeed);
- }
- return respDTO;
- }
- public class Test {
-
- public static void main(String[] args) {
- //从本地路径取图片测试
- File file = new File("D:\\jpg\\hehua.jpg");
- try {
- //直接把方法接收的参数改成File
- byte[] bytes = UploadImage.image2Bytes(file);
- String str = UploadImage.sshSftp(bytes, UUIDUtil.getUUID() + ".jpg");
- System.out.println(str);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
注意:配置完记得要重启nginx才能生效。重启命令:systemctl restart nginx
创作不易,动动你发财的小手,赞有余香!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。