问题
is there any possible way to write a file to a FTP directory using some sort of OutputStream without having to write a local file first?
I've found some 3rd party libraries which achieve this, but I was wondering if there is some java "standard" class that makes it possible, I mean, some class that is packaged into the standar Java API.
Thank you!!
回答1:
URL url = new URL("ftp://user:pass@ftp.something.com/file.txt;type=i");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream(); // To download
OutputStream os = urlc.getOutputStream(); // To upload
回答2:
- If you should only write/read files is better to use java.net.URL class.
- If seems you should manipulate files/directories via FTP you must use 3rd party library.
The best practice is building abstract layer to not depend from FTP solution.
As for me the best tool for Java FTP is http://www.sauronsoftware.it/projects/ftp4j/
来源:https://stackoverflow.com/questions/12835716/ftpoutputstream-or-similar-in-standard-java