Nginx+SFTP 做图片服务器

    xiaoxiao2021-12-14  18

    安装nginx之前需要安装nginx依赖的软件库.

    1 gcc

    安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gccyum install gcc-c++

    2 PCRE

    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginxhttp模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

    yum install -y pcre pcre-devel

    注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

    3 zlib

    zlib库提供了很多种压缩和解压缩的方式,nginx使用zlibhttp包的内容进行gzip,所以需要在linux上安装zlib库。

    yum install -y zlib zlib-devel

     4 openssl

    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

    yum install -y openssl openssl-devel

    nginx-1.8.0.tar.gz拷贝至linux服务器。

     

    安装步骤

    解压:

    tar -zxvf nginx-1.8.0.tar.gz

    cd nginx-1.8.0

     

    1、 configure

    ./configure --help查询详细参数

     

    参数设置如下:

    ./configure \

    --prefix=/usr/local/nginx \

    --pid-path=/var/run/nginx/nginx.pid \

    --lock-path=/var/lock/nginx.lock \

    --error-log-path=/var/log/nginx/error.log \

    --http-log-path=/var/log/nginx/access.log \

    --with-http_gzip_static_module \

    --http-client-body-temp-path=/var/temp/nginx/client \

    --http-proxy-temp-path=/var/temp/nginx/proxy \

    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

    --http-scgi-temp-path=/var/temp/nginx/scgi

     

    注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建tempnginx目录

    成功截图.

    2 编译安装

    make

    make  install

    安装位置在

    3 启动nginx ,并访问页面.

    5 停止nginx

    方式1,快速停止:

    cd /usr/local/nginx/sbin

    ./nginx -s stop

    此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

     

    方式2,完整停止(建议使用)

    cd /usr/local/nginx/sbin

    ./nginx -s quit

    此方式停止步骤是待nginx进程处理任务完毕进行停止。

     6 重启nginx

    方式1,先停止再启动(建议使用):

    nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

    如下:

    ./nginx -s quit

    ./nginx

     

    方式2,重新加载配置文件:

    nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

    ./nginx -s reload

    SFTP 上传

    需要的jar包为:jsch-0.1.42.jar

    package com.wupao.service.impl.picture; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.jcraft.jsch.ChannelSftp; import com.wupao.service.picture.PictureService; import com.wupao.util.ConstantUtils; import com.wupao.util.IDUtils; import com.wupao.util.MySFTP; /** * 图片上传服务 */ @Service public class PictureServiceImpl implements PictureService { @Override public String uploadPicture(MultipartFile uploadFile) { try { //生成一个新的文件名 //取原始文件名 String oldName = uploadFile.getOriginalFilename(); //生成新文件名 //UUID.randomUUID(); String newName = IDUtils.genImageName(); newName = newName + oldName.substring(oldName.lastIndexOf(".")); //图片上传 SimpleDateFormat myFmt1=new SimpleDateFormat("/yyyy/MM/"); String imagePath = myFmt1.format(new Date()); ChannelSftp channelSftp = MySFTP.getInstance(); MySFTP.upload(ConstantUtils.FILE_UPLOAD_PATH+imagePath, uploadFile, channelSftp,newName); return ConstantUtils.IMAGE_BASE_URL+imagePath+newName; } catch (Exception e) { e.printStackTrace(); } return null; } } package com.wupao.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Properties; import java.util.Vector; import org.omg.CORBA.SystemException; import org.springframework.web.multipart.MultipartFile; import sun.font.CreatedFontTracker; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpATTRS; import com.jcraft.jsch.SftpException; /** * */ public class MySFTP { private static ChannelSftp instance ; public static ChannelSftp getInstance(){ if (instance == null) { String host =ConstantUtils.FTP_SERVER_IP; int port = Integer.parseInt(ConstantUtils.FTP_SERVER_PORT); String username = ConstantUtils.FTP_SERVER_USERNAME; String password = ConstantUtils.FTP_SERVER_PASSWORD; instance = connect(host, port, username, password); } return instance; } /** * 连接sftp服务器 * * @param host * 主机 * @param port * 端口 * @param username * 用户名 * @param password * 密码 * @return */ public static ChannelSftp connect(String host, int port, String username, String password) { // ChannelSftp sftp = null; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); System.out.println("Session created."); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); System.out.println("Session connected."); System.out.println("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); instance = (ChannelSftp) channel; System.out.println("Connected to " + host + "."); } catch (Exception e) { e.printStackTrace(); } return instance; } /** * 上传文件 * * @param directory * 上传的目录 * @param uploadFile * 要上传的文件 * @param sftp * @throws SftpException */ public static void upload(String directory, MultipartFile uploadFile, ChannelSftp sftp,String fileName) throws SftpException { try { createDir(directory, sftp); sftp.cd(directory); sftp.put(uploadFile.getInputStream(), fileName); } catch (Exception e) { e.printStackTrace(); } } /** * 下载文件 * * @param directory * 下载目录 * @param downloadFile * 下载的文件 * @param saveFile * 存在本地的路径 * @param sftp */ public void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) { try { sftp.cd(directory); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件 * * @param directory * 要删除文件所在目录 * @param deleteFile * 要删除的文件 * @param sftp */ public void delete(String directory, String deleteFile, ChannelSftp sftp) { try { sftp.cd(directory); sftp.rm(deleteFile); } catch (Exception e) { e.printStackTrace(); } } /** * 列出目录下的文件 * * @param directory * 要列出的目录 * @param sftp * @return * @throws SftpException */ public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException { return sftp.ls(directory); } /** * 创建一个文件目录 */ public static void createDir(String createpath, ChannelSftp sftp) { try { if (isDirExist(createpath,sftp)) { sftp.cd(createpath); } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path + "/"); if (isDirExist(filePath.toString(),sftp)) { sftp.cd(filePath.toString()); } else { // 建立目录 sftp.mkdir(filePath.toString()); // 进入并设置为当前目录 sftp.cd(filePath.toString()); } } sftp.cd(createpath); } catch (SftpException e) { // throw new SystemException("创建路径错误:" + createpath); e.printStackTrace(); } } /** * 判断目录是否存在 */ public static boolean isDirExist(String directory,ChannelSftp sftp) { boolean isDirExistFlag = false; try { SftpATTRS sftpATTRS = sftp.lstat(directory); isDirExistFlag = true; return sftpATTRS.isDir(); } catch (Exception e) { if (e.getMessage().toLowerCase().equals("no such file")) { isDirExistFlag = false; } } return isDirExistFlag; } public static void main(String[] args) throws SftpException { /* * #文件上传相关配置信息 FTP_SERVER_IP=123.56.200.118 FTP_SERVER_PORT=22 FTP_SERVER_USERNAME=root FTP_SERVER_PASSWORD=zhangkong1@A IMAGE_BASE_URL=1111 FILE_UPLOAD_PATH=/tmp/test */ MySFTP sf = new MySFTP(); String host = "123.56.200.118"; int port = 22; String username = "root"; String password = "zhangkong1@A"; String directory = "/tmp/test/2016/10"; // String uploadFile = "F\\HXMceshi.txt"; // String downloadFile = "PathTest.txt"; // String saveFile = "D:\\tmp\\download.txt"; // String deleteFile = "delete.txt"; ChannelSftp sftp = sf.connect(host, port, username, password); sf.createDir(directory, sftp); // sf.upload(directory, uploadFile, sftp); //上传 // sf.download(directory, downloadFile, saveFile, sftp); //下载 // sf.delete(directory, deleteFile, sftp); //删除 try { sftp.cd(directory); /*Vector content= sftp.ls(directory); for(int i =0; i < content.size();i++){ System.out.println("content的元素:"+i + "----"+content.get(i)); } if (!content.contains("HXMtest")) { sftp.mkdir(directory); System.out.println("创建了文件夹/home/HXMtest"); }*/ System.out.println("finished"); } catch(SftpException sException){ if(sftp.SSH_FX_NO_SUCH_FILE == sException.id){ sftp.mkdir(directory); sftp.cd(directory); System.out.println("SftpException sException"); } } } }

    转载请注明原文地址: https://ju.6miu.com/read-963768.html

    最新回复(0)