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

专业搭建网站创建网站需要什么条件

专业搭建网站,创建网站需要什么条件,网站建设收费明细,网站托管服务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.mnyf.cn/news/42365.html

相关文章:

  • 百度推广 做网站钓鱼网站制作教程
  • 什么公司网站建设比较好网络推广引流方式
  • 免费做公益网站泰安百度推广公司
  • 自己做购物网站需要什么吉林刷关键词排名优化软件
  • 常州微信网站建设案例搜索 引擎优化
  • android开发工具有哪些关键词优化推广排名多少钱
  • 独立ip做担保网站会被360拦截吗seo排名优化是什么
  • 企业网站备案 网站服务内容搜索量查询百度指数
  • 定制网站开发公司电话外国黄冈网站推广平台
  • 用Wordpress建的网站有营销百度app下载手机版
  • 专门做萝莉视频网站seo免费自学的网站
  • 广水网页定制给你一个网站seo如何做
  • 愿景 做中国最受欢迎的互联网网站seo包年优化
  • 优惠券的网站怎么做seo自学教程seo免费教程
  • 赤峰中国建设招标网站网店seo关键词
  • 公司网站建设高端网站建设网页设计桂林seo顾问
  • 易搜网站建设网络搜索关键词排名
  • 自己搭建网站服务器百度推广销售员好做吗
  • 房地产网站开发商关键词优化营销
  • 网站建设金手指排名专业购物网站
  • 江西企业网站建设站长工具无内鬼放心开车禁止收费
  • 包头seo优化专业网站优化外包
  • 怎么搭建一个博客网站专业放心关键词优化参考价格
  • 高端网站建设高端网站建设专家企业网站建设的步骤
  • 阿里云备案多个网站吗yandex搜索引擎入口
  • 找人建网站搜狗推广登录平台
  • 福州做网站互联网公司地推
  • 天津设计师网站大全短视频seo搜索优化
  • 服装设计网站知乎网络事件营销
  • 网站注册域名位置教你免费申请个人网站