获取 Applet OutputStream 抛出异常:出了什么问题?
我制作了一个小程序,尝试使用 conn.getOutputStream(); 检索 URLConnection 对象输出流。当我尝试执行此操作时,我的小程序抛出异常 java.net.UnknownServiceException:协议不支持输出。
出了什么问题&我该如何解决这个问题?这是我已经处理了一段时间的问题&我真的很紧张,因为我不明白到底出了什么问题&我该如何解决它。
一些重要的背景信息。我打开&通过打开加载小程序的 HTML 文件来运行我的小程序。小程序加载成功&创建其所有 JComponent。在尝试获取输出流时,我收到了上面提到的异常。
在浏览器中运行时,小程序中显示的输出:
路径:文件:/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 );
}
}
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.
Some important background information. I open & run my applet by opening a HTML file that loads the applet. The applet loads successfully & creates all its JComponents. Upon attempting to get the output stream I get the exception I mentioned above.
The Output displayed in my applet when run in my browser:
Path: file:/C:/Users/Soribo/Desktop/Website/Test/
In connect(): Failure: java.net.UnknownServiceException: protocol doesn't support output
My code:
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 );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
file:
URL 不支持写入。当您的小程序页面位于网络服务器上时,您将有一个
http:
URL,它支持写入 - 但只有在服务器端有人接受请求(可能是 POST或 PUT,不知道)。file:
URLs don't support writing to them.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).就像 Paŭlo Ebermann 所说,问题在于 Url 类型:如果文件类型在运行时是一个文件,那么您将能够在 Stream 中读取或写入。显然,您尝试使用 URL 中的 OutputStream。
我遇到了同样的问题,经过一番压力,我明白了原因:Applet 类(在您的情况下为“TestApplet.class”)驻留在您的 web 应用程序和 web 服务器的范围之外。在运行时,Url 被解析为文件(“ file://" )而不是 Web 应用程序资源或页面("http://")。
我的解决方案是将我的 Applet 类移动到 Web-Content 目录内部(与包含的 html 文件处于同一级别),以便将其视为 Web 应用程序资源,然后能够发送一些 servlet 请求/响应,其中包含您的输出/输入流。
只需将您的 Applet 类移动到 Web-Content 目录,然后启动您的 Servlet ,而不是从文件系统(url: file://),而是从
来自您的网络服务器(网址:http://)
like Paŭlo Ebermann said , the problem lies on the Url type: if the file type is ,at runtime , a file , you'll write be able to read or writer in Stream. And Obviously you try to use an OutputStream from your Url.
I had the same Problem, and after some stress, i understood the cause: the Applet class (in your case "TestApplet.class" ) resides outside the reach of your webapp and webserver.At Runtime the Url is resolved as a file ("file://" ) instead of an web-app ressources or page ( "http://").
My solution was to move my Applet class inside (at the same level of the containing html file) of the Web-Content directory so that it will be seen as a Web-apps ressources, and then be able to send some servlet request/response which will have your Output/InputStream.
just move your Applet class to the Web-Content Directory, and start your Servlet , not from the file System (url: file://), but rather
from you webserver (url : http://)