package com.wisu.xitiancheng.control;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.lang.StringUtils;
import org.apache.fop.svg.PDFTranscoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wisu.xitiancheng.base.BaseControl;
@Controller
public class CommController extends BaseControl{
@RequestMapping(value="pageComm.do")
public String forward(ModelMap model, HttpServletRequest request,
HttpServletResponse response) {
String page = request.getParameter("page");
//String path = request.getParameter("path");
//System.out.println("str = " + path);
if("top".equals(page)){
Date now = new Date();
String time = DateFormat.getDateInstance(DateFormat.FULL).format(now);
request.setAttribute("time", time);
//request.setAttribute("path", path);
}
if("tmpTop".equals(page)){
Date now = new Date();
String time = DateFormat.getDateInstance(DateFormat.FULL).format(now);
request.setAttribute("time", time);
//request.setAttribute("path", path);
}
if(StringUtils.isEmpty(page)){
page = "index";
}
return page; //返回结果,供视图解析器分析使用
}
@RequestMapping("index.htm")
public String toLogin(HttpServletRequest request, HttpServletResponse response){
return "index";
}
@RequestMapping("/index")
public String login(HttpServletRequest request, HttpServletResponse response){
return "index";
}
@RequestMapping("highCharts.htm")
public String toHighCharts(HttpServletRequest request, HttpServletResponse response){
return "highCharts";
}
/**
* HighCharts导出图片
* @param response
* @param request
* @param page
* @throws ServletException, IOException
*/
@RequestMapping(value="SaveAsImage.do")
public void toSaveAsImage(HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException{
request.setCharacterEncoding("utf-8"); //设置UTF-8编码,解决乱码问题
String type = request.getParameter("type");
String svg = request.getParameter("svg");
String filename = request.getParameter("filename");
filename = filename==null?"chart":filename;
ServletOutputStream out = response.getOutputStream();
if (null != type && null != svg) {
svg = svg.replaceAll(":rect", "rect");
String ext = "";
Transcoder t = null;
if (type.equals("image/png")) {
ext = "png";
t = new PNGTranscoder();
} else if (type.equals("image/jpeg")) {
ext = "jpg";
t = new JPEGTranscoder();
} else if (type.equals("application/pdf")) {
ext = "pdf";
t =(Transcoder) new PDFTranscoder();
} else if(type.equals("image/svg+xml"))
ext = "svg";
response.addHeader("Content-Disposition", "attachment; filename="+ filename + "."+ext);
response.addHeader("Content-Type", type);
if (null != t) {
TranscoderInput input = new TranscoderInput(new StringReader(svg));
TranscoderOutput output = new TranscoderOutput(out);
try {
t.transcode(input, output);
} catch (TranscoderException e) {
out.print("Problem transcoding stream. See the web logs for more details.");
e.printStackTrace();
}
} else if (ext.equals("svg")) {
// out.print(svg);
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.append(svg);
writer.close();
} else
out.print("Invalid type: " + type);
} else {
response.addHeader("Content-Type", "text/html");
out.println("Usage:\n\tParameter [svg]: The DOM Element to be converted." +
"\n\tParameter [type]: The destination MIME type for the elment to be transcoded.");
}
out.flush();
out.close();
}
}
- 1
- 2
- 3
前往页