ftp服务器搭建,这是在服务器上搭建的,下面的上传代码功能是在另一台电脑上操作的,相当于是一台电脑往另一台电脑上传图片.

双击它进去

右键网站,添加ftp站点,下面的aaa是我新建好的.

我这个路径选择的是nginx下面的路径,后面上传图片到这个路径下,使用nginx直接就可以访问到了,这儿设置这个路径就相当于是两台电脑直接的共享文件夹.

这个ip是本机ip端口自定义就可以,21是ftp默认端口

这个填上面新创建的用户

就可以看到共享文件夹下面的东西了,如果让一直输入用户名密码的话,就重新回到创建用户那里重置一下密码.

右键编辑权限

上面新建的用户在user组下,把相应的读写权限加上,

上传图片,文件代码

导入依赖,我使用的是springcloud项目,自带MultipartFile包,如果没有这个包的导下就可以了.

       <!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.1</version></dependency><!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3</version></dependency><!-- https://mvnrepository.com/artifact/commons-net/commons-net --><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.3</version></dependency>

创建配置文件

#商品图片地址
commodityImgPath=commodity
#用户图片地址
userImgPath=user
#订单图片地址
orderImgPath=order#ftp相关配置
FTP_ADDRESS=192.168.34.81
FTP_PORT=21
FTP_USERNAME=kxj
FTP_PASSWORD=123456
#下面这个默认不配就相当于是ftp服务器上的这个路径,就是创建ftp的时候选的那个共享路径C:/soft/nginx-1.14.2/nginx-1.14.2/commodity
FTP_BASEPATH=
#图片服务器相关配置,这个是nginx路径,当上传图片成功的时候,拼接一个图片路径的参数
IMAGE_BASE_URL=http://192.168.34.81:8888/

创建一个实体类,注入进来值,方便传参

package com.zy.zs.bean;/*** @author kxj* @version 1.0* @date 2019/8/14 21:15*/import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource(value={"classpath:all.properties"})
public class FTPBean {@Value("${FTP_ADDRESS}")private String FTP_ADDRESS;@Value("${FTP_PORT}")private String FTP_PORT;@Value("${FTP_USERNAME}")private String FTP_USERNAME;@Value("${FTP_PASSWORD}")private String FTP_PASSWORD;@Value("${FTP_BASEPATH}")private String FTP_BASEPATH;@Value("${IMAGE_BASE_URL}")private String IMAGE_BASE_URL;public String getFTP_ADDRESS() {return FTP_ADDRESS;}public void setFTP_ADDRESS(String FTP_ADDRESS) {this.FTP_ADDRESS = FTP_ADDRESS;}public String getFTP_PORT() {return FTP_PORT;}public void setFTP_PORT(String FTP_PORT) {this.FTP_PORT = FTP_PORT;}public String getFTP_USERNAME() {return FTP_USERNAME;}public void setFTP_USERNAME(String FTP_USERNAME) {this.FTP_USERNAME = FTP_USERNAME;}public String getFTP_PASSWORD() {return FTP_PASSWORD;}public void setFTP_PASSWORD(String FTP_PASSWORD) {this.FTP_PASSWORD = FTP_PASSWORD;}public String getFTP_BASEPATH() {return FTP_BASEPATH;}public void setFTP_BASEPATH(String FTP_BASEPATH) {this.FTP_BASEPATH = FTP_BASEPATH;}public String getIMAGE_BASE_URL() {return IMAGE_BASE_URL;}public void setIMAGE_BASE_URL(String IMAGE_BASE_URL) {this.IMAGE_BASE_URL = IMAGE_BASE_URL;}
}

上传图片的工具类

package com.zy.zs.utils;import com.zy.zs.bean.FTPBean;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;import java.io.*;public class FtpUtil {/** * ftp上传图片方法 *title:pictureUpload *@param ftpConfig  由spring管理的FtpConfig配置,在调用本方法时,可以在使用此方法的类中通过@AutoWared注入该属性。由于本方法是静态方法,所以不能在此注入该属性 *@param picNewName 图片新名称--防止重名 例如:"1.jpg" *@param picSavePath 图片保存路径。注:最后访问路径是 ftpConfig.getFTP_ADDRESS()+"/images"+picSavePath *@param file 要上传的文件(图片) *@return 若上传成功,返回图片的访问路径,若上传失败,返回null * @throws*/  public static String pictureUploadByConfig(FTPBean ftpConfig, String picNewName, String picSavePath, InputStream inputStream) throws IOException{String picHttpPath = null;  boolean flag = uploadFile(ftpConfig.getFTP_ADDRESS(), ftpConfig.getFTP_PORT(), ftpConfig.getFTP_USERNAME(),  ftpConfig.getFTP_PASSWORD(), ftpConfig.getFTP_BASEPATH(), picSavePath, picNewName, inputStream);  if(!flag){  return picHttpPath;  }  //picHttpPath = ftpConfig.getFTP_ADDRESS()+"/images"+picSavePath+"/"+picNewName;  picHttpPath = ftpConfig.getIMAGE_BASE_URL()+picSavePath+"/"+picNewName;  System.out.println("==="+picHttpPath);  return picHttpPath;  }  /**   * Description: 向FTP服务器上传文件   * @param host FTP服务器hostname   * @param port FTP服务器端口   * @param username FTP登录账号   * @param password FTP登录密码   * @param basePath FTP服务器基础目录  * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath  * @param filename 上传到FTP服务器上的文件名   * @param input 输入流   * @return 成功返回true,否则返回false   */      public static boolean uploadFile(String host, String ftpPort, String username, String password, String basePath,    String filePath, String filename, InputStream input) {  int port = Integer.parseInt(ftpPort);  boolean result = false;    FTPClient ftp = new FTPClient();try {    int reply;    ftp.connect(host, port);// 连接FTP服务器    // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器    ftp.login(username, password);// 登录    reply = ftp.getReplyCode();    if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();    return result;    }    //切换到上传目录    if (!ftp.changeWorkingDirectory(basePath+filePath)) {    //如果目录不存在创建目录    String[] dirs = filePath.split("/");    String tempPath = basePath;    for (String dir : dirs) {    if (null == dir || "".equals(dir)) continue;    tempPath += "/" + dir;    if (!ftp.changeWorkingDirectory(tempPath)) {    if (!ftp.makeDirectory(tempPath)) {    return result;    } else {    ftp.changeWorkingDirectory(tempPath);    }    }    }    }    //设置上传文件的类型为二进制类型   ftp.setFileType(FTP.BINARY_FILE_TYPE);ftp.enterLocalPassiveMode();//这个设置允许被动连接--访问远程ftp时需要  //上传文件    if (!ftp.storeFile(filename, input)) {    return result;    }    input.close();    ftp.logout();    result = true;    } catch (Exception e) {e.printStackTrace();    } finally {    if (ftp.isConnected()) {    try {    ftp.disconnect();    } catch (IOException ioe) {}    }    }    return result;    }    //下载文件方法不用看,可能日后有用,先留在这里==========================================  /**   * Description: 从FTP服务器下载文件   * @param host FTP服务器hostname   * @param port FTP服务器端口   * @param username FTP登录账号   * @param password FTP登录密码   * @param remotePath FTP服务器上的相对路径   * @param fileName 要下载的文件名   * @param localPath 下载后保存到本地的路径   * @return   */      public static boolean downloadFile(String host, int port, String username, String password, String remotePath,    String fileName, String localPath) {    boolean result = false;    FTPClient ftp = new FTPClient();    try {    int reply;    ftp.connect(host, port);    // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器    ftp.login(username, password);// 登录    reply = ftp.getReplyCode();    if (!FTPReply.isPositiveCompletion(reply)) {    ftp.disconnect();    return result;    }    ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录    FTPFile[] fs = ftp.listFiles();for (FTPFile ff : fs) {    if (ff.getName().equals(fileName)) {    File localFile = new File(localPath + "/" + ff.getName());OutputStream is = new FileOutputStream(localFile);ftp.retrieveFile(ff.getName(), is);    is.close();    }    }    ftp.logout();    result = true;    } catch (IOException e) {    e.printStackTrace();    } finally {    if (ftp.isConnected()) {    try {    ftp.disconnect();    } catch (IOException ioe) {    }    }    }    return result;    }    /**
* Description: 从FTP服务器删除文件
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @return
*/
public boolean delFile(String url, int port,String username, String password, String remotePath) {
boolean success = false; FTPClient ftp = new FTPClient();  try {  int reply; ftp.connect(url, port); //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  ftp.login(username, password);//登录  reply = ftp.getReplyCode();  if (!FTPReply.isPositiveCompletion(reply)) {  ftp.disconnect();  return success;  }  ftp.deleteFile(remotePath); ftp.logout();  success = true;  } catch (IOException e) {  e.printStackTrace();  } finally {  if (ftp.isConnected()) {  try {  ftp.disconnect();  } catch (IOException ioe) {  }  }  }  return success;
}
}  

controller代码

    @RequestMapping("/insertCommodity2")@ResponseBodypublic ResultUtil<String> insertCommodity(HttpSession session,ProductDetails productDetails, MultipartFile productImg, MultipartFile[] productImgs, MultipartFile productDetailImg){String str1 = "null";//上传商品图片ResultUtil<String> stringResultUtil = uploadingImgs(commodityImgPath, productImg);if (stringResultUtil!=null){if (stringResultUtil.isSuccess()){str1 = stringResultUtil.getData();}else{return stringResultUtil;}}ResultUtil<Integer> integerResultUtil = zsCommodityFeign.insertCommodity(str1,str2,str3,productDetails);// ResultUtil<Integer> integerResultUtil = zsCommodityFeign.insertCommodity("1","2","3",productDetails);if (integerResultUtil.isSuccess()){return new ResultUtil<>(true,"添加成功!");}return new ResultUtil<>(false,"添加失败!");}/*** 上传图片操作,返回图片名称.* @param multipartFiles* @return*/public ResultUtil<String> uploadingImgs(String url,MultipartFile... multipartFiles){String str = "";if(multipartFiles!=null&&multipartFiles.length>0){for (MultipartFile multipartFile:multipartFiles){if (!"".equals(multipartFile.getOriginalFilename())){File file = new File(url);if(!file.exists()){   //判断改目录是否存在不存在就创建file.mkdirs();}String oriName = multipartFile.getOriginalFilename();  //获取到上传的时候的图片名String endName = oriName.substring(oriName.lastIndexOf("."));  //获取到后缀名//判断是否为图片
/*                if(!endName.matches(".+(.JPEG|.jpeg|.JPG|.jpg)$")){return new ResultUtil<>(false,"请上传图片!");}*///如果图片超过512k返回false   自己定义/*if(multipartFile.getSize()>512*1024){return new ResultUtil<>(false,"文件不支持>512KB!!");}*///重命名文件名   也可以使用UUIDSimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");String format = simpleDateFormat.format(new Date());//String picName = UUID.randomUUID().toString();  uuid生成的名字String newName = oriName.substring(0,oriName.lastIndexOf("."))+"_"+format+endName;//newName = newName.substring(0,newName.length()-4)+newName.substring(newName.length()-3,newName.length());try {String s = FtpUtil.pictureUploadByConfig(ftpBean, newName, commodityImgPath, multipartFile.getInputStream());//上传到图片服务器的操作str+=s+"-";// multipartFile.transferTo(new File(url+File.separator+newName));  //上传图片} catch (IOException e) {e.printStackTrace();return new ResultUtil<>(false,"上传失败,详细信息为"+e.getMessage());}}else{return null;}}str = str.substring(0,str.length()-1);return new ResultUtil<>(true,str);}else{return null;}}

就是在原来本地上传图片的基础上把上传方法一换就可以了,换成往ftp上传,其余的代码都是自己写的业务逻辑没必要一样.然后运行上传就可以了,上传完成后,会返回一个字符串,就是上传图片的位置,根据配置文件中IMAGE_BASE_URL=http://192.168.34.81:8888/这个参数进行搭配,然后直接访问这个路径下面的图片名就可以访问图片了,然后走自己的业务逻辑就可以了.

win7 ftp安装搭建,并且上传图片到ftp文件夹下,使用nginx访问下载图片相关推荐

  1. matlab安装c盘吗,matlab的安装步骤(附winC盘“用户”文件夹下账户名的更改方法).doc...

    matlab的安装步骤(附winC盘"用户"文件夹下账户名的更改方法) MATLAB 2010的下载和安装 MATLAB R2010a 下载地址:ed2k://|file|[矩陣實 ...

  2. 解决linux使用yum安装新版JDK时,Java文件夹下没有lib、bin等文件,只有jre的问题

    最近在Linux上使用yum安装JDK时,发现/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-0.el8_3.x86_64/文件夹下尽然只有jre文件夹,其 ...

  3. python27和python36 共存时安装pip方法,解决python27文件夹下没有script文件方法

    在官网下载python27后双击压缩包安装,记得要选择把python.exe加到环境变量中.我刚开始选择的文件路径是和python36一样,两个文件夹在同一个文件夹下.安装完成后打开python27文 ...

  4. win7添加ftp到计算机,技术编辑帮你win7系统FTP地址添加到资源管理器的收藏夹下的设置步骤...

    随着电脑的使用率越来越高,我们有时候可能会遇到对win7系统FTP地址添加到资源管理器的收藏夹下进行设置,如果我们需要对win7系统FTP地址添加到资源管理器的收藏夹下进行设置时,要怎么处理win7系 ...

  5. 当使用ftp访问文件夹时,拒绝访问

    当使用ftp访问文件夹时,拒绝访问 问题: 当使用ftp访问文件夹时,拒绝访问,例如\\192.168.10.53\config 解决: 进入config这个文件夹,设置成共享的即可

  6. 无法删除ftp服务器上的文件夹,批处理 FTP上传,后删除本地文件夹(无法删除) 问题...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 @echo off :: 设置FTP服务器地址(仅输入IP)set ftpIP=192.168.7.183 :: 设置FTP用户名set ftpUser= ...

  7. webpack 读取文件夹下的文件_TypeScript完全解读(26课时)_1.TypeScript完全解读-开发环境搭建...

    1.TypeScript完全解读-开发环境搭建 初始化项目 手动创建文件夹 D:\MyDemos\tsDemo\client-demo 用VSCode打开 npm init:初始化项目 然后我们的项目 ...

  8. 「Vue 学习笔记 1」Vue 项目快速搭建,初始项目各个文件夹作用介绍和启动代码执行流程分析

    「Vue 学习笔记 1」Vue 项目快速搭建,初始项目各个文件夹作用介绍和启动代码执行流程分析 前言 一.我的开发环境 二.使用 Vue CLI (Vue 脚手架)快速搭建项目 三.初始项目的目录结构 ...

  9. 移动文件需要计算机管理员权限,win7系统提示“需要管理员权限才能移动文件夹”的解决方法...

    很多小伙伴都遇到过win7系统提示"需要管理员权限才能移动文件夹"的困惑吧,一些朋友看过网上零散的win7系统提示"需要管理员权限才能移动文件夹"的处理方法,并 ...

最新文章

  1. sysctl -p详解
  2. 文件标识符必须为双精度类型的整数值标量_【翻译】VTK官方文档 - vtk文件格式
  3. “懒”的妙用——浅析图片懒加载技术
  4. Flask实战2问答平台-登录限制(装饰器)
  5. charles抓app包教程_charles关于手机APP抓包
  6. Day11多态部分-2-1
  7. vim代码自动补全函数提示设置
  8. C++按位异或运算符
  9. JDBC:数据库操作:处理大对象CLOB数据
  10. Android的双进程守护,广播和双进程
  11. mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享
  12. 设计测试用例需要注意的点
  13. Python基础入门:函数--阿里云天池
  14. 迈普路由器访问控制列表配置命令_迈普路由器配置手册
  15. [MOT学习笔记]JDE损失函数详解
  16. 漫谈程序员系列 薪资,你是我不能言说的伤
  17. 计算机二级考试Python考试内容大纲,二级考试还是很简单的
  18. SQLserver基础--语句、存储过程(七)
  19. python模拟鼠标键盘操作_python3实现复制粘贴 Python-模拟鼠标键盘动作 | 猴头客
  20. 简要分析用MD5加密算法加密信息(如有疑问,敬请留言)

热门文章

  1. 盘古开源:大数据赋能业务运营,自主研发实现新突破
  2. python安装 Autodesk FBX 包
  3. mysql密码为空包密码错误_apk空包签名方法及工具
  4. 关于fi dd ler 手机抓包 网卡地址地址_超详细的网络抓包神器 tcpdump 使用指南
  5. 【论文阅读笔记】Incremental Network Quantizatio:Towards Lossless CNNs with Low-Precision Weights
  6. 主板怎么安装在计算机主机箱,计算机主板、主机、机箱和计算机的制作方法
  7. EFR32--如何在EFR32程序中修改UUID
  8. android自定义UI模板图文详解
  9. 00 | 基础编程题目集题解传送门
  10. 【老生谈算法】matlab实现EKF UKF PF三种算法对比源码——EKF UKF PF算法