Java 程序无法在 Solaris 上使用 ftp。
我已经编写了简单的 Java 应用程序来使用 FTP。它在 Windows 和 Linux 上运行良好,但在 Solaris 上挂起。源代码如下:
import java.io.*;
import java.net.*;
public class ftp_try {
public static void main(String[] args) {
final String hlpurl = "ftp://<user>:<pass>@host/../../some_path/some_file";
try {
URL url = new URL(hlpurl);
final URLConnection con = url.openConnection();
final int remoteSize = con.getContentLength();
System.out.println("connection: " + con.toString());
System.out.println("remote size:" + remoteSize);
System.out.println("closing connection");
con.getInputStream().close();
System.out.println("connection closed");
System.in.read();
}
catch ( MalformedURLException exp) {
System.out.println("1");
}
catch (IOException exp) {
System.out.println(2);
}
}
}
之后挂起
问题是,在 Solaris 上,我的程序在System.out.println("close connection");
。你能帮我找出为什么会发生这种情况吗?
PS:JDK是java 6 SE u 23
更新:如果它对那些面临同样问题的人有任何帮助。我发现了一些解决方法: 从连接中读取所有可用字节,然后您可以关闭它。 像这样:
byte[] buffer= new byte [con.getInputStream().available()];
con.getInputStream().read(buffer);
I`ve written simple java application for working with FTP. It works fine on Windows and Linux, but hangs on Solaris. Here is the source code:
import java.io.*;
import java.net.*;
public class ftp_try {
public static void main(String[] args) {
final String hlpurl = "ftp://<user>:<pass>@host/../../some_path/some_file";
try {
URL url = new URL(hlpurl);
final URLConnection con = url.openConnection();
final int remoteSize = con.getContentLength();
System.out.println("connection: " + con.toString());
System.out.println("remote size:" + remoteSize);
System.out.println("closing connection");
con.getInputStream().close();
System.out.println("connection closed");
System.in.read();
}
catch ( MalformedURLException exp) {
System.out.println("1");
}
catch (IOException exp) {
System.out.println(2);
}
}
}
The problem is that on Solaris my program hangs up after
System.out.println("closing connection");
.
Would you help me to find out why is it happening?
PS: JDK is java 6 SE u 23
Update: if it would be any help for those who face the same problem. I`ve found little workaround:
read all available bytes from connection and then you can close it.
Like this:
byte[] buffer= new byte [con.getInputStream().available()];
con.getInputStream().read(buffer);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您通过在此处读取标准输入来锁定程序:
如果您按任意键,程序将完成
You are locking your program by reading standard input here:
If you press any key the program will be completed