当前位置: 首页 > news >正文

花店商城网站设计自动优化app

花店商城网站设计,自动优化app,属于c2c的网站是,做yy头像的网站FTP服务之Java操作FTP服务器下载文件的两种方式 文章目录 FTP服务之Java操作FTP服务器下载文件的两种方式1. 使用Apache commons-net工具包1. 引入commons-net依赖2. 操作案例1. 单文件下载2. 切换到指定目录批量下载文件 2. 使用Hutool工具1. 引入依赖2. 操作案例1. 文件下载 …

FTP服务之Java操作FTP服务器下载文件的两种方式

文章目录

  • FTP服务之Java操作FTP服务器下载文件的两种方式
  • 1. 使用Apache commons-net工具包
    • 1. 引入commons-net依赖
    • 2. 操作案例
      • 1. 单文件下载
      • 2. 切换到指定目录批量下载文件
  • 2. 使用Hutool工具
    • 1. 引入依赖
    • 2. 操作案例
      • 1. 文件下载

注意:如果fpt服务中没有建立目录, 则默认文件目录为根目录也即/,否则按具体目录进行操作,如: /demo
FTP服务搭建查看博文 FTP服务之WindowsServer2019中搭建私有FTP服务器

1. 使用Apache commons-net工具包

1. 引入commons-net依赖

<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.10.0</version></dependency>

2. 操作案例

1. 单文件下载

@Testpublic void downLoadOne() {String server = "192.168.31.252";int port = 21;String user = "anonymous";String password = "";String remoteFile = "/demo/xxx说明书.pdf";String localFile = "F:\\ftpDownlaod\\newAAA.pdf";FTPClient ftpClient = new FTPClient();OutputStream outputStream = null;try {ftpClient.connect(server, port);ftpClient.login(user, password);ftpClient.enterLocalPassiveMode();outputStream = Files.newOutputStream(Paths.get(localFile));// ftp默认使用ISO-8859-1编码格式,所以这里需要转换为ISO-8859-1,“解决文件名为中文时,下载后为空文件的问题”String remoteFileName = new String(remoteFile.getBytes("GBK"), StandardCharsets.ISO_8859_1);ftpClient.retrieveFile(remoteFileName, outputStream);} catch (IOException ex) {System.out.println("DownLoad Error: " + ex.getMessage());ex.printStackTrace();} finally {try {if (outputStream != null) {outputStream.close();}ftpClient.disconnect();} catch (IOException ex) {ex.printStackTrace();}}}

2. 切换到指定目录批量下载文件

   @Testpublic void batchDownLoadFileFromFtp() {FTPClient client = new FTPClient();try {//设置主机与端口client.connect("192.168.31.252", 21);//设置用户名及密码,这里以匿名用户登录为例,根据需求改为自己的用户名及密码client.login("anonymous", "");System.out.println("FTP服务器文件编码===>>" + client.getControlEncoding());int reply = client.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {client.disconnect();System.out.println("Login Error,Please check if your username or password is correct");return;}client.setControlEncoding("GBK");System.out.println("设置后的文件编码:" + client.getControlEncoding());client.enterLocalPassiveMode();//切换到demo目录下client.changeWorkingDirectory("demo");System.out.println("---------------------------------------");String[] names;names = client.listNames();for (String name : names) {System.out.println(name);}System.out.println("ftp服务中,demo目录中的所有文件:" + Arrays.toString(names));System.out.println("---------------------------------------");FTPFile f = client.listFiles()[0];System.out.println("getLink===>" + f.getLink());//切换到根目录下client.changeWorkingDirectory("/");String path = "/demo";client.setBufferSize(1024);client.setFileType(FTP.BINARY_FILE_TYPE);client.enterLocalPassiveMode();//切换到demo目录下获取此目录中所有的文件,并进行一个下载client.changeWorkingDirectory(path);FTPFile[] fs = client.listFiles();for (FTPFile ff : fs) {String outFileName = ff.getName();System.out.println(outFileName);//本地目录文件不需要编码File localFile = new File("F:\\ftpDownlaod\\" + ff.getName());OutputStream fos = Files.newOutputStream(localFile.toPath());// ftp默认使用ISO-8859-1编码格式,所以这里需要转换为ISO-8859-1,“解决文件名为中文时,下载后为空文件的问题”String localFileName = new String(ff.getName().getBytes("GBK"), StandardCharsets.ISO_8859_1);client.retrieveFile(localFileName, fos);fos.close();}} catch (Exception e) {e.printStackTrace();} finally {try {client.disconnect();} catch (IOException e) {e.printStackTrace();}}}

2. 使用Hutool工具

Hutool对FTP客户端基于Apache Commons Net做了进一步的封装。

文档地址:扩展(Hutool-extra) - FTP封装-Ftp

1. 引入依赖

<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.10.0</version></dependency>             
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.23</version></dependency>

2. 操作案例

1. 文件下载

目前存在的问题: 如果文件名称是中文,则下载后的文件大小为0


@Testpublic void ftpServerTestByAnonymousOne() {Ftp ftp = new Ftp("192.168.31.252", 21);String downLoadPath = "/demo";String fileName = "demo.pdf";String outputPath = "F:\\ftpDownlaod";ftp.download(downLoadPath, fileName, new File(outputPath));//关闭连接try {ftp.close();} catch (IOException e) {throw new RuntimeException(e);}//FtpUtil.downloadFile(host, port, user, password, remotePath, localPath);}@Testpublic void ftpServerTestByAnonymousTwo() throws UnsupportedEncodingException {Ftp ftp = new Ftp("192.168.31.252", 21, "anonymous", "", StandardCharsets.UTF_8);String downLoadPath = "/demo";String fileName = "数据迁移最佳实践.pdf";//String remoteFileName = new String(fileName.getBytes("utf-8"),"ISO-8859-1");String outputPath = "F:\\ftpDownlaod\\xxx.pdf";ftp.download(downLoadPath, fileName, new File(outputPath));//关闭连接try {ftp.close();} catch (IOException e) {throw new RuntimeException(e);}//FtpUtil.downloadFile(host, port, user, password, remotePath, localPath);}
http://www.hkea.cn/news/473387/

相关文章:

  • 房地产网站建设方案百度实名认证
  • 做外贸可以在哪些网站注册网络项目免费的资源网
  • 中国建设银行信用卡网站首页青岛关键词优化平台
  • 阿里云网站建设考试题目长沙网站推广服务公司
  • 甘肃建设项目审批权限网站俄罗斯搜索引擎yandex官网入口
  • 网站建设公司新员工培训ppt模板百度热门搜索排行榜
  • 仿魔客吧网站模板网址大全是ie浏览器吗
  • 网站产品后台界面怎么做湖南关键词排名推广
  • 网站数据每隔几秒切换怎么做的湖南百度seo排名点击软件
  • 网站制作先学什么百度新闻下载安装
  • 河南省网站建设哪家好免费观看行情软件网站进入
  • 粘合剂东莞网站建设体育热点新闻
  • 百度网站排名关键词整站优化培训网站建设
  • 网络平台代理seo外包 杭州
  • 东方头条网站源码免费推广软件工具
  • 北京网站建设公司分享网站改版注意事项流程优化四个方法
  • 案例学 网页设计与网站建设手机百度seo快速排名
  • 江门网站建设总部电话产品推广渠道有哪些
  • 网站建设全攻略站长之家ping检测
  • 导航网站 cmsgoogle chrome谷歌浏览器
  • wordpress看其他人博客优化师是做什么的
  • 现在哪个网站还做白拿2021小说排行榜百度风云榜
  • 网站流量seo提升seo排名的方法
  • 做html网站模板下载地址网站页面布局和样式设计
  • 公司网站邮箱费用磁力宅在线搜种子
  • wordpress 缺少临时文件夹刷关键词优化排名
  • 做网站要有什么团队淘宝关键词排名查询工具
  • 开源门户网站源码宁波谷歌seo
  • wordpress+一页一屏seo关键技术有哪些
  • 学校校园网站建设实施方案精准营销的案例