use of javax.servlet.ServletOutputStream in project ratpack by ratpack.
the class PatchedSampleServer method directResponse.
private String directResponse(HttpServletResponse httpResp, String response) throws IOException {
ServletOutputStream os = httpResp.getOutputStream();
os.write(response.getBytes());
os.close();
return null;
}
use of javax.servlet.ServletOutputStream in project ranger by apache.
the class ServiceDBStore method writeJson.
private void writeJson(List<RangerPolicy> policies, String jsonFileName, HttpServletResponse response) throws JSONException, IOException {
response.setContentType("text/json");
response.setHeader("Content-Disposition", "attachment; filename=" + jsonFileName);
ServletOutputStream out = null;
RangerExportPolicyList rangerExportPolicyList = new RangerExportPolicyList();
putMetaDataInfo(rangerExportPolicyList);
rangerExportPolicyList.setPolicies(policies);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(rangerExportPolicyList, RangerExportPolicyList.class);
try {
out = response.getOutputStream();
response.setStatus(HttpServletResponse.SC_OK);
IOUtils.write(json, out, "UTF-8");
} catch (Exception e) {
LOG.error("Error while exporting json file " + jsonFileName, e);
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (Exception ex) {
}
}
}
use of javax.servlet.ServletOutputStream in project hutool by looly.
the class ServletUtil method write.
/**
* 返回数据给客户端
*
* @param response 响应对象{@link HttpServletResponse}
* @param in 需要返回客户端的内容
* @param bufferSize 缓存大小
*/
public static void write(HttpServletResponse response, InputStream in, int bufferSize) {
ServletOutputStream out = null;
try {
out = response.getOutputStream();
IoUtil.copy(in, out, bufferSize);
} catch (IOException e) {
throw new UtilException(e);
} finally {
IoUtil.close(out);
IoUtil.close(in);
}
}
use of javax.servlet.ServletOutputStream in project incubator-dubbo-ops by apache.
the class Restful method execute.
public void execute(Map<String, Object> context) throws Exception {
Result result = new Result();
if (request.getParameter("url") != null) {
url = URL.valueOf(URL.decode(request.getParameter("url")));
}
if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
currentUser = user;
operator = user.getUsername();
role = user.getRole();
context.put(WebConstants.CURRENT_USER_KEY, user);
}
operatorAddress = (String) context.get("clientid");
if (operatorAddress == null || operatorAddress.isEmpty()) {
operatorAddress = (String) context.get("request.remoteHost");
}
context.put("operator", operator);
context.put("operatorAddress", operatorAddress);
String jsonResult = null;
try {
result = doExecute(context);
result.setStatus("OK");
} catch (IllegalArgumentException t) {
result.setStatus("ERROR");
result.setCode(3);
result.setMessage(t.getMessage());
}// }
catch (Throwable t) {
result.setStatus("ERROR");
result.setCode(1);
result.setMessage(t.getMessage());
}
response.setContentType("application/javascript");
ServletOutputStream os = response.getOutputStream();
try {
jsonResult = JSON.toJSONString(result);
os.print(jsonResult);
} catch (Exception e) {
response.setStatus(500);
os.print(e.getMessage());
} finally {
os.flush();
}
}
use of javax.servlet.ServletOutputStream in project summer-bean by cn-cerc.
the class ExportPdf method export.
public void export() throws IOException, DocumentException {
Template template = this.getTemplate();
// 清空输出流
response.reset();
if ("file".equals(template.getOutputDevice())) {
// 设置相应内容的编码格式
response.setCharacterEncoding("UTF-8");
String fname = new String(template.getFileName().getBytes(), "ISO-8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fname + ".pdf");
// 定义输出类型
response.setContentType("application/pdf");
}
// 第一步
Document doc = template.getDocument();
// 第二步
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(doc, pdfStream);
// 第三步
if (template.getHeaderFooter() != null) {
writer.setPageEvent(template.getHeaderFooter());
} else if (template.getHeader() != null) {
ReportHeaderFooter headerFooter = new ReportHeaderFooter();
headerFooter.setHeader(template.getHeader());
writer.setBoxSize("art", doc.getPageSize());
writer.setPageEvent(headerFooter);
}
doc.open();
// 第四步
doc.addAuthor("地藤系统");
doc.addSubject("地藤系统报表文件");
doc.addCreationDate();
template.output(doc, writer);
// 设置是否自动显示打印对话框
if ("printer".equals(template.getOutputDevice())) {
// writer.addJavaScript("this.print(false);", false);
writer.addJavaScript("this.print({bUI: true, bSilent: true,bShrinkToFit:true});", false);
// document.add(new Chunk("Silent Auto Print"));
}
// 第五步
doc.close();
// 第六步
response.setContentType("application/pdf");
response.setContentLength(pdfStream.size());
ServletOutputStream out = response.getOutputStream();
pdfStream.writeTo(out);
out.flush();
response.flushBuffer();
}
Aggregations