2013年11月4日星期一

About servlet inside path problem when using OutputStream , thank

 This post last edited by the raylee2007 on 2013-03-15 10:44:39
questions: general program , implemented in java application and implemented in the servlet path settings OutputStream what ? Why the former can use relative paths , the latter can only use absolute path ?


The first is the use of java program to achieve the production of a JFreeChart, middle OutputStream that can be used normally .

import java.awt.Font;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

public class JFreeChartTest3 {
public static void main(String[] args) throws Exception {
JFreeChart chart = ChartFactory.createPieChart("某公司人员组织数据图",
getDataset(), true, true, false);
chart.setTitle(new TextTitle("某公司组织结构图", new Font("宋体", Font.BOLD
+ Font.ITALIC, 20)));

LegendTitle legend = chart.getLegend(0);// 设置Legend
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
PiePlot plot = (PiePlot) chart.getPlot();// 设置Plot
plot.setLabelFont(new Font("隶书", Font.BOLD, 16));




        //------------------------------------------------------
OutputStream os = new FileOutputStream(
"WebContent/admin/images/reports/company.jpeg");
                // 图片是文件格式的,故要用到FileOutputStream用来输出。
        //------------------------------------------------------






ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
// 使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。

os.close();// 关闭输出流
}

private static DefaultPieDataset getDataset() {
DefaultPieDataset dpd = new DefaultPieDataset(); // 建立一个默认的饼图
dpd.setValue("管理人员", 25); // 输入数据
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);
return dpd;
}
}


The second program is implemented through a servlet , but the same that path is wrong


import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;


public class SalesProductChartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


public SalesProductChartServlet() {
super();
// TODO Auto-generated constructor stub
}


protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
JFreeChart chart = ChartFactory.createPieChart("某公司人员组织数据图",
getDataset(), true, true, false);
chart.setTitle(new TextTitle("某公司组织结构图", new Font("宋体", Font.BOLD
+ Font.ITALIC, 20)));

LegendTitle legend = chart.getLegend(0);// 设置Legend
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
PiePlot plot = (PiePlot) chart.getPlot();// 设置Plot
plot.setLabelFont(new Font("隶书", Font.BOLD, 16));





//-------------------------------------------------------
OutputStream os = new FileOutputStream(
"WebContent/admin/images/reports/company.jpeg");
                // 图片是文件格式的,故要用到FileOutputStream用来输出。
                //这里的路径如果换成绝对路径就可以正常实现
                //报错:java.io.FileNotFoundException: WebContent\admin\images\reports\company.jpeg (系统找不到指定的路径。)

//-------------------------------------------------------





ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
// 使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。

os.close();// 关闭输出流
}

private static DefaultPieDataset getDataset() {
DefaultPieDataset dpd = new DefaultPieDataset(); // 建立一个默认的饼图
dpd.setValue("管理人员", 25); // 输入数据
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);
return dpd;
}
}


------ Solution ------------------------------------- -------
one for browser with , one is for server .
------ Solution ---------------------------------------- ----
servlet in the root directory is the WebContent
Try / admin / images / reports / company.jpeg
------ For reference only -------------------- -------------------
Why use relative paths will complain it ?
------ For reference only -------------------------------------- -
it seems to be the default path under tomcat bin folder !

没有评论:

发表评论