This is a continuation of the last thread. I insert 3 files: Two bins (path and a number) and either a jpg or a bmp This does the following.
1: Connects to FTP and changes to C:/ (/)
2: It checks if a webfolder exists. If it is, goes to it. If it isnt, it creates it and goes to it.
3: It checks if a folder with the current date exists. If it exists, it checks if it has more than/equal to 44 files, if it does 3a. If it doesnt, 3b
3a: If 3a, it makes the current date but with a "_2". For example, today the folder would be 20121211 but since it has more than/equal to 44 files, it would be 20121211_2 if it doesnt have more/equal to 44 files, if not continue to 3, 4, etc. It would then change to this folder and continue to 3b
3b: Since it doesnt have less than 44, i go inside the folder. if i am inserting a bmp, i create the bmp inside this folder and then make a new folder with the name being the number and insert the path bin and the number bin. if i am inserting a jpg, i make a new folder with the name being the number, and insert the path bin, the number bin, and the jpg.
4: I then disconect.
The code is big and I dont think Ill translate much. That being said if someone wants me to translate Ill go ahead and do it.
public void subidaftpfecha(String ip, String usuario, String pass, String fichero, String directorio)
{
int counter=1; //Counter so I can add 1, 2, 3, etc to the folder
FTPFile found; //When I find the correct folder
int done=0; //When I finish the while
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); //Date format
Date date = new Date();
String fechaactual=dateFormat.format(date);
fechaactual=fechaactual.replace("/",""); //Removing the slashes just in case
FTPClient client = new FTPClient();
String extension=fichero.substring(fichero.indexOf("."),fichero.indexOf(".")+4); //Extension
try
{
if (directorio.equals(""))
{
directorio=fechaactual;
}
client.connect(ip);
client.login(usuario,pass);
client.setFileType(FTP.BINARY_FILE_TYPE);
String filename = fichero;
File e=new File(filename);
FileInputStream s=new FileInputStream(e);
String cai=fechaactual; //folder I want to use
client.changeWorkingDirectory("/");
if (client.changeWorkingDirectory("webfolder")==false) //Check if webfolder exists
{
client.makeDirectory("webfolder");
client.changeWorkingDirectory("webfolder");
}
FTPFile[] list = client.listFiles();
while (done==0)
{
found=null;
if (counter==1)
{
cai=fechaactual;
}
else
{
cai=fechaactual+"_"+Integer.toString(counter);
}
for (int i=0;i<list.length;i++) //I run thru searching for the folder
{
if ((list[i].getType() == FTPFile.DIRECTORY_TYPE) && (list[i].getName().equals(cai)))
{
found=list[i];
break;
}
}
if (found!=null) //If Ive found the folder Im looking for
{
client.changeWorkingDirectory(found.getName()); //I change to it
FTPFile[] list2 = client.listFiles();
int archivos=0; //number of files
for (int x=0;x<list2.length;x++) //I count the number of files inside of it
{
if (list2[x].getType()==FTPFile.FILE_TYPE)
{
archivos=archivos+1;
}
}
//DEBUG
System.out.println("Working with "+client.printWorkingDirectory()+"/"+e.getName());
System.out.println("Total file: "+archivos);
if (archivos<44) //If it is less than 44
{
if (client.changeWorkingDirectory(directorio)==false) //If It does not exist
{
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
client.setFileType(FTP.BINARY_FILE_TYPE);
if (extension.equals(".jpg")) //If Im inserting a JPG, first....
{
client.storeFile(client.printWorkingDirectory()+"/"+e.getName(), s);
return;
}
client.makeDirectory(directorio);
client.changeWorkingDirectory(directorio);
client.storeFile(client.printWorkingDirectory()+"/"+e.getName(), s);
done=1;
}
}
else //If it exists
{
client.changeToParentDirectory();
//f.changeWorkingDirectory("/webfolder/");
counter=counter+1;
}
}//end if found not null
else //if found equals null
{
client.makeDirectory(cai);
client.changeWorkingDirectory(cai);
if (client.changeWorkingDirectory(directorio)==false)
{
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
client.setFileType(FTP.BINARY_FILE_TYPE);
if (extension.equals(".jpg"))
{
client.storeFile(client.printWorkingDirectory()+"/"+e.getName(), s);
return;
}
client.makeDirectory(directorio);
client.changeWorkingDirectory(directorio);
client.storeFile(client.printWorkingDirectory()+"/"+e.getName(), s);
done=1;
}
}
} //end while
} //end try
catch (SocketException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if (client.isConnected()==true)
{
client.logout();
client.disconnect();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
As you can see this has parameters. First three are simple FTP auth. The variable "fichero" is the file I am inserting (this can be one of the two bins (I call this function at least three times, one more path bin, another for the number bin and another for the jpg or bmp. I have to do these three connections so please bear this in mind)). The variable "directory" is the name of the folder I am inserting this files. Lets keep something in mind: The JPG are not in the actual "directory" I am pointing (reason why the directory can by semioptional). Thats why the JPG has a return because as soon as I insert it, I have no reason to keep going.
Basically I dont know if this is going to work so I need some input on how it looks :)
Thank you very much.