Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about writing jpg to browser in jsp 1

Status
Not open for further replies.

Geminist

Programmer
Jul 6, 2002
25
CN
I used a component called "jchart" to produce pie chart and wanna output jpg to browser instead of making a file.
Here is the code:

<%@ page contentType=&quot;image/jpeg; charset=gb2312&quot; %>
<%@ page import=&quot;org.jCharts.chartData.ChartDataException&quot; %>
<%@ page import=&quot;org.jCharts.chartData.PieChartDataSet&quot; %>
<%@ page import=&quot;org.jCharts.encoders.*&quot; %>
<%@ page import=&quot;org.jCharts.nonAxisChart.PieChart2D&quot; %>
<%@ page import=&quot;org.jCharts.properties.*&quot; %>
<%@ page import=&quot;java.awt.*&quot; %>
<%@ page import=&quot;java.io.*&quot;%>

<%
try
{
PieChart2DProperties properties=new PieChart2DProperties();
LegendProperties legendProperties=new LegendProperties();
legendProperties.setNumColumns(2);
legendProperties.setPlacement(LegendProperties.RIGHT);
ChartProperties chartProperties=new ChartProperties();
int width = 550;
int height = 350;

double[] data = new double[]{40, 15, 35, 65, 59};
Paint[] paints = new Paint[]{Color.blue, Color.red, Color.green, Color.yellow, Color.white};
String[] labels = {&quot;BMW&quot;, &quot;Honda&quot;, &quot;Lexus&quot;, &quot;Audi&quot;, &quot;Acura&quot;};
PieChartDataSet piechartds=new PieChartDataSet( &quot;Cars That Own!&quot;, data, labels, paints, properties );
PieChart2D pieChart2D = new PieChart2D(piechartds, legendProperties, chartProperties, width, height );
//out.println(&quot;width=&quot;+width);
//out.println(&quot;height=&quot;+height);

//FileOutputStream fo=new FileOutputStream(new File(&quot;sample1.jpg&quot;));
//response.setContentType(&quot;image/jpeg&quot;);
DataOutputStream imagestream=new DataOutputStream(response.getOutputStream());
JPEGEncoder13.encode(pieChart2D,1.0f,imagestream);
imagestream.flush();
System.out.println(&quot;success!&quot;);

}
catch(Throwable throwable)
{
throwable.printStackTrace();
}
%>

weblogic reported a java.net.exception,said that read file failed

and if i use a FileOutputStream to replace this DataOutputStream and write a file on server,everything is ok

can anyone hellp me?
 
The easiest way I know to this is to write a Servlet to do the job. You then call the Servlet witin HTML like so to embed your img -

Code:
 <img src=&quot;[URL unfurl="true"]http://localhost:8080/myContext/servlet/ImgServlet&quot;>[/URL]

So maybe something like :
Code:
public class ImgServlet extends HttpServlet {

	public int counter = 0;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

			PieChart2DProperties properties=new PieChart2DProperties();
			LegendProperties legendProperties=new LegendProperties();
			legendProperties.setNumColumns(2);
			legendProperties.setPlacement(LegendProperties.RIGHT);
			ChartProperties chartProperties=new ChartProperties();
			int width = 550;
			int height = 350;

			double[] data = new double[]{40, 15, 35, 65, 59};
			Paint[] paints = new Paint[]{Color.blue, Color.red, Color.green, Color.yellow, Color.white};
			String[] labels = {&quot;BMW&quot;, &quot;Honda&quot;, &quot;Lexus&quot;, &quot;Audi&quot;, &quot;Acura&quot;};
			PieChartDataSet piechartds=new PieChartDataSet( &quot;Cars That Own!&quot;, data, labels, paints, properties );
			PieChart2D pieChart2D = new PieChart2D(piechartds, legendProperties, chartProperties, width, height );
			//out.println(&quot;width=&quot;+width);
			//out.println(&quot;height=&quot;+height);

			//FileOutputStream fo=new FileOutputStream(new File(&quot;sample1.jpg&quot;));
			//response.setContentType(&quot;image/jpeg&quot;);
			//DataOutputStream imagestream=new DataOutputStream(response.getOutputStream());
			ByteArrayOutputStream baos  = new ByteArrayOutputStream();
			BufferedOutputStream output = new BufferedOutputStream(baos);
			JPEGEncoder13.encode(pieChart2D,1.0f,output);

			(response.getOutputStream()).write(baos.toByteArray());


			System.out.println(&quot;success!&quot;);

    }



}
 
try reset the respone first, then set it with the right contentType before you write the image stream to response.


response.reset();
response.setContentType(&quot;image/jpeg&quot;);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top