【发布时间】:2011-02-12 01:37:35
【问题描述】:
我制作了一个小程序,我尝试使用 conn.getOutputStream(); 检索 URLConnection 对象输出流。当我尝试这样做时,我的小程序抛出异常 java.net.UnknownServiceException: 协议不支持输出。
出了什么问题?我该如何解决?这是我一直在处理的一个问题,我真的很紧张,因为我不明白到底出了什么问题以及如何解决它。
一些重要的背景信息。我通过打开加载小程序的 HTML 文件来打开并运行我的小程序。小程序成功加载并创建其所有 JComponent。在尝试获取输出流时,我得到了上面提到的异常。
在我的浏览器中运行时在我的小程序中显示的输出:
路径:file:/C:/Users/Soribo/Desktop/Website/Test/ 在 connect() 中:失败:java.net.UnknownServiceException:协议不支持输出
我的代码:
public class TestApplet extends JApplet
{
JTextArea displayTf;
public TestApplet()
{
}
public void init()
{
try
{
SwingUtilities.invokeAndWait( new Runnable() {
public void run()
{
initComponents();
connect();
}
});
}
catch (InterruptedException e) { e.printStackTrace(); }
catch (InvocationTargetException e) { e.printStackTrace(); }
}
public void stop() {}
public void destroy() {}
public void start() {}
public void initComponents()
{
JPanel mainPanel = (JPanel) getContentPane();
displayTf = new JTextArea( "" );
mainPanel.add( displayTf );
}
public void connect()
{
try
{
displayTf.setText( displayTf.getText() + "\nPath: " + getCodeBase() ); // In the browser it displays 'file:/c:/.../TestApplet/bin'
URL servletUrl = new URL( getCodeBase(), "TestApplet" ); // My applet's class file name is TestApplet.class
URLConnection conn = servletUrl.openConnection();
conn.setDoInput( true );
conn.setDoOutput( true );
conn.setUseCaches( false );
conn.setDefaultUseCaches (false);
conn.setRequestProperty ("Content-Type", "application/octet-stream"); // Set the content type to indicate that we're sending binary data
OutputStream out = conn.getOutputStream(); // EXCEPTION thrown here java.net.UnknownServiceException: protocol doesn't support output
// Some tests I have done
// conn.setRequestProperty( "Content-Type", "application/x-java-serialized-object" );
// conn.setRequestProperty("Authorization", "Basic " + encode("uidPassword"));
// System.setProperty("http.proxyHost", "proxy.example.com");
// System.setProperty("http.proxyPort", "8080");
}
catch ( IOException e )
{
displayTf.setText( displayTf.getText() + "\nIn connect(): Failure: " + e );
}
}
【问题讨论】:
-
不应该是
file://吗?