當前位置: 首頁>>代碼示例>>Java>>正文


Java FTPClient.storeFile方法代碼示例

本文整理匯總了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;
}
 
開發者ID:yunjiweidian,項目名稱:TITAN,代碼行數:25,代碼來源:FtpUtils.java

示例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);
    }
}
 
開發者ID:wyp0596,項目名稱:elegant-springboot,代碼行數:32,代碼來源:MyFtpClient.java

示例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));
        }
    }
}
 
開發者ID:Microsoft,項目名稱:azure-maven-plugins,代碼行數:27,代碼來源:FTPUploader.java

示例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;
}
 
開發者ID:Xvms,項目名稱:xvms,代碼行數:50,代碼來源:FtpClient.java

示例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();
    }
}
 
開發者ID:Azure-Samples,項目名稱:acr-java-manage-azure-container-registry,代碼行數:33,代碼來源:Utils.java

示例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();
    }
}
 
開發者ID:Azure-Samples,項目名稱:acr-java-manage-azure-container-registry,代碼行數:33,代碼來源:Utils.java

示例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();
    }    


}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:61,代碼來源:FTPServerTest.java


注:本文中的org.apache.commons.net.ftp.FTPClient.storeFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。