0

I am using java to create a line chart png file using jfree. The code first presents a port for processing the request, a browser opens the port, an image is created then the java code sends html back the browser with a reference (<IMG SRC=\"images/receiverevent.png\" BORDER=1/>) to the file created.

Thing is, doesn't display the image.

All other html elements display OK and If I save the html source from the browser into a local file (say showme.html) and open it the image is displayed. So, the html I am generating appears OK.

Anyone else seen this issue, have any idea how to resolve??

More details:

  1. code to generate chart.

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

Output: creates a file in the images directory...

  1. code which generates html from program to browser...

     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 captured from the browsers View Source...

     <html>
     <title>Stub Monitoring Page</title>
     <body>
     <h1>Bar Chart</h1><br>
     <IMG SRC="images/receivereventBC.png" BORDER=1/><br>
     </body>
     </html>
    
  3. What the browser displays

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

  1. what the browser displays after i've saved above html code to a text file showme.html

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

I suspect I have some sort of relative path issue so I changed these lines

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

to

    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/>";

But the same effects as previously occurred..

Solved it: cant load external resources in modern browsers.

I'll need some other way, might need to setup an actual web server...

  • 1
    it sounds like - *I wrote something and it doesn't work as I expected I won't show what I wrote but try to guess what's wrong with it*. Show you code, results and expected results. – StanislavL May 30 '14 at 05:46
  • Are your quotes be [escaped correctly](http://stackoverflow.com/q/4015345/230513)? Some other things to look at [here](http://stackoverflow.com/q/12354614/230513). – trashgod May 30 '14 at 08:30
  • more info added, trust this is sufficient. trashgod, pretty sure I've got my characters escaped but a good pickup it was a problem I had earlier in the attempt.. – user3690083 May 30 '14 at 10:16
  • Solved it: Nothing wrong with my code you cant load external resources in modern browsers. I'll need some other way, might need to setup an actual web server... – user3690083 May 30 '14 at 13:02

0 Answers0