FTP Client with URL
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sdadatatransfer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
/**
*
* @author ferrante
*/
public class FTPConnection2 {
private final MyLog myLog;
public FTPConnection2(MyLog myLog){
this.myLog = myLog;
}
public void downloadFile(String hostpath, String remoteFile, String localFile) throws IOException{
URL url = new URL(hostpath + remoteFile + ";type=i");
URLConnection con = url.openConnection();
try{
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
FileOutputStream out = new FileOutputStream(localFile);
int i = 0;
byte[] bytesIn = new byte[1024];
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
in.close();
}catch(Exception e){
this.myLog.warning("Impossibile ricevere " + remoteFile + ". Forse è una directory");
}
}
public void upload() throws IOException{
URL url = new URL("ftp://user:password@better.altervista.org/ridere/insulti2.htm;type=i");
URLConnection con = url.openConnection();
BufferedOutputStream out = new BufferedOutputStream(con.getOutputStream());
FileInputStream in = new FileInputStream("C:/TMP2/insulti.htm");
int i = 0;
byte[] bytesIn = new byte[1024];
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
in.close();
}
public Vector dirlist(String hostpath) throws IOException{
Vector names = new Vector();
URL url = new URL(hostpath + ";type=i");
URLConnection con = url.openConnection();
InputStreamReader input = new InputStreamReader(con.getInputStream());
BufferedReader reader = new BufferedReader(input);
while(reader.ready()){
String line = reader.readLine();
String type = line.substring(0, 1);
String name = line.substring(62, line.length());
names.add(name);
}
input.close();
return names;
}
public boolean downloadDirectory(String hostpath, String localpath) throws IOException{
boolean chk = true;
Vector names = this.dirlist(hostpath);
for(int i=0;i<names.size();i++){
this.myLog.info("Download del file " + (String)names.get(i));
this.downloadFile(hostpath, (String)names.get(i), localpath + (String)names.get(i));
}
return chk;
}
}