documents不能上传文件到ftp服务器
答案:2 悬赏:80 手机版
解决时间 2021-03-11 19:09
- 提问者网友:相思似海深
- 2021-03-11 01:06
documents不能上传文件到ftp服务器
最佳答案
- 五星知识达人网友:玩世
- 2021-03-11 01:29
package com.sinosoft.sepmis.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil {
public static boolean uploadFile(String url, int port, String username, String password, String path,
String filename, InputStream input) throws Exception
{
boolean success = false;
FTPClient ftp = new FTPClient();
try
{
int reply;
// 连接FTP服务器
if (port > -1)
{
ftp.connect(url, port);
}
else
{
ftp.connect(url);
}
// 登录FTP
ftp.login(username, password);
reply = ftp.getReplyCode();
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
}
catch (IOException e)
{
success = false;
throw e;
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException e)
{
throw e;
}
}
}
return success;
}
public static void main(String agrs[]) {
try {
File file = new File("E:\\1.txt");
FileInputStream in = new FileInputStream(file);
//返回true上传成功,否则上传失败
// FtpUtil.uploadFile("192.168.61.209", -22, "instiaci", "instiaci", "/db2home/instiaci/personal/shanhz","2.txt",in);
//“sinopipi/IC/tkk"目录要是已经存在的目录
FtpUtil.uploadFile("192.168.61.104", 22, "administrator", "123456", "/sinopipi/IC","6.txt",in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
说明:所需的jar包是commons-net-2.0.jar
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil {
public static boolean uploadFile(String url, int port, String username, String password, String path,
String filename, InputStream input) throws Exception
{
boolean success = false;
FTPClient ftp = new FTPClient();
try
{
int reply;
// 连接FTP服务器
if (port > -1)
{
ftp.connect(url, port);
}
else
{
ftp.connect(url);
}
// 登录FTP
ftp.login(username, password);
reply = ftp.getReplyCode();
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
}
catch (IOException e)
{
success = false;
throw e;
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException e)
{
throw e;
}
}
}
return success;
}
public static void main(String agrs[]) {
try {
File file = new File("E:\\1.txt");
FileInputStream in = new FileInputStream(file);
//返回true上传成功,否则上传失败
// FtpUtil.uploadFile("192.168.61.209", -22, "instiaci", "instiaci", "/db2home/instiaci/personal/shanhz","2.txt",in);
//“sinopipi/IC/tkk"目录要是已经存在的目录
FtpUtil.uploadFile("192.168.61.104", 22, "administrator", "123456", "/sinopipi/IC","6.txt",in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
说明:所需的jar包是commons-net-2.0.jar
全部回答
- 1楼网友:不想翻身的咸鱼
- 2021-03-11 02:51
在网页浏览器的地址栏中输入,会有提示用户名和密码,输入后,进入ftp服务器,直接讲文件夹中的文件拖入就可以了。推荐使用个开源的filezilla,很小的,5.53m,地址: http://d10xg45o6p6dbl.cloudfront.net/projects/f/filezilla/filezilla_3.2.4.1_win32.zip
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯