本文介绍了入门小程序的OutputStream抛出异常:什么是错的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小程序,我试图找回一个URLConnection对象的输出流的使用的 conn.getOutputStream(); 的。当我尝试这样做,我的小程序抛出异常的 java.net.UnknownServiceException:协议不支持输出

这是怎么回事错误和放大器;我该如何解决这一问题?这一直是我一直在处理了一段时间与放大器的问题;我真的讲,因为我不明白究竟是什么错&安培;我怎样才能解决这个问题。

一些重要的背景信息。我打开和放大器;通过打开加载一个小程序的HTML文件运行我的小程序。小程序加载成功和放大器;创建所有JComponent上。在试图获得输出流,我得到我上面提到的例外。

在我的浏览器中运行时,在我的小程序将显示输出:

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抛出异常:什么是错的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 09:00