使用jfree创建图像并在浏览器中显示它

时间:2014-05-30 05:42:39

标签: java jfreechart

我使用java使用jfree创建折线图png文件。代码首先显示一个用于处理请求的端口,浏览器打开端口,创建一个映像,然后java代码将html发送回浏览器,并将引用(< IMG SRC=\"images/receiverevent.png\" BORDER=1/>)发送到创建的文件。

事情是,不显示图像。

所有其他html元素显示OK并且如果我将html源从浏览器保存到本地文件(例如showme.html)并打开它,则显示图像。所以,我生成的html似乎没问题。

其他人看过这个问题,知道如何解决?

更多详情:

  1. 生成图表的代码。

     JFreeChart barChartObject = ChartFactory.createBarChart(
     "Reciever Event Totals",  // chart title
     "Event",                  // domain axis label
     "Volume",                 // range axis label
     bar_chart_dataset,        // data
     PlotOrientation.VERTICAL, // orientation
     true,                     // include legend
     true,                     // tooltips?
     false                     // URLs?
     );
    
     width=640; /* Width of the image */
     height=240; /* Height of the image */                
     File barChart=new File("images//receivereventBC.png");              
     ChartUtilities.saveChartAsPNG(barChart,barChartObject,width,height);
    
  2. 输出:在images目录中创建一个文件......

    1. 从程序到浏览器生成html的代码......

       BufferedOutputStream outStream = new             BufferedOutputStream(clientConnection.getOutputStream());
      
       String barChartImgSrc = "<IMG SRC=\"images/receivereventBC.png\" BORDER=1/>";
      
      String responseMsg = "HTTP/1.1 200 OK\r\n"
        + "Content-Type: text/html; charset=UTF-8\r\n"
        + "Content-Length: 188\r\n"
        + "Connection: Close\r\n\r\n"
        + "<html>\r\n" 
        + "<title>Stub Monitoring Page</title>\r\n"
        + "<body>\r\n"
        + "<h1>Bar Chart</h1><br>\r\n"
        + barChartImgSrc
        + "<br>\r\n"
        + "</body>\r\n"
        + "</html>\r\n";
      
      outStream.write(responseMsg.getBytes()); 
      outStream.flush(); 
      outStream.close();
      inStream.close();
      clientConnection.close(); 
      
    2. 从浏览器中捕获的HTML查看来源...

       <html>
       <title>Stub Monitoring Page</title>
       <body>
       <h1>Bar Chart</h1><br>
       <IMG SRC="images/receivereventBC.png" BORDER=1/><br>
       </body>
       </html>
      
    3. 浏览器显示的内容

    4. http://imageshack.com/a/img839/4204/ebc2.png

      1. 我将html代码保存到文本文件showme.html
      2. 后浏览器显示的内容

        http://imageshack.com/a/img837/4549/asxb.png

        我怀疑我有某种相对路径问题所以我改变了这些行

            File barChart=new File("images//receivereventBC.png");
            String barChartImgSrc = "<IMG SRC=\"images/receivereventBC.png\" BORDER=1/>";
        

            File barChart=new   File("D://Dropbox//java//20140529_httpStub//images//receivereventBC.png");              
            String barChartImgSrc = "<IMG SRC=\"D://Dropbox//java//20140529_httpStub//images//receivereventBC.png\" BORDER=1/>";
        

        但效果与之前相同..

        解决了它:无法在现代浏览器中加载外部资源。

        我还需要其他方法,可能需要设置一个实际的网络服务器...

0 个答案:

没有答案