【问题标题】:Getting Applets OutputStream throws an exception: What is wrong?获取 Applets OutputStream 抛出异常:什么问题?
【发布时间】:2011-02-12 01:37:35
【问题描述】:

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

出了什么问题?我该如何解决?这是我一直在处理的一个问题,我真的很紧张,因为我不明白到底出了什么问题以及如何解决它。

一些重要的背景信息。我通过打开加载小程序的 HTML 文件来打开并运行我的小程序。小程序成功加载并创建其所有 JComponent。在尝试获取输出流时,我得到了上面提到的异常。

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

路径:file:/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 );
        }
    }

【问题讨论】:

  • 不应该是file://吗?

标签: java servlets applet


【解决方案1】:

file: URL 不支持写入。

当您的小程序页面位于网络服务器上时,您将拥有一个支持写入的http: URL - 但它仅在服务器端有人接受请求时才有效(可能是 POST 或 PUT,不知道)。

【讨论】:

  • 如果我将 URL servletUrl = new URL( getCodeBase(), "TestApplet" ); 行更改为 ** URL servletUrl = new URL("http : //localhost:8000/" ); & 然后运行我自己的 cgiserver.py mediafire.com/?67bkhhv60bofwyr 文件,该文件创建一个 moch 服务器,然后获取输出流工作。但是当我通过 InputStream in = conn.getInputStream(); 获取输入流时 ** 我收到 HTTP 501 错误。我需要对我的 URLConnection 做什么才能允许输入,我已经说过 conn.setDoInput(true);?
  • 查找服务器的日志文件以了解产生错误的原因。
【解决方案2】:

就像Paŭlo Ebermann 说的,问题出在Url 类型上:如果文件类型是,在运行时,一个文件,你将在Stream 中写入能够读取或写入。显然,您尝试使用 Url 中的 OutputStream。

我遇到了同样的问题,经过一番压力后,我明白了原因:Applet 类(在您的情况下为“TestApplet.class”)位于您的 webapp 和 webserver 的范围之外。在运行时,Url 被解析为文件(“file://”)而不是网络应用资源或页面(“http://”)。

我的解决方案是将我的 Applet 类移动到 Web-Content 目录的内部(在包含 html 文件的同一级别),以便将其视为 Web-apps 资源,并且然后能够发送一些 servlet 请求/响应,这些请求/响应将包含您的输出/输入流。

只需将您的 Applet 类移动到 Web-Content 目录,然后启动您的 Servlet ,而不是从文件系统 (url: file://),而是 来自您的网络服务器(网址:http://)

【讨论】:

    猜你喜欢
    • 2017-08-18
    • 2013-01-16
    • 2017-10-04
    • 2016-07-14
    • 2010-12-09
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    相关资源
    最近更新 更多