本文整理汇总了Java中org.apache.commons.net.ftp.FTPClient.storeFile方法的典型用法代码示例。如果您正苦于以下问题:Java FTPClient.storeFile方法的具体用法?Java FTPClient.storeFile怎么用?Java FTPClient.storeFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.storeFile方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 上传文件至FTP服务器
*
* @author gaoxianglong
*/
public boolean uploadFile(File file) {
boolean result = false;
FTPClient ftpClient = ftpConnManager.getFTPClient();
if (null == ftpClient || !ftpClient.isConnected()) {
return result;
}
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file.getPath()))) {
boolean storeFile = ftpClient.storeFile(file.getName(), in);
if (storeFile) {
result = true;
log.info("file-->" + file.getPath() + "成功上传至FTP服务器");
}
} catch (Exception e) {
log.error("error", e);
} finally {
disconnect(ftpClient);
}
return result;
}
示例2: upload
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 上传文件
*
* @param remoteDir 上传目录
* @param remoteFileName 上传文件名
* @param localFile 本地文件
* @return 上传结果 true - 上传成功 false - 上传失败
*/
public boolean upload(String remoteDir, String remoteFileName, File localFile) {
FTPClient ftp = null;
try {
ftp = initFtpClient(remoteDir);
if (ftp == null) {
logger.debug("ftp初始化失败");
return false;
}
try (InputStream is = new FileInputStream(localFile)) {
boolean storeRet = ftp.storeFile(remoteFileName, is);
if (!storeRet) {
logger.debug("上传文件失败");
return false;
}
}
return true;
} catch (IOException e) {
logger.error("FTP操作异常", e);
return false;
} finally {
close(ftp);
}
}
示例3: uploadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Upload a single file to FTP server with the provided FTP client object.
*
* @param sourceFilePath
* @param targetFilePath
* @param logPrefix
* @throws IOException
*/
protected void uploadFile(final FTPClient ftpClient, final String sourceFilePath, final String targetFilePath,
final String logPrefix) throws IOException {
log.info(String.format(UPLOAD_FILE, logPrefix, sourceFilePath, targetFilePath));
final File sourceFile = new File(sourceFilePath);
try (final InputStream is = new FileInputStream(sourceFile)) {
ftpClient.changeWorkingDirectory(targetFilePath);
ftpClient.storeFile(sourceFile.getName(), is);
final int replyCode = ftpClient.getReplyCode();
final String replyMessage = ftpClient.getReplyString();
if (isCommandFailed(replyCode)) {
log.error(String.format(UPLOAD_FILE_REPLY, logPrefix, replyMessage));
throw new IOException("Failed to upload file: " + sourceFilePath);
} else {
log.info(String.format(UPLOAD_FILE_REPLY, logPrefix, replyMessage));
}
}
}
示例4: uploadFile
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Description: 向FTP服务器上传文件
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
//设置FTP以2进制传输
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//TODO 读取文件配置判断是否使用主动
ftp.enterLocalPassiveMode();//被动
//ftp.enterLocalActiveMode();//主动
//创建目录
mkDir(path,ftp);
//改变目录
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
示例5: uploadFileToWebApp
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Uploads a file to an Azure web app.
* @param profile the publishing profile for the web app.
* @param fileName the name of the file on server
* @param file the local file
*/
public static void uploadFileToWebApp(PublishingProfile profile, String fileName, InputStream file) {
FTPClient ftpClient = new FTPClient();
String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
String server = ftpUrlSegments[0];
String path = "./site/wwwroot/webapps";
if (fileName.contains("/")) {
int lastslash = fileName.lastIndexOf('/');
path = path + "/" + fileName.substring(0, lastslash);
fileName = fileName.substring(lastslash + 1);
}
try {
ftpClient.connect(server);
ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
for (String segment : path.split("/")) {
if (!ftpClient.changeWorkingDirectory(segment)) {
ftpClient.makeDirectory(segment);
ftpClient.changeWorkingDirectory(segment);
}
}
ftpClient.storeFile(fileName, file);
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
示例6: uploadFileToFunctionApp
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Uploads a file to an Azure function app.
* @param profile the publishing profile for the web app.
* @param fileName the name of the file on server
* @param file the local file
*/
public static void uploadFileToFunctionApp(PublishingProfile profile, String fileName, InputStream file) {
FTPClient ftpClient = new FTPClient();
String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
String server = ftpUrlSegments[0];
String path = "site/wwwroot";
if (fileName.contains("/")) {
int lastslash = fileName.lastIndexOf('/');
path = path + "/" + fileName.substring(0, lastslash);
fileName = fileName.substring(lastslash + 1);
}
try {
ftpClient.connect(server);
ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
for (String segment : path.split("/")) {
if (!ftpClient.changeWorkingDirectory(segment)) {
ftpClient.makeDirectory(segment);
ftpClient.changeWorkingDirectory(segment);
}
}
ftpClient.storeFile(fileName, file);
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: testRenameCase
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Test of rename case ALF-20584
*
*/
public void testRenameCase() throws Exception
{
logger.debug("Start testRenameCase");
FTPClient ftp = connectClient();
String PATH1="testRenameCase";
try
{
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login successful", login);
reply = ftp.cwd("/Alfresco/User*Homes");
assertTrue(FTPReply.isPositiveCompletion(reply));
// Delete the root directory in case it was left over from a previous test run
try
{
ftp.removeDirectory(PATH1);
}
catch (IOException e)
{
// ignore this error
}
// make root directory for this test
boolean success = ftp.makeDirectory(PATH1);
assertTrue("unable to make directory:" + PATH1, success);
ftp.cwd(PATH1);
String FILE1_CONTENT_2="That's how it is says Pooh!";
ftp.storeFile("FileA.txt" , new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
assertTrue("unable to rename", ftp.rename("FileA.txt", "FILEA.TXT"));
}
finally
{
// clean up tree if left over from previous run
ftp.disconnect();
}
}