日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

java ftp上传文件_jaVA使用FTP上传下载文件的问题

發(fā)布時(shí)間:2025/3/15 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java ftp上传文件_jaVA使用FTP上传下载文件的问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為了實(shí)現(xiàn) FTP上傳下載,大概試了兩個(gè)方法

sun.net.ftp.FtpClient

org.apache.commons.net

一開始使用sun.net.ftp.FtpClient,結(jié)果發(fā)現(xiàn)唯一的問題是它不可以創(chuàng)建目錄,

隨后試了下org.apache.commons.net,創(chuàng)建目錄倒是沒有問題,不過用FTPClient 的storeFile方法存儲(chǔ)文件的時(shí)候發(fā)現(xiàn)文本文件正常,但是存儲(chǔ)word 或者是壓縮包等等的文件就會(huì)損壞,由于時(shí)間緊迫,網(wǎng)上也沒有找到有效的解決辦法,結(jié)果最后還是采用sun.net.ftp.FtpClient方法

sun.net.ftp.FtpClient中的sendserver方法可以發(fā)送FTP服務(wù)器的命令,從而可以通過它發(fā)送XMDK命令來(lái)創(chuàng)建目錄

值得注意的是使用sendserver發(fā)送命令后,還應(yīng)該要解析發(fā)回來(lái)的返回信息。并不是一個(gè)命令發(fā)送完后就可以發(fā)送下一個(gè)命令。有時(shí)候要等待到就緒狀態(tài)才行。

使用ftpclient.readServerResponse來(lái)接收服務(wù)器發(fā)回來(lái)的執(zhí)行結(jié)果

附代碼:

sun.net.ftp.FtpClient:

// 連接ftp服務(wù)器

private void connectServer(String server, String user, String password, String path) throws IOException {

ftpClient_sun = new FtpClient();

ftpClient_sun = new FtpClient();

ftpClient_sun.openServer(server,22);

ftpClient_sun.login(user, password);

// path是FTP服務(wù)下主目錄的子目錄

if (path.length() != 0)

ftpClient_sun.cd(path);

ftpClient_sun.sendServer("XMKD test\r\n");

ftpClient_sun.readServerResponse();

ftpClient_sun.cd("test");

// 用2進(jìn)制上傳

ftpClient_sun.binary();

System.out.println("登錄成功");

}

// 上傳文件;并返回上傳文件的信息

private String upload(FormFile formFile) throws Exception {

TelnetOutputStream os = null;

InputStream is = null;

try {

os = ftpClient_sun.put("upftp"+getName()+"." + getExtName(formFile.getFileName()));

is = formFile.getInputStream();

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 "上傳文件成功!";

}

/**

* 下載文件

*

* @param fileName

*??????????? FormFile對(duì)象

* @param HttpServletResponse

*??????????? HTTP響應(yīng)

* @throws IOException

*/

private void download(String fileName, HttpServletResponse response) throws IOException {

TelnetInputStream ftpIn = ftpClient_sun.get(fileName);

response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

OutputStream out = null;

try {

out = response.getOutputStream();

IOUtils.copy(ftpIn, out);

} finally {

if (ftpIn != null) {

ftpIn.close();

}

}

}

/**

*?展示圖片???? *

* @param fileName

*??????????? FormFile對(duì)象

* @param HttpServletResponse

*??????????? HTTP響應(yīng)

* @throws IOException

*/

private void show(String fileName, HttpServletResponse response) throws IOException {

TelnetInputStream ftpIn = ftpClient_sun.get(fileName);

OutputStream out = null;

try {

out = response.getOutputStream();

IOUtils.copy(ftpIn, out);

} finally {

if (ftpIn != null) {

ftpIn.close();

}

}

}

org.apache.commons.net

// 連接ftp服務(wù)器

private void connectServer(String server, String user, String password, String path) throws IOException {

try {

ftpClient = new FTPClient();

ftpClient.connect(server);

ftpClient.login(user, password);

System.out.println(" login success !!! ");

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

boolean flag = ftpClient.changeWorkingDirectory(path);

if (flag) {

System.out.println(" set working directory successful !!! ");

}else

{

ftpClient.makeDirectory(path);

ftpClient.changeWorkingDirectory(path);

}

}

}

catch (IOException e) {

System.out.println(" not login !!! ");

System.out.println(e.getMessage());

}

}

// 上傳文件;并返回上傳文件的信息

private String upload(FormFile formFile) throws Exception {

InputStream fis = formFile.getInputStream();

// 上傳本地文件到服務(wù)器上(文件名以'temp_'開頭,當(dāng)上傳完畢后,名字改為正式名)

boolean flag = ftpClient.storeFile("upftp"+getName()+"."+getExtName(formFile.getFileName()), fis);

if (flag) {

System.out.println(" upload success !!! ");

}

// 關(guān)閉文件流

fis.close();

return "上傳文件成功!";

}

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的java ftp上传文件_jaVA使用FTP上传下载文件的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。