Java/Servlets/Send File

Материал из Java эксперт
Перейти к: навигация, поиск

Another servlet to Send PDF

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendPdf extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    //get the "file" parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
      throw new ServletException(
          "Invalid or non-existent file parameter in SendPdf servlet.");
    // add the .pdf suffix if it doesn"t already exist
    if (fileName.indexOf(".pdf") == -1)
      fileName = fileName + ".pdf";
    String pdfDir = getServletContext().getInitParameter("pdf-dir");
    if (pdfDir == null || pdfDir.equals(""))
      throw new ServletException(
          "Invalid or non-existent "pdf-Dir" context-param.");
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try {
      stream = response.getOutputStream();
      File pdf = new File(pdfDir + "/" + fileName);
      response.setContentType("application/pdf");
      response.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
      response.setContentLength((int) pdf.length());
      FileInputStream input = new FileInputStream(pdf);
      buf = new BufferedInputStream(input);
      int readBytes = 0;
      while ((readBytes = buf.read()) != -1)
        stream.write(readBytes);
    } catch (IOException ioe) {
      throw new ServletException(ioe.getMessage());
    } finally {
      if (stream != null)
        stream.close();
      if (buf != null)
        buf.close();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}





Another servlet to Send XML

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendXml extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    //get the "file" parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
      throw new ServletException(
          "Invalid or non-existent file parameter in SendXml servlet.");
    // add the .doc suffix if it doesn"t already exist
    if (fileName.indexOf(".xml") == -1)
      fileName = fileName + ".xml";
    String xmlDir = getServletContext().getInitParameter("xml-dir");
    if (xmlDir == null || xmlDir.equals(""))
      throw new ServletException(
          "Invalid or non-existent xmlDir context-param.");
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try {
      stream = response.getOutputStream();
      File xml = new File(xmlDir + "/" + fileName);
      response.setContentType("text/xml");
      response.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
      response.setContentLength((int) xml.length());
      FileInputStream input = new FileInputStream(xml);
      buf = new BufferedInputStream(input);
      int readBytes = 0;
      while ((readBytes = buf.read()) != -1)
        stream.write(readBytes);
    } catch (IOException ioe) {
      throw new ServletException(ioe.getMessage());
    } finally {
      if (stream != null)
        stream.close();
      if (buf != null)
        buf.close();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}





Send mp3

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendMp3 extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
      throw new ServletException(
          "Invalid or non-existent file parameter in SendMp3 servlet.");
      if (fileName.indexOf(".mp3") == -1)
      fileName = fileName + ".mp3";
    String mp3Dir = getServletContext().getInitParameter("mp3-dir");
    if (mp3Dir == null || mp3Dir.equals(""))
      throw new ServletException(
          "Invalid or non-existent mp3Dir context-param.");
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try {
      stream = response.getOutputStream();
      File mp3 = new File(mp3Dir + "/" + fileName);
      //set response headers
      response.setContentType("audio/mpeg");
      response.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
      response.setContentLength((int) mp3.length());
      FileInputStream input = new FileInputStream(mp3);
      buf = new BufferedInputStream(input);
      int readBytes = 0;
      //read from the file; write to the ServletOutputStream
      while ((readBytes = buf.read()) != -1)
        stream.write(readBytes);
    } catch (IOException ioe) {
      throw new ServletException(ioe.getMessage());
    } finally {
      if (stream != null)
        stream.close();
      if (buf != null)
        buf.close();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}





Send PDF file

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UrlServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    //get the "file" parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
      throw new ServletException(
          "Invalid or non-existent file parameter in UrlServlet servlet.");
    if (fileName.indexOf(".pdf") == -1)
      fileName = fileName + ".pdf";
    URL pdfDir = null;
    URLConnection urlConn = null;
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try {
      pdfDir = new URL(getServletContext().getInitParameter(
          "remote-pdf-dir")
          + fileName);
    } catch (MalformedURLException mue) {
      throw new ServletException(mue.getMessage());
    }
    try {
      stream = response.getOutputStream();
      //set response headers
      response.setContentType("application/pdf");
      response.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
      urlConn = pdfDir.openConnection();
      response.setContentLength((int) urlConn.getContentLength());
      buf = new BufferedInputStream(urlConn.getInputStream());
      int readBytes = 0;
      //read from the file; write to the ServletOutputStream
      while ((readBytes = buf.read()) != -1)
        stream.write(readBytes);
    } catch (IOException ioe) {
      throw new ServletException(ioe.getMessage());
    } finally {
      if (stream != null)
        stream.close();
      if (buf != null)
        buf.close();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}





Send word document

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendWord extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    //get the "file" parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
      throw new ServletException(
          "Invalid or non-existent file parameter in SendWord servlet.");
    // add the .doc suffix if it doesn"t already exist
    if (fileName.indexOf(".doc") == -1)
      fileName = fileName + ".doc";
    String wordDir = getServletContext().getInitParameter("word-dir");
    if (wordDir == null || wordDir.equals(""))
      throw new ServletException(
          "Invalid or non-existent wordDir context-param.");
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try {
      stream = response.getOutputStream();
      File doc = new File(wordDir + "/" + fileName);
      response.setContentType("application/msword");
      response.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
      response.setContentLength((int) doc.length());
      FileInputStream input = new FileInputStream(doc);
      buf = new BufferedInputStream(input);
      int readBytes = 0;
      while ((readBytes = buf.read()) != -1)
        stream.write(readBytes);
    } catch (IOException ioe) {
      throw new ServletException(ioe.getMessage());
    } finally {
      if (stream != null)
        stream.close();
      if (buf != null)
        buf.close();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}





Send XML

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResourceServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    //get web.xml for display by a servlet
    String file = "/WEB-INF/web.xml";
    URL url = null;
    URLConnection urlConn = null;
    PrintWriter out = null;
    BufferedInputStream buf = null;
    try {
      out = response.getWriter();
      url = getServletContext().getResource(file);
      //set response header
      response.setContentType("text/xml");
      urlConn = url.openConnection();
      //establish connection with URL presenting web.xml
      urlConn.connect();
      buf = new BufferedInputStream(urlConn.getInputStream());
      int readBytes = 0;
      while ((readBytes = buf.read()) != -1)
        out.write(readBytes);
    } catch (MalformedURLException mue) {
      throw new ServletException(mue.getMessage());
    } catch (IOException ioe) {
      throw new ServletException(ioe.getMessage());
    } finally {
      if (out != null)
        out.close();
      if (buf != null)
        buf.close();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}