Getting Applets OutputStream throws an exception: What is wrong?

风格不统一 提交于 2019-12-13 05:44:18

问题


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 );
        }
    }

回答1:


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).




回答2:


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://)



来源:https://stackoverflow.com/questions/4975743/getting-applets-outputstream-throws-an-exception-what-is-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!