浏览 5467 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-07-29
后来,直接把servlet的内容写到Action里,应该就可以了吧。测试实现后,发现确实可行。实现代码贴出来,看有需要的,也可以看看。 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.StringReader; import java.io.UnsupportedEncodingException; 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.fop.svg.PDFTranscoder; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.ActionSupport; public class ImageExportAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{ /** * */ private static final long serialVersionUID = 7547110498655678209L; private String svg; private String type; private String extName; @SuppressWarnings("unused") private HttpServletRequest request; @SuppressWarnings("unused") private HttpServletResponse response; private InputStream inputStream; public String getContentDisposition() throws UnsupportedEncodingException { String s = new String("分析图".getBytes(), "ISO8859-1"); return "attachment;filename=\"" + s + "."+getExtName()+"\""; } public String execute() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); 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 = new PDFTranscoder(); } else if (type.equals("image/svg+xml")) { ext = "svg"; } setExtName(ext); if (null != t) { TranscoderInput input = new TranscoderInput(new StringReader(svg)); TranscoderOutput output = new TranscoderOutput(out); try { t.transcode(input, output); } catch (TranscoderException e) { e.printStackTrace(); } } out.close(); byte[] data=out.toByteArray(); inputStream= new ByteArrayInputStream(data); return SUCCESS; } public String getSvg() { return svg; } public void setSvg(String svg) { this.svg = svg; } public String getType() { return type; } public void setType(String type) { this.type = type; } public InputStream getInputStream(){ return inputStream; } public String getExtName() { return extName; } public void setExtName(String extName) { this.extName = extName; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO Auto-generated method stub this.request=arg0; } @Override public void setServletResponse(HttpServletResponse arg0) { // TODO Auto-generated method stub this.response=arg0; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-08-13
你好,请问这个struts.xml怎么配置?还有url怎么写?刚接触这个,多谢啦!
|
|
返回顶楼 | |