Example usage for javax.servlet.http HttpServletResponse getOutputStream

List of usage examples for javax.servlet.http HttpServletResponse getOutputStream

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getOutputStream.

Prototype

public ServletOutputStream getOutputStream() throws IOException;

Source Link

Document

Returns a ServletOutputStream suitable for writing binary data in the response.

Usage

From source file:com.usefullc.platform.common.utils.IOUtils.java

/**
 * /*from   ww  w.  ja v a  2 s  .  c o m*/
 * 
 * @param content
 * @param fileName
 * @param response
 */
public static void download(byte[] content, String fileName, HttpServletResponse response) {
    try {
        // response
        response.reset();

        // ??linux ?  linux utf-8,windows  GBK)
        String defaultEncoding = System.getProperty("file.encoding");
        if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) {

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1"));
        } else {
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
        }

        // responseHeader
        response.addHeader("Content-Length", "" + content.length);
        response.setContentType("application/octet-stream");
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(content);
        toClient.flush();
        toClient.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * ?//from  ww  w.  ja  v  a2  s .  co  m
 * 
 * @param files 
 * @param out ?
 * @throws IOException
 * @throws Exception
 */
public static void zipDownLoad(Map<File, String> downQuene, HttpServletResponse response) throws IOException {

    ServletOutputStream out = response.getOutputStream();
    ZipOutputStream zipout = new ZipOutputStream(out);
    ZipEntry entry = null;
    zipout.setLevel(1);
    // zipout.setEncoding("GBK");
    if (downQuene != null && downQuene.size() > 0) {
        for (Entry<File, String> fileInfo : downQuene.entrySet()) {
            File file = fileInfo.getKey();
            try {
                String filename = new String(fileInfo.getValue().getBytes(), "GBK");
                entry = new ZipEntry(filename);
                entry.setSize(file.length());
                zipout.putNextEntry(entry);
            } catch (IOException e) {
                // Logger.getLogger(FileUtil.class).warn(":", e);
            }
            BufferedInputStream fr = new BufferedInputStream(new FileInputStream(fileInfo.getKey()));
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fr.read(buffer)) != -1)
                zipout.write(buffer, 0, len);
            fr.close();
        }
    }

    zipout.finish();
    zipout.flush();
    // out.flush();
}

From source file:com.lushapp.common.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();/*w ww .  j  a  v  a 2 s . c  o  m*/
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayFilename);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out.println("<html><head><title>Basic Servlet</title></head>");
    out.println("<body>Database username is  <b>" + dbName);
    out.println("</b><br>Database password is  <b>" + dbPassword + "</b>");
    out.println("</body></html>");
}

From source file:com.trailmagic.image.ui.InputStreamView.java

private void writeOutput(Map model, HttpServletResponse res) throws IOException {
    OutputStream out = res.getOutputStream();
    InputStream in = (InputStream) model.get(STREAM_KEY);

    IOUtils.copy(in, out);/*from ww  w .j ava  2 s. c o  m*/
}

From source file:LoggingServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    ServletOutputStream out = res.getOutputStream();
    context.log("doGet has now been invoked");
    res.setContentType("text/html");
    out.println("<html><head><title>Logging Servlet</title></head>");
    out.println("<body>Visit the <tomcat-home>\\logs and open the xx file to see the log entries");
    out.println("</body></html>");
}

From source file:MyServlet.java

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out.println("<html><head><title>Basic Form Processor Output</title></head>");
    out.println("<body>");
    out.println("<h1>Here is your Form Data</h1>");
    //extract the form data here
    String title = req.getParameter("title");
    String name = req.getParameter("name");
    String city = req.getParameter("city");
    String country = req.getParameter("country");
    String tel = req.getParameter("tel");
    String age = req.getParameter("age");
    // extracting data from the checkbox field
    String[] interests = req.getParameterValues("interests");
    //output the data into a web page
    out.println("Your title is " + title);
    out.println("<br>Your name is " + name);
    out.println("<br>Your city is " + city);
    out.println("<br>Your country is " + country);
    out.println("<br>Your tel is " + tel);
    out.println("<br>Your interests include<ul> ");
    for (int i = 0; i < interests.length; i++) {
        out.println("<li>" + interests[i]);
    }/*w  ww . j a  va 2  s.c  om*/
    out.println("</ul>");
    out.println("<br>Your age is " + age);
    out.println("</body></html>");
}

From source file:DeliberateException.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out.println("<html><head><title>Exception Thrower</title></head>");
    out.println("<body>You'll never see this!</body></html>");

}

From source file:org.opensafety.hishare.view.StringOutput.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String output = (String) model.get("string");
    response.getOutputStream().print(output);
    response.getOutputStream().flush();//  w  w  w .  ja  va 2 s  .co m
}

From source file:net.oletalk.hellospringboot.controller.S3Controller.java

@RequestMapping("/from")
void from(@RequestParam(value = "key") String objectKey, HttpServletResponse response) {
    try {/*from  w w w  .j av a  2  s  .  c  om*/
        service.writeDocument(objectKey, response.getOutputStream());
    } catch (IOException ioe) {
        try {
            response.getWriter().println("Error in response: " + ioe.toString());
        } catch (IOException e) {
            LOG.error(e);
        }
    } catch (S3Exception ex) {
        LOG.error(ex);
    }
}