ftp://123.246.52.130/ 这是一个服务器的路径,可以直接定位到文件夹,端口号是21,用户名密码没有,下面我贴出一段代码。
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ftpUpload("123.246.52.130", "21", "anonymous", "anonymous", "F://", "/mnt/sdcard/CameraTest/", "1024_768.jpg");
}
/**
* 通过ftp上传文件
* @param url ftp服务器地址 如: 192.168.1.110
* @param port 端口如 : 21
* @param username 登录名
* @param password 密码
* @param remotePath 上到ftp服务器的磁盘路径
* @param fileNamePath 要上传的文件路径
* @param fileName 要上传的文件名
* @return
*/
//图片或者音频需要设置成:
//ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
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("123.246.52.130", Integer.parseInt("21"));
Log.i("aa", "a");
boolean loginResult = ftpClient.login("anonymous ", "anonymous ");
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
ftpClient.makeDirectory("F://");
// 设置上传目录
ftpClient.changeWorkingDirectory("F://");
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream("/mnt/sdcard/CameraTest/" + "1024_768.jpg");
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包,commons-net-3.3.jar
------解决方案--------------------
ftpClient.changeWorkingDirectory("F://");你看这段代码,我怎么感觉像从ftp上下载了,,,你把FileInputStream看看,,,
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream("/mnt/sdcard/CameraTest/" + "1024_768.jpg");
ftpClient.storeFile(fileName, fis);