大家好,我正在开发一个具有将文件上传到 ftp 服务器功能的 android 应用程序我希望我的应用程序将 Html 文件上传到 ftp 服务器我正在使用此代码---请回答。提前谢谢...这里是代码..只有主要代码..
new Thread(new Runnable() {
@Override
public void run() {
String host = "www.ethicstrain.tk";
String user = "user";
String pass = "pass";
String filePath = Environment.getExternalStorageDirectory()+"index.htm";
String uploadPath = "public_html/"+str+"/index.htm"; // str is input from editText.
String filename = "index.html";
StringBuffer sb = new StringBuffer( "ftp://" );
sb.append( user );
sb.append( ':' );
sb.append( pass);
sb.append( '@' );
sb.append( server );
sb.append( '/' );
sb.append( str );
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();
bos = new BufferedOutputStream( urlc.getOutputStream() );
bis = new BufferedInputStream( new FileInputStream(filePath ) );
int i;
// read byte by byte until end of stream
while ((i = bis.read()) != -1)
{
bos.write( i );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}).start();
和 Logcat:
07-05 09:46:28.055: W/System.err(1123): java.io.IOException: Unable to connect to server: null
07-05 09:46:28.055: W/System.err(1123): at libcore.net.url.FtpURLConnection.connect(FtpURLConnection.java:203)
07-05 09:46:28.055: W/System.err(1123): at libcore.net.url.FtpURLConnection.getOutputStream(FtpURLConnection.java:339)
07-05 09:46:28.065: W/System.err(1123): at dolphin.developers.com.facebook1$DownloadFileFromURL$3.run(facebook1.java:382)
07-05 09:46:28.065: W/System.err(1123): at java.lang.Thread.run(Thread.java:856)