使用 Java 上传到 FTP

编程入门 行业动态 更新时间:2024-10-22 23:28:23
本文介绍了使用 Java 上传到 FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是想知道是否有一种简单方法可以将小文件上传到 ftp 服务器.我已经查看了 Apache Commons Net 库,但老实说这似乎相当复杂.有没有更简单的方法可以把小文件上传到ftp?

I was just wondering if there was a simple way I could upload a small file to a ftp server. I've checked out Apache Commons Net library but that seems quite complicated to be honest. Is there any simpler way to upload a small file to ftp?

最终使用了 Apache Commons Net Library,并不太难.

Ended up using the Apache Commons Net Library, wasn't too hard.

推荐答案

来自这个链接:使用 URLConnection 类上传文件到 FTP 服务器.无需外部库.

From this link: Upload files to FTP server using URLConnection class. No external library necessary.

String ftpUrl = "ftp://%s:%s@%s/%s;type=i"; String host = "www.myserver"; String user = "tom"; String pass = "secret"; String filePath = "E:/Work/Project.zip"; String uploadPath = "/MyProjects/archive/Project.zip"; ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath); System.out.println("Upload URL: " + ftpUrl); try { URL url = new URL(ftpUrl); URLConnection conn = url.openConnection(); OutputStream outputStream = conn.getOutputStream(); FileInputStream inputStream = new FileInputStream(filePath); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } inputStream.close(); outputStream.close(); System.out.println("File uploaded"); } catch (IOException ex) { ex.printStackTrace(); }

更多推荐

使用 Java 上传到 FTP

本文发布于:2023-11-10 00:54:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!