JAVA 将流储存成图片在Linux下报错
业务需求是 flex前台做截图,以流的形式传入后台java,后台java的action中,写文件的时候就报错了,
在windows下没有问题,放到linux下就出错了
报错是:
HTTP Status 500 - Unable to show problem report:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
网上查了下资料,大部分都是jsp的,
说是要加上这两句话
out.clear();
out = pageContext.pushBody();
但是现在是java 怎末解决啊
下面是代码:
public class ExportImageAction extends BaseAction {
private static final long serialVersionUID = 549144288577959118L;
public void saveImage() {
try {
//记录前台传入的输入流
BufferedImage result = ImageIO.read(getRequest().getInputStream());
super.getResponse().setContentType("image/jpeg; charset=utf-8");
super.getResponse().setHeader("Content-Disposition", "attachment; filename=\"amchart.jpg\"");
Graphics2D g = result.createGraphics();
// 处理图形平滑度
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.dispose();
ServletOutputStream f = getResponse().getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(result);
param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70
encoder.encode(result, param);
ImageIO.write(result, "JPEG", f);// 输出图片
f.flush();
f.close();
}
catch (
IOException e) {
e.printStackTrace();
}
}
我把代码移植到JSP中 ,
是这一行报错
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(result);
在线求大神指导!!!
------解决方案--------------------自己多找找原因