入门小程序的OutputStream抛出异常:什么是错的? [英] Getting Applets OutputStream throws an exception: What is wrong?
问题描述
我做了一个小程序,我试图找回一个URLConnection对象的输出流的使用的 conn.getOutputStream(); 的。当我尝试这样做,我的小程序抛出异常的 java.net.UnknownServiceException:协议不支持输出的
这是怎么回事错误和放大器;我该如何解决这一问题?这一直是我一直在处理了一段时间与放大器的问题;我真的讲,因为我不明白究竟是什么错&安培;我怎样才能解决这个问题。
一些重要的背景信息。我打开和放大器;通过打开加载一个小程序的HTML文件运行我的小程序。小程序加载成功和放大器;创建所有JComponent上。在试图获得输出流,我得到我上面提到的例外。
在我的浏览器中运行时,在我的小程序将显示输出:
路径:文件:/ C:/用户/ Soribo /桌面/网站/测试/
在Connect():失败:java.net.UnknownServiceException:协议不支持输出
块引用>我的code:
公共类TestApplet扩展JApplet的
{
JTextArea中displayTf; 公共TestApplet()
{ } 公共无效的init()
{
尝试
{
SwingUtilities.invokeAndWait(新的Runnable(){
公共无效的run()
{
的initComponents();
连接();
}
});
}
赶上(InterruptedException的E){e.printStackTrace(); }
赶上(的InvocationTargetException E){e.printStackTrace(); }
} 公共无效停止(){}
公共无效的destroy(){}
公共无效的start(){} 公共无效的initComponents()
{
的JPanel的mainPanel =(JPanel中)的getContentPane();
displayTf =新的JTextArea(); mainPanel.add(displayTf);
} 公共无效连接()
{
尝试
{
displayTf.setText(displayTf.getText()+\\ n路径:+得到codeBase的()); //在浏览器中会显示文件:/ C:/.../ TestApplet /斌
网址servletUrl =新的URL(获得codeBase的(),TestApplet); //我的applet的类文件的名称是TestApplet.class
康涅狄格州的URLConnection = servletUrl.openConnection(); conn.setDoInput(真);
conn.setDoOutput(真);
conn.setUseCaches(假);
conn.setDefaultUseCaches(假);
conn.setRequestProperty(内容类型,应用程序/八位字节流); //设置内容类型,以表明我们发送的二进制数据 OutputStream的OUT = conn.getOutputStream(); //这里EXCEPTION抛出java.net.UnknownServiceException:协议不支持输出 //一些测试我做过
// conn.setRequestProperty(内容类型,应用程序/ x-java的序列化对象);
// conn.setRequestProperty(授权,基本+ EN code(uidPassword));
// System.setProperty(http.proxyHost,proxy.example.com);
// System.setProperty(把http.proxyPort,8080); }
赶上(IOException异常E)
{
displayTf.setText(displayTf.getText()+\\ n在连接():失败:+ E);
}
}
解决方案
文件:
网址不支持写入其中。当你的applet页上的Web服务器,你就会有一个
HTTP:
URL,它支持文字 - 但它只会工作,如果有人在服务器 - 一边是有接受请求(POST可能或PUT,不知道)。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 ); } }
解决方案
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).这篇关于入门小程序的OutputStream抛出异常:什么是错的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!