當前位置:首頁 » 文件管理 » ftplogout

ftplogout

發布時間: 2022-04-16 14:04:05

㈠ android中如何上傳圖片到ftp伺服器

在安卓環境下可以使用,在java環境下也可以使用,已經在Java環境下實現了功能,然後移植到了安卓手機上,其它都是一樣的。

[java] view plain
package com.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FileTool {

/**
* Description: 向FTP伺服器上傳文件
*
* @param url
* FTP伺服器hostname
* @param port
* FTP伺服器埠
* @param username
* FTP登錄賬號
* @param password
* FTP登錄密碼
* @param path
* FTP伺服器保存目錄,是linux下的目錄形式,如/photo/
* @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.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;
}

// 測試
public static void main(String[] args) {

FileInputStream in = null ;
File dir = new File("G://pathnew");
File files[] = dir.listFiles();
if(dir.isDirectory()) {
for(int i=0;i<files.length;i++) {
try {
in = new FileInputStream(files[i]);
boolean flag = uploadFile("17.8.119.77", 21, "android", "android",
"/photo/", "412424123412341234_20130715120334_" + i + ".jpg", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

}
}

以上為java代碼,下面是android代碼。

[java] view plain
package com.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new uploadThread().start();
}

class uploadThread extends Thread {
@Override
public void run() {
FileInputStream in = null ;
File dir = new File("/mnt/sdcard/DCIM/Camera/test/");
File files[] = dir.listFiles();
if(dir.isDirectory()) {
for(int i=0;i<files.length;i++) {
try {
in = new FileInputStream(files[i]);
boolean flag = FileTool.uploadFile("17.8.119.77", 21, "android", "android",
"/", "412424123412341234_20130715120334_" + i + ".jpg", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
}

㈡ 自己寫的java代碼,能向本機ftp伺服器傳輸文件,為什麼不能往阿里雲中自己買的伺服器傳輸文件

阿里雲你看看埠有沒有開放(防火牆) 還有就是阿里雲有個策略組, 在阿里雲後台管理那裡開啟, 需要開戶策略組才能訪問某個埠的

㈢ java 實現ftp上傳如何創建文件夾

這個功能我也剛寫完,不過我也是得益於同行,現在我也把自己的分享給大家,希望能對大家有所幫助,因為自己的項目不涉及到創建文件夾,也僅作分享,不喜勿噴謝謝!

interface:
packagecom.sunline.bank.ftputil;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importorg.apache.commons.net.ftp.FTPClient;

publicinterfaceIFtpUtils{
/**
*ftp登錄
*@paramhostname主機名
*@paramport埠號
*@paramusername用戶名
*@parampassword密碼
*@return
*/
publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword);
/**
*上穿文件
*@paramhostname主機名
*@paramport埠號
*@paramusername用戶名
*@parampassword密碼
*@paramfpathftp路徑
*@paramlocalpath本地路徑
*@paramfileName文件名
*@return
*/
(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName);
/**
*批量下載文件
*@paramhostname
*@paramport
*@paramusername
*@parampassword
*@paramfpath
*@paramlocalpath
*@paramfileName源文件名
*@paramfilenames需要修改成的文件名
*@return
*/
publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames);
/**
*修改文件名
*@paramlocalpath
*@paramfileName源文件名
*@paramfilenames需要修改的文件名
*/
(Stringlocalpath,StringfileName,Stringfilenames);
/**
*關閉流連接、ftp連接
*@paramftpClient
*@parambufferRead
*@parambuffer
*/
publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer);
}

impl:
packagecom.sunline.bank.ftputil;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;
importcommon.Logger;

{
privatestaticLoggerlog=Logger.getLogger(FtpUtilsImpl.class);
FTPClientftpClient=null;
Integerreply=null;

@Override
publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword){
ftpClient=newFTPClient();
try{
ftpClient.connect(hostname,port);
ftpClient.login(username,password);
ftpClient.setControlEncoding("utf-8");
reply=ftpClient.getReplyCode();
ftpClient.setDataTimeout(60000);
ftpClient.setConnectTimeout(60000);
//設置文件類型為二進制(避免解壓縮文件失敗)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//開通數據埠傳輸數據,避免阻塞
ftpClient.enterLocalActiveMode();
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
log.error("連接FTP失敗,用戶名或密碼錯誤");
}else{
log.info("FTP連接成功");
}
}catch(Exceptione){
if(!FTPReply.isPositiveCompletion(reply)){
try{
ftpClient.disconnect();
}catch(IOExceptione1){
log.error("登錄FTP失敗,請檢查FTP相關配置信息是否正確",e1);
}
}
}
returnftpClient;
}

@Override
@SuppressWarnings("resource")
(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName){
booleanflag=false;
ftpClient=loginFtp(hostname,port,username,password);
BufferedInputStreambuffer=null;
try{
buffer=newBufferedInputStream(newFileInputStream(localpath+fileName));
ftpClient.changeWorkingDirectory(fpath);
fileName=newString(fileName.getBytes("utf-8"),ftpClient.DEFAULT_CONTROL_ENCODING);
if(!ftpClient.storeFile(fileName,buffer)){
log.error("上傳失敗");
returnflag;
}
buffer.close();
ftpClient.logout();
flag=true;
returnflag;
}catch(Exceptione){
e.printStackTrace();
}finally{
closeFtpConnection(ftpClient,null,buffer);
log.info("文件上傳成功");
}
returnfalse;
}

@Override
publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames){
ftpClient=loginFtp(hostname,port,username,password);
booleanflag=false;
=null;
if(fpath.startsWith("/")&&fpath.endsWith("/")){
try{
//切換到當前目錄
this.ftpClient.changeWorkingDirectory(fpath);
this.ftpClient.enterLocalActiveMode();
FTPFile[]ftpFiles=this.ftpClient.listFiles();
for(FTPFilefiles:ftpFiles){
if(files.isFile()){
System.out.println("=================="+files.getName());
FilelocalFile=newFile(localpath+"/"+files.getName());
bufferRead=newBufferedOutputStream(newFileOutputStream(localFile));
ftpClient.retrieveFile(files.getName(),bufferRead);
bufferRead.flush();
}
}
ftpClient.logout();
flag=true;

}catch(IOExceptione){
e.printStackTrace();
}finally{
closeFtpConnection(ftpClient,bufferRead,null);
log.info("文件下載成功");
}
}
modifiedLocalFileName(localpath,fileName,filenames);
returnflag;
}

@Override
(Stringlocalpath,StringfileName,Stringfilenames){
Filefile=newFile(localpath);
File[]fileList=file.listFiles();
if(file.exists()){
if(null==fileList||fileList.length==0){
log.error("文件夾是空的");
}else{
for(Filedata:fileList){
Stringorprefix=data.getName().substring(0,data.getName().lastIndexOf("."));
Stringprefix=fileName.substring(0,fileName.lastIndexOf("."));
System.out.println("index==="+orprefix+"prefix==="+prefix);
if(orprefix.contains(prefix)){
booleanf=data.renameTo(newFile(localpath+"/"+filenames));
System.out.println("f============="+f);
}else{
log.error("需要重命名的文件不存在,請檢查。。。");
}
}
}
}
}


@Override
publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer){
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(null!=bufferRead){
try{
bufferRead.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(null!=buffer){
try{
buffer.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}


publicstaticvoidmain(String[]args)throwsIOException{
Stringhostname="xx.xxx.x.xxx";
Integerport=21;
Stringusername="edwftp";
Stringpassword="edwftp";
Stringfpath="/etl/etldata/back/";
StringlocalPath="C:/Users/Administrator/Desktop/ftp下載/";
StringfileName="test.txt";
Stringfilenames="ok.txt";
FtpUtilsImplftp=newFtpUtilsImpl();
/*ftp.modifiedLocalFileName(localPath,fileName,filenames);*/
ftp.downloadFileList(hostname,port,username,password,fpath,localPath,fileName,filenames);
/*ftp.uploadLocalFilesToFtp(hostname,port,username,password,fpath,localPath,fileName);*/
/*ftp.modifiedLocalFileName(localPath);*/
}
}

㈣ FtpClient

當時我用SUN 的FtpClient.get()方法下載文件是有問題的,我推薦你用org.apache.commons.net.ftp.FTPClient下載文件,可以解決中文文件下載問題,你可以去我博客里看看哦:http://hi..com/renliangli/blog/item/6ccb6b3a049d95c9d46225a5.html,文章摘給你吧:
現在就來看下我解決的代碼吧,希望對遇到同樣問題的人有點幫助。

1)把ftp地址中的文件保存到本地的java類源碼

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class Ftp {

/**
* Description: 從FTP伺服器下載文件
* @param ip FTP伺服器的ip地址
* @param port FTP伺服器埠,默認為:21
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param remotePath FTP伺服器上的相對路徑
* @param fileName 要下載的文件名
* @param localPath 下載後保存到本地的路徑
* @return
*/
public static boolean downFile(String ip, int port,String username, String password, String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ip, port);
//下面三行代碼必須要,而且不能改變編碼格式,否則不能正確下載中文文件
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");

//如果採用默認埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}

ftp.changeWorkingDirectory(remotePath);//轉移到FTP伺服器目錄
FTPFile[] fs = ftp.listFiles();

for(int i = 0; i < fs.length; i++){
FTPFile ff = fs[i];
if(ff.getName().equals(fileName)){

File localFile = new File(localPath+File.separator+ff.getName());
//
OutputStream is = new FileOutputStream(localFile);

//注意此處retrieveFile的第一個參數由GBK轉為ISO-8859-1編碼。否則下載後的文件內容為空。
//原因可能是由於aix系統默認的編碼為ISO-8859-1
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), is);
is.close();
}
}

ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Ftp.downFile("10.32.166.144", 21, "test", "test", "/flashfxp", "激活碼.txt", "C:");

}

}

2)將ftp資源以文件流的方式打開,由用戶決定保存在本地何處,程序運行後可以從IE跳出框中打開或者保存的Action代碼,利用Struts1寫的:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.mocha.test;

import java.io.IOException;

import java.io.OutputStream;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DownloadAction extends Action{

/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException{

OutputStream os=null;

try {

os = response.getOutputStream();

response.reset();

downFile("10.32.166.144", 21, "test", "test", "/flashfxp", "激活碼.txt",os,response);

} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
os.close();

} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return null;

}

/**
* Description: 從FTP伺服器下載文件
* @param ip FTP伺服器ip地址
* @param port FTP伺服器埠,默認為21
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param remotePath 附件在FTP伺服器上的絕對路徑
* @param fileName 要下載的文件名
* @param outputStream 輸出流
* @param response
* @return
*/
public static boolean downFile(String ip, int port,String username, String password, String remotePath
,String fileName,OutputStream outputStream,HttpServletResponse response) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ip, port);
//下面三行代碼必須要,而且不能改變編碼格式
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");

//如果採用默認埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}

ftp.changeWorkingDirectory(remotePath);//轉移到FTP伺服器目錄
FTPFile[] fs = ftp.listFiles();

for(int i = 0; i < fs.length; i++){
FTPFile ff = fs[i];
if(ff.getName().equals(fileName)){

String filename = fileName;
//這個就就是彈出下載對話框的關鍵代碼
response.setHeader("Content-disposition",
"attachment;filename=" +
URLEncoder.encode(filename, "utf-8"));
//將文件保存到輸出流outputStream中
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), outputStream);
outputStream.flush();
outputStream.close();
}
}

ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}

差點忘了利用ftpclient要用到的jar包了,呵呵,就這兩個了:commons-net-1.4.1.jar、jakarta-oro-2.0.8.jar

對了我用的jdk是1.4的。所以commons-net-1.4.1.jar用了這個版本比較老的。

㈤ documents不能上傳文件到ftp伺服器

你好!
你得開許可權的,文件夾要配置寫許可權,ftp伺服器端軟體也要開寫入。 我的是小鳥雲伺服器 不懂的都是問客服。希望對你有幫助,望採納!

㈥ Java已經連接到ftp上了 但是不能在ftp上面操作

如果代碼沒有問題 就是許可權

㈦ 你是怎麼解決ftp上傳失敗的

jfileupload applet我之前用的是這個很古老的東西來做文件上傳,遇到這個問題。後來只得換了另外一種做法。你是要做FTP文件上傳嗎?如果是的話像我這樣做,代碼:
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之後文件內容會出錯*/
ftp.setControlEncoding("GBK");
//設置文件類型(二進制)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
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;
}

㈧ java代碼 經已連接成功linux ftp伺服器,但是就是上傳不了文件

你是想從伺服器下載文件吧?那應該用retrieveFile(String remote, OutputStream local),而不是storefile啊,自己看看API
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html

㈨ ftp上去後只剩3個不明文件:.bashrc 和.bash_profile和.bash_logout 裡面別的東西都沒了

用戶家目錄下的配置文件,一般情況下
這3個文件是linux系統裡面的腳本文件
,裝上系統就有!
┏┓ ┏┓┏┓
┃┃┏━┳┳┳━┓ ┃┗┛┣━┳┳┓
┃┗┫┃┃┃┃┻┫ ┗┓┏┫┃┃┃┃
┗━┻━┻━┻━┛ ┗┛┗━┻━┛
【沒有問題請注意採納吧!】
【網路一下——知足而樂!】

㈩ Linux下的FTP伺服器性能怎麼去測試

以前弄了一個FTP的腳本,拿出來大家隨便看看:

ftp1 = 0;
ftp_logon_ex(&ftp1, "FtpLogon",
"URL=ftp://Zee:1234@ZEE",
LAST);

lr_start_transaction("logon");

ftp2 = 0;
ftp_logon_ex(&ftp2, "FtpLogon",
"URL=ftp://Zee:1234@ZEE",
LAST);

ftp_dir_ex(&ftp2, "FtpDir",
"PATH=", "PASSIVE=TRUE", ENDITEM,
LAST);

ftp_dir_ex(&ftp2, "FtpDir",
"PATH=/", "PASSIVE=TRUE", ENDITEM,

LAST);

lr_end_transaction("logon", LR_AUTO);

ftp_get_ex(&ftp2, "Get_Files",

"SOURCE_PATH=/1.txt",
"TARGET_PATH=d:/1.txt",
"MODE=ASCII",
ENDITEM ,
LAST);
ftp_put_ex(&ftp2, "FtpPut",
"SOURCE_PATH=ftpfile_1.dat", "TARGET_PATH=/1.mdb", ENDITEM,
LAST);

ftp_delete_ex(&ftp2, "FtpDelete",
"PATH=/1.txt", ENDITEM,
LAST);

ftp_mkdir_ex(&ftp2, "FtpMakeDir",
"PATH=/新文件夾");

ftp_dir_ex(&ftp2, "FtpDir",
"PATH=/", "PASSIVE=TRUE", ENDITEM,
LAST);

ftp_rendir_ex(&ftp2, "FtpRenDir",
"SOURCE_DIR=/新文件夾", "TARGET_DIR=/2", ENDITEM,
LAST);

ftp_logout_ex(&ftp2);

ftp_logon_ex(&ftp2, "FtpLogon",
"URL=ftp://Zee:1234@ZEE",
LAST);

ftp_dir_ex(&ftp2, "FtpDir",
"PATH=", "PASSIVE=TRUE", ENDITEM,
LAST);

ftp_dir_ex(&ftp2, "FtpDir",
"PATH=/", "PASSIVE=TRUE", ENDITEM,
LAST);

ftp_delete_ex(&ftp2, "FtpDelete",
"PATH=/2.txt", ENDITEM,
LAST);

ftp_logout_ex(&ftp2);

ftp_logout_ex(&ftp1);
zee神寫的

熱點內容
qq刷紅包腳本 發布:2024-05-03 16:16:54 瀏覽:769
c服務編譯耗時優化原理及實例 發布:2024-05-03 15:35:26 瀏覽:15
ue編程 發布:2024-05-03 15:34:40 瀏覽:610
經典的c語言程序 發布:2024-05-03 15:03:24 瀏覽:859
工程加密網 發布:2024-05-03 14:59:55 瀏覽:292
吃冰球解壓 發布:2024-05-03 14:59:10 瀏覽:895
編譯晶元發燙 發布:2024-05-03 14:59:05 瀏覽:549
優化演算法pdf 發布:2024-05-03 14:18:10 瀏覽:291
python演算法書 發布:2024-05-03 14:14:25 瀏覽:736
方舟怎麼加入伺服器閃退 發布:2024-05-03 14:05:27 瀏覽:491