欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java代碼模擬上傳 java上傳和下載

怎么用Java實現(xiàn)FTP上傳

sun.net..,該類庫主要提供了用于建立FTP連接的類。利用這些類的方法,編程人員可以遠程登錄到FTP服務(wù)器,列舉該服務(wù)器上的目錄,設(shè)置傳輸協(xié)議,以及傳送文件。FtpClient類涵蓋了幾乎所有FTP的功能,F(xiàn)tpClient的實例變量保存了有關(guān)建立"代理"的各種信息。下面給出了這些實例變量:

成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、前鋒網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)電子商務(wù)商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為前鋒等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

public static boolean useFtpProxy

這個變量用于表明FTP傳輸過程中是否使用了一個代理,因此,它實際上是一個標(biāo)記,此標(biāo)記若為TRUE,表明使用了一個代理主機。

public static String ftpProxyHost

此變量只有在變量useFtpProxy為TRUE時才有效,用于保存代理主機名。

public static int ftpProxyPort此變量只有在變量useFtpProxy為TRUE時才有效,用于保存代理主機的端口地址。

FtpClient有三種不同形式的構(gòu)造函數(shù),如下所示:

1、public FtpClient(String hostname,int port)

 此構(gòu)造函數(shù)利用給出的主機名和端口號建立一條FTP連接。

2、public FtpClient(String hostname)

此構(gòu)造函數(shù)利用給出的主機名建立一條FTP連接,使用默認端口號。

3、FtpClient()

此構(gòu)造函數(shù)將創(chuàng)建一FtpClient類,但不建立FTP連接。這時,F(xiàn)TP連接可以用openServer方法建立。

一旦建立了類FtpClient,就可以用這個類的方法來打開與FTP服務(wù)器的連接。類ftpClient提供了如下兩個可用于打開與FTP服務(wù)器之間的連接的方法。

public void openServer(String hostname)

這個方法用于建立一條與指定主機上的FTP服務(wù)器的連接,使用默認端口號。

public void openServer(String host,int port)

這個方法用于建立一條與指定主機、指定端口上的FTP服務(wù)器的連接。

打開連接之后,接下來的工作是注冊到FTP服務(wù)器。這時需要利用下面的方法。

public void login(String username,String password)

此方法利用參數(shù)username和password登錄到FTP服務(wù)器。使用過Intemet的用戶應(yīng)該知道,匿名FTP服務(wù)器的登錄用戶名為anonymous,密碼一般用自己的電子郵件地址。

下面是FtpClient類所提供的一些控制命令。

public void cd(String remoteDirectory):該命令用于把遠程系統(tǒng)上的目錄切換到參數(shù)remoteDirectory所指定的目錄。

public void cdUp():該命令用于把遠程系統(tǒng)上的目錄切換到上一級目錄。

public String pwd():該命令可顯示遠程系統(tǒng)上的目錄狀態(tài)。

public void binary():該命令可把傳輸格式設(shè)置為二進制格式。

public void ascii():該命令可把傳輸協(xié)議設(shè)置為ASCII碼格式。

public void rename(String string,String string1):該命令可對遠程系統(tǒng)上的目錄或者文件進行重命名操作。

除了上述方法外,類FtpClient還提供了可用于傳遞并檢索目錄清單和文件的若干方法。這些方法返回的是可供讀或?qū)懙妮斎?、輸出流。下面是其中一些主要的方法?/p>

public TelnetInputStream list()

返回與遠程機器上當(dāng)前目錄相對應(yīng)的輸入流。

public TelnetInputStream get(String filename)

獲取遠程機器上的文件filename,借助TelnetInputStream把該文件傳送到本地。

public TelnetOutputStream put(String filename)

以寫方式打開一輸出流,通過這一輸出流把文件filename傳送到遠程計算機

package myUtil;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;

import sun.net.TelnetInputStream;

import sun.net.TelnetOutputStream;

import sun.net.;

/**

* ftp上傳,下載

*

* @author why 2009-07-30

*

*/

public class FtpUtil {

private String ip = "";

private String username = "";

private String password = "";

private int port = -1;

private String path = "";

FtpClient ftpClient = null;

OutputStream os = null;

FileInputStream is = null;

public FtpUtil(String serverIP, String username, String password) {

this.ip = serverIP;

this.username = username;

this.password = password;

}

public FtpUtil(String serverIP, int port, String username, String password) {

this.ip = serverIP;

this.username = username;

this.password = password;

this.port = port;

}

/**

* 連接ftp服務(wù)器

*

* @throws IOException

*/

public boolean connectServer() {

ftpClient = new FtpClient();

try {

if (this.port != -1) {

ftpClient.openServer(this.ip, this.port);

} else {

ftpClient.openServer(this.ip);

}

ftpClient.login(this.username, this.password);

if (this.path.length() != 0) {

ftpClient.cd(this.path);// path是ftp服務(wù)下主目錄的子目錄

}

ftpClient.binary();// 用2進制上傳、下載

System.out.println("已登錄到\"" + ftpClient.pwd() + "\"目錄");

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

/**

* 斷開與ftp服務(wù)器連接

*

* @throws IOException

*/

public boolean closeServer() {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

if (ftpClient != null) {

ftpClient.closeServer();

}

System.out.println("已從服務(wù)器斷開");

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

/**

* 檢查文件夾在當(dāng)前目錄下是否存在

*

* @param dir

*@return

*/

private boolean isDirExist(String dir) {

String pwd = "";

try {

pwd = ftpClient.pwd();

ftpClient.cd(dir);

ftpClient.cd(pwd);

} catch (Exception e) {

return false;

}

return true;

}

/**

* 在當(dāng)前目錄下創(chuàng)建文件夾

*

* @param dir

* @return

* @throws Exception

*/

private boolean createDir(String dir) {

try {

ftpClient.ascii();

StringTokenizer s = new StringTokenizer(dir, "/"); // sign

s.countTokens();

String pathName = ftpClient.pwd();

while (s.hasMoreElements()) {

pathName = pathName + "/" + (String) s.nextElement();

try {

ftpClient.sendServer("MKD " + pathName + "\r\n");

} catch (Exception e) {

e = null;

return false;

}

ftpClient.readServerResponse();

}

ftpClient.binary();

return true;

} catch (IOException e1) {

e1.printStackTrace();

return false;

}

}

/**

* ftp上傳 如果服務(wù)器段已存在名為filename的文件夾,該文件夾中與要上傳的文件夾中同名的文件將被替換

*

* @param filename

* 要上傳的文件(或文件夾)名

* @return

* @throws Exception

*/

public boolean upload(String filename) {

String newname = "";

if (filename.indexOf("/") -1) {

newname = filename.substring(filename.lastIndexOf("/") + 1);

} else {

newname = filename;

}

return upload(filename, newname);

}

/**

* ftp上傳 如果服務(wù)器段已存在名為newName的文件夾,該文件夾中與要上傳的文件夾中同名的文件將被替換

*

* @param fileName

* 要上傳的文件(或文件夾)名

* @param newName

* 服務(wù)器段要生成的文件(或文件夾)名

* @return

*/

public boolean upload(String fileName, String newName) {

try {

String savefilename = new String(fileName.getBytes("GBK"),

"GBK");

File file_in = new File(savefilename);// 打開本地待長傳的文件

if (!file_in.exists()) {

throw new Exception("此文件或文件夾[" + file_in.getName() + "]有誤或不存在!");

}

if (file_in.isDirectory()) {

upload(file_in.getPath(), newName, ftpClient.pwd());

} else {

uploadFile(file_in.getPath(), newName);

}

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

return true;

} catch (Exception e) {

e.printStackTrace();

System.err.println("Exception e in Ftp upload(): " + e.toString());

return false;

} finally {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 真正用于上傳的方法

*

* @param fileName

* @param newName

* @param path

* @throws Exception

*/

private void upload(String fileName, String newName, String path)

throws Exception {

String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");

File file_in = new File(savefilename);// 打開本地待長傳的文件

if (!file_in.exists()) {

throw new Exception("此文件或文件夾[" + file_in.getName() + "]有誤或不存在!");

}

if (file_in.isDirectory()) {

if (!isDirExist(newName)) {

createDir(newName);

}

ftpClient.cd(newName);

File sourceFile[] = file_in.listFiles();

for (int i = 0; i sourceFile.length; i++) {

if (!sourceFile[i].exists()) {

continue;

}

if (sourceFile[i].isDirectory()) {

this.upload(sourceFile[i].getPath(), sourceFile[i]

.getName(), path + "/" + newName);

} else {

this.uploadFile(sourceFile[i].getPath(), sourceFile[i]

.getName());

}

}

} else {

uploadFile(file_in.getPath(), newName);

}

ftpClient.cd(path);

}

/**

* upload 上傳文件

*

* @param filename

* 要上傳的文件名

* @param newname

* 上傳后的新文件名

* @return -1 文件不存在 =0 成功上傳,返回文件的大小

* @throws Exception

*/

public long uploadFile(String filename, String newname) throws Exception {

long result = 0;

TelnetOutputStream os = null;

FileInputStream is = null;

try {

java.io.File file_in = new java.io.File(filename);

if (!file_in.exists())

return -1;

os = ftpClient.put(newname);

result = file_in.length();

is = new FileInputStream(file_in);

byte[] bytes = new byte[1024];

int c;

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c);

}

} finally {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

}

return result;

}

/**

* 從ftp下載文件到本地

*

* @param filename

* 服務(wù)器上的文件名

* @param newfilename

* 本地生成的文件名

* @return

* @throws Exception

*/

public long downloadFile(String filename, String newfilename) {

long result = 0;

TelnetInputStream is = null;

FileOutputStream os = null;

try {

is = ftpClient.get(filename);

java.io.File outfile = new java.io.File(newfilename);

os = new FileOutputStream(outfile);

byte[] bytes = new byte[1024];

int c;

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c);

result = result + c;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return result;

}

/**

* 取得相對于當(dāng)前連接目錄的某個目錄下所有文件列表

*

* @param path

* @return

*/

public List getFileList(String path) {

List list = new ArrayList();

DataInputStream dis;

try {

dis = new DataInputStream(ftpClient.nameList(this.path + path));

String filename = "";

while ((filename = dis.readLine()) != null) {

list.add(filename);

}

} catch (IOException e) {

e.printStackTrace();

}

return list;

}

public static void main(String[] args) {

FtpUtil ftp = new FtpUtil("192.168.11.11", "111", "1111");

;

boolean result = ("C:/Documents and Settings/ipanel/桌面/java/Hibernate_HQL.docx", "amuse/audioTest/music/Hibernate_HQL.docx");

System.out.println(result ? "上傳成功!" : "上傳失?。?);

;

/**

* FTP遠程命令列表 USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT PASS PASV STOR

* REST CWD STAT RMD XCUP OPTS ACCT TYPE APPE RNFR XCWD HELP XRMD STOU

* AUTH REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ QUIT MODE SYST ABOR

* NLST MKD XPWD MDTM PROT

* 在服務(wù)器上執(zhí)行命令,如果用sendServer來執(zhí)行遠程命令(不能執(zhí)行本地FTP命令)的話,所有FTP命令都要加上\r\n

* ftpclient.sendServer("XMKD /test/bb\r\n"); //執(zhí)行服務(wù)器上的FTP命令

* ftpclient.readServerResponse一定要在sendServer后調(diào)用

* nameList("/test")獲取指目錄下的文件列表 XMKD建立目錄,當(dāng)目錄存在的情況下再次創(chuàng)建目錄時報錯 XRMD刪除目錄

* DELE刪除文件

*/

}

}

java實現(xiàn)文件上傳,代碼盡量簡潔~~~~~·

你說的2種方法都是很簡單的,參考網(wǎng)上的資料都不難做出,用io流做更是基礎(chǔ)中的基礎(chǔ),我說下smartupload好了,有的人是直接寫在jsp上面,感覺比較亂,我一般都是寫在action里面,打好jar包和配置后

SmartUpload mySmartUpload = new SmartUpload();

//如果是struts2.0或者webwork 則是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());

mySmartUpload.initialize(servlet.getServletConfig(), request,response);

mySmartUpload.setTotalMaxFileSize(500000);

//如果上傳任意文件不設(shè)置mySmartUpload.setAllowedFilesList(文件后綴名)就可以了

mySmartUpload.upload();

for (int i = 0; i mySmartUpload.getFiles().getCount(); i++) {

com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);

if (file.isMissing()) continue;

file.saveAs(保存的地址 + file.getFileName(),

su.SAVE_PHYSICAL);

如何用java代碼實現(xiàn)ftp文件上傳

import java.io.File;

import java.io.FileInputStream;

import org.apache.commons.net.;

import org.apache.commons.net.;

public class test {

private FTPClient ftp;

/**

*

* @param path 上傳到ftp服務(wù)器哪個路徑下

* @param addr 地址

* @param port 端口號

* @param username 用戶名

* @param password 密碼

* @return

* @throws Exception

*/

private boolean connect(String path,String addr,int port,String username,String password) throws Exception {

boolean result = false;

ftp = new FTPClient();

int reply;

;

;

;

reply = ;

if (!FTPReply.isPositiveCompletion(reply)) {

;

return result;

}

;

result = true;

return result;

}

/**

*

* @param file 上傳的文件或文件夾

* @throws Exception

*/

private void upload(File file) throws Exception{

if(file.isDirectory()){

(file.getName());

(file.getName());

String[] files = file.list();

for (int i = 0; i files.length; i++) {

File file1 = new File(file.getPath()+"\\"+files[i] );

if(file1.isDirectory()){

upload(file1);

;

}else{

File file2 = new File(file.getPath()+"\\"+files[i]);

當(dāng)前題目:java代碼模擬上傳 java上傳和下載
當(dāng)前鏈接:http://chinadenli.net/article46/hpejeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航響應(yīng)式網(wǎng)站、微信公眾號Google、品牌網(wǎng)站制作網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)