-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from hobbit-project/send-to-ftp_v2
Send to FTP
- Loading branch information
Showing
3 changed files
with
130 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/org/hobbit/benchmark/versioning/util/FTPUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.hobbit.benchmark.versioning.util; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.net.ftp.FTPClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FTPUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(FTPUtils.class); | ||
|
||
public static void sendToFtp(String inputDir, String outputDir, String fileExtention) { | ||
FTPClient client = new FTPClient(); | ||
FileInputStream fis = null; | ||
|
||
try { | ||
client.connect("hobbitdata.informatik.uni-leipzig.de"); | ||
LOGGER.info("connected: " + client.sendNoOp()); | ||
client.enterLocalPassiveMode(); | ||
String username = "****"; | ||
String pwd = "****"; | ||
LOGGER.info("login: " + client.login(username, pwd)); | ||
|
||
LOGGER.info(client.printWorkingDirectory()); | ||
// recursively create ftp folders if not exist | ||
boolean dirExists = true; | ||
String[] directories = outputDir.split("/"); | ||
for (String dir : directories ) { | ||
if (!dir.isEmpty() ) { | ||
if (dirExists) { | ||
dirExists = client.changeWorkingDirectory(dir); | ||
} | ||
if (!dirExists) { | ||
if (!client.makeDirectory(dir)) { | ||
throw new IOException("Unable to create remote directory '" + dir + "'. error='" + client.getReplyString()+"'"); | ||
} | ||
if (!client.changeWorkingDirectory(dir)) { | ||
throw new IOException("Unable to change into newly created remote directory '" + dir + "'. error='" + client.getReplyString()+"'"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
LOGGER.info(client.printWorkingDirectory()); | ||
File inputDirFile = new File(inputDir); | ||
List<File> inputFiles = (List<File>) FileUtils.listFiles(inputDirFile, new String[] { fileExtention }, true); | ||
for (File file : inputFiles) { | ||
fis = new FileInputStream(file); | ||
client.deleteFile(file.getAbsolutePath()); | ||
client.storeFile(file.getName(), fis); | ||
} | ||
client.logout(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (fis != null) { | ||
fis.close(); | ||
} | ||
client.disconnect(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |