I want to upload directory to my FTP server.
This is the code that i use to upload a "single file" to my ftp server, I want to know if there is something that can help me to upload all pictures ".jpg" in this folder "mnt/sdcard/DCIM/Camera" to my FTP.
public static final String TAG = "Contacts";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t = new Thread(new Runnable(){
@Override
public void run(){
jetzt();
}
});
t.start();
Log.i(TAG, "thread started");
}
public void jetzt(){
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("HOST SERVER IP");
ftpClient.setSoTimeout(10000);
ftpClient.enterLocalPassiveMode();
if(ftpClient.login(" LOGIN ", " PASSWORD "))
{
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
File sFile=new File("mnt/sdcard/DCIM/Camera/mypicture.jpg");
FileInputStream fs= new FileInputStream(sFile);
String fileName = sFile.getName();
Boolean result = ftpClient.storeFile(fileName, fs);
fs.close();
Log.i(TAG, "sent");
String has = "";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
There's no function to upload whole directory in the FTPClient
class .
You have to:
Enumerate all files in a directory.
For that see How to read all files in a folder from Java? or even Recursively list files and directories in an FTP server using Java .
Upload them one by one.
You may use this modified function:
public void jetzt() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("HOST SERVER IP");
ftpClient.setSoTimeout(10000);
ftpClient.enterLocalPassiveMode();
if (ftpClient.login(" LOGIN ", " PASSWORD ")) {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
final File folder = new File("mnt/sdcard/DCIM/Camera");
for (final File fileEntry : folder.listFiles()) {
try {
FileInputStream fs = new FileInputStream(fileEntry);
if (!fileEntry.isDirectory()) {
String fileName = fileEntry.getName();
ftpClient.storeFile(fileName, fs);
fs.close();
Log.i(TAG, "sent");
}
} catch (Exception e) {
Log.i(TAG, "error uploading");
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.