android中如何上传图片到FTP服务器

android中如何上传图片到FTP服务器,第1张

android客户端实现FTP文件需要用到 commons-net-3.0.1.jar

先将jar包复制到android libs目录下

复制以下实现代码

以下为实现代码:

/**

* 通过ftp上传文件

* @param url ftp服务器地址 如:

* @param port 端口如 :

* @param username 登录名

* @param password 密码

* @param remotePath 上到ftp服务器的磁盘路径

* @param fileNamePath 要上传的文件路径

* @param fileName 要上传的文件名

* @return

*/

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

}

这个有几个不同情况:手机安装ftp客户端,AndFTP是android设备上的一款FTP/SFTP/FTPS客户端软件,可以实现和电脑一样的文件传输方式,直接连接你的空间即可传输。手机没有客户端软件,可以采用中间方式,使激敏弊用网页传输,叫做webftp工具,就是利用网页数据传输的方式,打开webftp网站,输入空间的明族FTP信息连接即可传输文件。注意一点,使用webftp需要在空间后台先设置允拿搭许连接的IP地址,使空间服务器允许webftp连接并向其传输文件。

首先需蠢岩要设置下FTP的配置

1.打开Serv-U控制台,点击“限制和设置”--“为域配置高级FTP命令设置和行为”。

2.在FTP设置中找到OPTS UTF8命令,右兄御击禁用此命令

3.点击下面的“全局属性”。

4.在出来的FTP命令属性选项卡中,“高级选项”羡档岩里,把“对所有收发的路径和文件名使用UFT-8编码”前面的钩去掉!

5.以后再上传中文文件,就不会出现乱码问题啦。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12256997.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存