nginx+ftp实现图片的上传与访问

根据项目的开发要求,使用ftp实现上传图片,通过nginx搭建图片服务器,即对nginx的简单功能的一种应用。

      关于vsftp和nginx的安装就不在这里详细演示,下面的代码是关于nginx.conf的配置,即将ftp上传的图片路径映射到nginx.conf中。

                             

    下面介绍代码是如何实现上传图片的。

     jar包的引入

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!-- 时间操作组件 -->  
  2.     <dependency>  
  3.         <groupId>joda-time</groupId>  
  4.         <artifactId>joda-time</artifactId>  
  5.   
  6.     </dependency>  
  7.     <!-- Apache工具组件 -->  
  8.     <dependency>  
  9.         <groupId>org.apache.commons</groupId>  
  10.         <artifactId>commons-lang3</artifactId>  
  11.   
  12.     </dependency>  
  13.     <!-- 处理io流的工具 -->  
  14.     <dependency>  
  15.         <groupId>org.apache.commons</groupId>  
  16.         <artifactId>commons-io</artifactId>  
  17.   
  18.     </dependency>  
  19.     <!--tomcat上传到服务器工具  -->  
  20.     <dependency>  
  21.         <groupId>commons-net</groupId>  
  22.         <artifactId>commons-net</artifactId>  
  23.   
  24.     </dependency>  
   代码实现

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public void testFtpClient() throws Exception{  
  2.         //创建ftpClient对象  
  3.         FTPClient ftpClient= new FTPClient();  
  4.         //创建ftp链接,默认是21端口  
  5.         ftpClient.connect("192.168.*.*",21);  
  6.           
  7.         //登录ftp服务器,使用用户名和密码  
  8.         ftpClient.login("ftpuser""**");  
  9.           
  10.         //上传文件  
  11.         //读取本地文件  
  12.         FileInputStream inputStream=new FileInputStream(new File("H:\\04 美丽记忆\\2015鸟巢\\IMG_20150716_010643.JPG"));  
  13.           
  14.         //设置上传的路径  
  15.         ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");  
  16.           
  17.         //修改上传格式  
  18.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
  19.         //第一个参数:服务器端文档名  
  20.         //第二个参数,上传文档的inputStream  
  21.         ftpClient.storeFile("rest.png", inputStream);  
  22.         //关闭链接  
  23.         ftpClient.logout();  
  24.           
  25.           
  26.     }  
     对上传方法的进一步抽取

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class FtpUtil {  
  2.   
  3.     /**  
  4.      * Description: 向FTP服务器上传文件  
  5.      * @param host FTP服务器hostname  
  6.      * @param port FTP服务器端口  
  7.      * @param username FTP登录账号  
  8.      * @param password FTP登录密码  
  9.      * @param basePath FTP服务器基础目录 
  10.      * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath 
  11.      * @param filename 上传到FTP服务器上的文件名  
  12.      * @param input 输入流  
  13.      * @return 成功返回true,否则返回false  
  14.      */    
  15.     public static boolean uploadFile(String host, int port, String username, String password, String basePath,  
  16.             String filePath, String filename, InputStream input) {  
  17.         boolean result = false;  
  18.         FTPClient ftp = new FTPClient();  
  19.           
  20.         try {  
  21.             int reply;  
  22.             ftp.connect(host, port);// 连接FTP服务器  
  23.             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  
  24.             ftp.login(username, password);// 登录  
  25.             reply = ftp.getReplyCode();  
  26.             if (!FTPReply.isPositiveCompletion(reply)) {  
  27.                 ftp.disconnect();  
  28.                 return result;  
  29.             }  
  30.             //切换到上传目录  
  31.             if (!ftp.changeWorkingDirectory(basePath+filePath)) {  
  32.                 //如果目录不存在创建目录  
  33.                 String[] dirs = filePath.split("/");  
  34.                 String tempPath = basePath;  
  35.                 for (String dir : dirs) {  
  36.                     if (null == dir || "".equals(dir)) continue;  
  37.                     tempPath += "/" + dir;  
  38.                     if (!ftp.changeWorkingDirectory(tempPath)) {  
  39.                         if (!ftp.makeDirectory(tempPath)) {  
  40.                             return result;  
  41.                         } else {  
  42.                             ftp.changeWorkingDirectory(tempPath);  
  43.                         }  
  44.                     }  
  45.                 }  
  46.             }  
  47.             //设置上传文件的类型为二进制类型  
  48.             ftp.setFileType(FTP.BINARY_FILE_TYPE);  
  49.             //上传文件  
  50.             if (!ftp.storeFile(filename, input)) {  
  51.                 return result;  
  52.             }  
  53.             input.close();  
  54.             ftp.logout();  
  55.             result = true;  
  56.         } catch (IOException e) {  
  57.             e.printStackTrace();  
  58.         } finally {  
  59.             if (ftp.isConnected()) {  
  60.                 try {  
  61.                     ftp.disconnect();  
  62.                 } catch (IOException ioe) {  
  63.                 }  
  64.             }  
  65.         }  
  66.         return result;  
  67.     }  
  68.       
  69.     /**  
  70.      * Description: 从FTP服务器下载文件  
  71.      * @param host FTP服务器hostname  
  72.      * @param port FTP服务器端口  
  73.      * @param username FTP登录账号  
  74.      * @param password FTP登录密码  
  75.      * @param remotePath FTP服务器上的相对路径  
  76.      * @param fileName 要下载的文件名  
  77.      * @param localPath 下载后保存到本地的路径  
  78.      * @return  
  79.      */    
  80.     public static boolean downloadFile(String host, int port, String username, String password, String remotePath,  
  81.             String fileName, String localPath) {  
  82.         boolean result = false;  
  83.         FTPClient ftp = new FTPClient();  
  84.         try {  
  85.             int reply;  
  86.             ftp.connect(host, port);  
  87.             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  
  88.             ftp.login(username, password);// 登录  
  89.             reply = ftp.getReplyCode();  
  90.             if (!FTPReply.isPositiveCompletion(reply)) {  
  91.                 ftp.disconnect();  
  92.                 return result;  
  93.             }  
  94.             ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录  
  95.             FTPFile[] fs = ftp.listFiles();  
  96.             for (FTPFile ff : fs) {  
  97.                 if (ff.getName().equals(fileName)) {  
  98.                     File localFile = new File(localPath + "/" + ff.getName());  
  99.   
  100.                     OutputStream is = new FileOutputStream(localFile);  
  101.                     ftp.retrieveFile(ff.getName(), is);  
  102.                     is.close();  
  103.                 }  
  104.             }  
  105.   
  106.             ftp.logout();  
  107.             result = true;  
  108.         } catch (IOException e) {  
  109.             e.printStackTrace();  
  110.         } finally {  
  111.             if (ftp.isConnected()) {  
  112.                 try {  
  113.                     ftp.disconnect();  
  114.                 } catch (IOException ioe) {  
  115.                 }  
  116.             }  
  117.         }  
  118.         return result;  
  119.     }  
  120. }  
      上传图片的功能很普遍,也根据这个功能的实现学习了如何抽取工具类的方法,代码一步步的优化。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值