Android FTP上传图片代码
答案:2 悬赏:40 手机版
解决时间 2021-11-23 07:43
- 提问者网友:山高云阔
- 2021-11-22 11:04
Android FTP上传图片代码
最佳答案
- 五星知识达人网友:老鼠爱大米
- 2021-11-22 11:10
android客户端实现FTP文件需要用到 commons-net-3.0.1.jar
public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(url, Integer.parseInt(port));
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
ftpClient.makeDirectory(remotePath);
// 设置上传目录
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
ftpClient.storeFile(fileName, fis);
returnMessage = "1"; //上传成功
} else {// 如果登录失败
returnMessage = "0";
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
//IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}
- 先将jar包复制到android libs目录下
复制以下实现代码
public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(url, Integer.parseInt(port));
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
ftpClient.makeDirectory(remotePath);
// 设置上传目录
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
ftpClient.storeFile(fileName, fis);
returnMessage = "1"; //上传成功
} else {// 如果登录失败
returnMessage = "0";
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
//IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}
全部回答
- 1楼网友:山君与见山
- 2021-11-22 11:32
FTPClient ftpClient = new FTPClient();
public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(url, 21);
boolean loginResult = ftpClient.login(username, password);
System.out.println("loginResult==="+loginResult);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
ftpClient.makeDirectory(remotePath);
// 设置上传目录
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(5000);
// FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
// conf.setServerLanguageCode("zh");
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
// ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
returnMessage = "1"; //上传成功
System.out.println("true");
} else {// 如果登录失败
returnMessage = "0";
System.out.println("false");
}
} catch (IOException e) {
System.out.println("IOException======"+e);
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
//IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯