Example usage for javax.servlet ServletOutputStream write

List of usage examples for javax.servlet ServletOutputStream write

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream write.

Prototype

public abstract void write(int b) throws IOException;

Source Link

Document

Writes the specified byte to this output stream.

Usage

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * /*  w ww. j  a  v a2 s .co m*/
 *
 * @param attachFile 
 * @param fileName   ????
 * @throws IOException
 */
public static void write(byte[] attachFile, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response??????com???/*from  www .  j a va2 s. c  om*/
 *
 * @param contentType
 * @param attachFile
 * @throws IOException
 */
public static void write(String contentType, byte[] attachFile) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.setContentType(contentType);
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.seer.datacruncher.spring.ConnectionsFileDownloadController.java

public ModelAndView checkValidity(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Long connId = Long.parseLong(request.getParameter("connId"));
    ConnectionsEntity connectionEntity = connectionsDao.find(connId);

    keptXSD = getFileContent(connId, connectionEntity);
    Validate result = new Validate();
    if (keptXSD == null || keptXSD.trim().length() == 0) {
        result.setSuccess(false);//w ww.  j  a  va2s .  c o m
    } else {
        result.setSuccess(true);
    }
    ServletOutputStream out = null;
    response.setContentType("application/json");
    out = response.getOutputStream();
    out.write(new ObjectMapper().writeValueAsBytes(result));
    out.flush();
    out.close();

    return null;
}

From source file:org.exoplatform.frameworks.jcr.command.web.DisplayResourceCommand.java

public boolean execute(Context context) throws Exception {
    GenericWebAppContext webCtx = (GenericWebAppContext) context;
    HttpServletResponse response = webCtx.getResponse();
    HttpServletRequest request = webCtx.getRequest();

    // standalone request?
    String servletPath = request.getPathInfo();
    boolean doClose = true;
    // or included?
    if (servletPath == null) {
        servletPath = (String) request.getAttribute("javax.servlet.include.path_info");
        if (servletPath != null)
            doClose = false;/*from w  w w .  j  ava  2  s  .  c  om*/
    }

    Node file = (Node) webCtx.getSession().getItem((String) context.get(pathKey));
    file.refresh(false);

    Node content = null;

    try {
        content = JCRCommandHelper.getNtResourceRecursively(file);
    } catch (ItemNotFoundException e) {
        // Patch for ver 1.0 back compatibility
        // as exo:image was not primary item
        if (file.isNodeType("exo:article")) {
            try {
                content = file.getNode("exo:image");
            } catch (PathNotFoundException e1) {
                throw e; // new ItemNotFoundException("No nt:resource node found at
                // "+file.getPath()+" nor primary items of nt:resource type
                // ");
            }
        } else {
            throw e; // new ItemNotFoundException("No nt:resource node found at
            // "+file.getPath()+" nor primary items of nt:resource type
            // ");
        }

    }

    // if(file.isNodeType("nt:file")) {
    // content = file.getNode("jcr:content");
    // } else if(file.isNodeType("exo:article")) {
    // content = file.getNode("exo:image");
    // } else
    // throw new Exception("Invalid node type, expected nt:file or exo:article,
    // have "+file.getPrimaryNodeType().getName()+" at "+file.getPath());

    Property data;
    try {
        data = content.getProperty("jcr:data");
    } catch (PathNotFoundException e) {
        throw new PathNotFoundException("No jcr:data node found at " + content.getPath());
    }

    String mime = content.getProperty("jcr:mimeType").getString();
    String encoding = content.hasProperty("jcr:encoding") ? content.getProperty("jcr:encoding").getString()
            : DEFAULT_ENCODING;

    MimeTypeResolver resolver = new MimeTypeResolver();
    String fileName = file.getName();
    String fileExt = "";
    if (fileName.lastIndexOf(".") > -1) {
        fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
        fileName = fileName.substring(0, fileName.lastIndexOf("."));
    }
    String mimeExt = resolver.getExtension(mime);
    if (fileExt == null || fileExt.length() == 0) {
        fileExt = mimeExt;
    }
    response.setContentType(mime + "; charset=" + encoding);
    String parameter = (String) context.get("cache-control-max-age");
    String cacheControl = parameter == null ? "" : "public, max-age=" + parameter;
    response.setHeader("Cache-Control: ", cacheControl);
    response.setHeader("Pragma: ", ""); // leave blank to avoid IE errors
    response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "." + fileExt + "\"");

    if (mime.startsWith("text")) {
        PrintWriter out = response.getWriter();
        out.write(data.getString());
        out.flush();
        if (doClose)
            out.close();
    } else {
        InputStream is = data.getStream();
        byte[] buf = new byte[is.available()];
        is.read(buf);
        ServletOutputStream os = response.getOutputStream();
        os.write(buf);
        os.flush();
        if (doClose)
            os.close();
    }

    return true;
}

From source file:org.obm.push.impl.ResponderImpl.java

private void writeData(byte[] data, String type) throws IOException {
    resp.setContentType(type);// w ww  .ja v  a 2s  .c o  m
    resp.setContentLength(data.length);
    ServletOutputStream out = resp.getOutputStream();
    out.write(data);
    out.flush();
    out.close();
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response?/*  w ww  .  j  a  v a 2  s.  c o m*/
 * ???10mdownloadCall
 *
 * @param attachFile
 * @throws IOException
 */
public static void download(byte[] attachFile, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
        resp.addHeader("Content-Length", "" + attachFile.length);
        //            resp.setContentType("application/octet-stream");
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response//from  ww w  .j av  a 2  s . c o  m
 * ???10mdownloadCall
 *
 * @param file
 * @throws IOException
 */
public static void download(File file, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
        //            resp.addHeader("Content-Length", "" + attachFile.length);
        //            resp.setContentType("application/octet-stream");
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(IOUtils.toByteArray(new FileInputStream(file)));
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl.ExportPhdIndividualProgramProcessInformation.java

private void writeResponse(HttpServletResponse response, final byte[] presentationPage,
        final String contentType) throws IOException {
    final ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType(contentType);
    outputStream.write(presentationPage);
    outputStream.close();/*  w  w w.j a va 2 s.  c o  m*/
}

From source file:org.appverse.web.framework.backend.api.controllers.FileRetrievalController.java

private void processCall(HttpServletResponse response, Map<String, String> parameters) throws Exception {
    Object presentationService = applicationContext.getBean(serviceName.get());
    if (!(presentationService instanceof IFileRetrievalPresentationService)) {
        throw new IllegalArgumentException(
                "Requested Spring Bean is not a File Retrieval Presentation Service: (" + presentationService
                        + ")");
    }//from   w  ww  .  j  av a  2  s . co m

    FileVO fileVO = ((IFileRetrievalPresentationService) presentationService).retrievalFile(parameters);
    final ServletOutputStream output = response.getOutputStream();
    response.setContentType(fileVO.getMimeType());
    response.setHeader("Content-disposition", "attachment; filename=\"" + fileVO.getFilename() + "\"");
    output.write(fileVO.getBytes());
    output.close();

}

From source file:org.obm.push.impl.ResponderImpl.java

@Override
public void sendResponseFile(String contentType, InputStream file) {
    Preconditions.checkNotNull(contentType);
    Preconditions.checkNotNull(file);//from w  w  w. java2s.c om

    logger.debug("response: send file");
    try {
        byte[] b = FileUtils.streamBytes(file, false);
        resp.setContentType(contentType);
        resp.setContentLength(b.length);
        ServletOutputStream out = resp.getOutputStream();
        out.write(b);
        out.flush();
        out.close();
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}