获取 Applets OutputStream 抛出异常:出什么问题了? [英] Getting Applets OutputStream throws an exception: What is wrong?
问题描述
我制作了一个小程序,我尝试使用 conn.getOutputStream(); 检索 URLConnection 对象输出流.当我尝试这样做时,我的小程序抛出异常java.net.UnknownServiceException:协议不支持输出.
I have made an applet where I attempt to retrieve a URLConnection objects output stream using conn.getOutputStream();. When I attempt to do this, my applet throws the exception java.net.UnknownServiceException: protocol doesn't support output.
出了什么问题&我怎样才能解决这个问题?这是我一直在处理的一个问题 &我真的很紧张,因为我不明白到底出了什么问题&我该如何解决.
What is going wrong & how can I fix this? This has been a problem I have been dealing with for a while & I am really stressing because I dont understand what exactly is wrong & how I can fix it.
一些重要的背景信息.我打开 &通过打开加载小程序的 HTML 文件来运行我的小程序.小程序加载成功&创建其所有 JComponent.在尝试获取输出流时,我得到了上面提到的异常.
在我的浏览器中运行时显示在我的小程序中的输出:
The Output displayed in my applet when run in my browser:
路径:文件:/C:/Users/Soribo/Desktop/Website/Test/在连接():失败:java.net.UnknownServiceException:协议不支持输出
Path: file:/C:/Users/Soribo/Desktop/Website/Test/ In connect(): Failure: java.net.UnknownServiceException: protocol doesn't support output
我的代码:
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:
URL 不支持写入.
file:
URLs don't support writing to them.
当您的小程序页面位于网络服务器上时,您将拥有一个 http:
URL,该 URL 支持写入 - 但只有在服务器端有人接受请求时才有效(可能是 POST 或 PUT,不知道).
When your applet page is on a webserver, you'll have an http:
URL, which supports writing - but it'll only work if someone on the server-side is there accepting the request (likely POST or PUT, don't know).
这篇关于获取 Applets OutputStream 抛出异常:出什么问题了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!