我正在尝试使用以下代码将文件上传到 FTP 服务器:
private void upload( String ftpServer, String user, String password,
String fileName, File source ) throws MalformedURLException,
IOException
{
if (ftpServer != null && fileName != null && source != null){
StringBuffer sb = new StringBuffer( "ftp://" );
if (user != null && password != null){
sb.append( user );
sb.append( ':' );
sb.append( password );
sb.append( '@' );
}
sb.append( ftpServer );
sb.append( '/' );
sb.append( fileName );
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
urlc.setDoOutput(true);
bos = new BufferedOutputStream( urlc.getOutputStream() );
bis = new BufferedInputStream( new FileInputStream( source ) );
int i;
while((i = bis.read()) != -1){
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else{
Log.e("Tag", "Input not available." );
}
}
但是文件并没有在服务器上结束。
最佳答案
我不确定您是否可以通过这种方式与 FTP 服务器通信。 我推荐使用 apache ftp 客户端。
下载地址 http://commons.apache.org/net/download_net.cgi
文档位于 http://commons.apache.org/net/api/org/apache/commons/net/ftp/package-summary.html
关于android - 上传文件到FTP服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871850/