Java urlconnection未将文件上载到ftp服务器
大家好,我正在开发一个android应用程序,它具有将文件上载到ftp服务器的功能我希望我的应用程序将Html文件上载到ftp服务器我正在使用此代码--- 请回答。Thanx提前。。。 代码在这里。。只有主代码Java urlconnection未将文件上载到ftp服务器,java,android,ftp,android-edittext,urlconnection,Java,Android,Ftp,Android Edittext,Urlconnection,大家好,我正在开发一个android应用程序,它具有将文件上载到ftp服务器的功能我希望我的应用程序将Html文件上载到ftp服务器我正在使用此代码--- 请回答。Thanx提前。。。 代码在这里。。只有主代码 new Thread(new Runnable() { @Override public void run() { String host = "www.ethicstrain.tk";
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)
这条线是怎么回事
sb.append( server );
服务器变量来自哪里。如果未设置,则u将获得异常。我猜你是从一个教程中抄来的,忘了修改了。您应该使用您的主机变量,但不使用“www.”
哦,刚刚注意到logcat说:
07-05 09:46:28.065: W/System.err(1123): at dolphin.developers.com.facebook1$DownloadFileFromURL$3.run(facebook1.java:382)
您是否尝试访问facebook,可能是因为错误发生在您尚未显示的其他位置?尝试更改此行
sb.append(服务器)代码>到sb.append(主机)代码>
此外,user和pass-它们是您试图访问的ftp服务器的真实用户名和密码吗
还有,您如何使用filePath和uploadPath?仅供参考,您没有将它们添加到StringBuilder对象
使用如下硬编码url尝试下载-
URL url = new URL( "ftp://youruser:yourpass@www.ethicstrain.tk/yourfile");
URLConnection urlc = url.openConnection();
查看这篇文章,了解更多有关其工作原理的帮助-
希望这有帮助。此地址下是否有可访问的ftp服务器?maby防火墙阻止了它,或者它在另一个端口下运行?是的,我现在修复了,但它会给我相同的错误,谢谢你anwereingno i facebook是我的类名,它从我的服务器下载facebook钓鱼页面,然后将其上载到我在editText列中提到的文件夹。download filefromurl是一个异步任务我不知道错误来自哪里从…起我猜它是服务器连接的附件。您的应用程序无法访问服务器或类似的东西。我正在使用有效的用户名和密码尝试此操作,并且我已更改了sb.append(服务器);附属于某人(主人);试着像下面那样努力编码你的ftp url,并检查你是否成功。URL URL=新URL(“ftp://”);我尝试过,但不起作用。无论如何,谢谢回答。我想你正在使用的某个字段无法访问。我建议你尝试更正硬编码的URL。我尝试过,但不起作用,并给出与我使用此URL=新URL(“myusername:password@www.ethicstrain.tk/“+str+”类型=i“;