Following is an example of a Servlet which downloads a PDF and returns the PDF as part of HttpResponse.
The source of the PDF can be anything i.e. a local file system, database or any internal file server.
For this example I am using an Http URL for PDF resource.
Things to notice in this program :
inputStream = url.openStream();
This is available in java 7 and above.
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
Program.
Here is the code. (Please change the PDF_URL constant in the program to a valid url you would like to use)
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This Servlet downloads a document from a given url and return as part of the reponse.
*
* Servlet implementation class PDFDataServlet
*/
@WebServlet("/pdfdata")
public class PDFDataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Http URL to PDF resource.
*/
private static String PDF_URL = "<<URL>>";
/**
* @see HttpServlet#HttpServlet()
*/
public PDFDataServlet() {
super();
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
InputStream inputStream = null;
try {
URL url = new URL(PDF_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if(conn.getResponseCode() != 200){
System.out.println("Error Getting the PDF doc");
} else {
inputStream = url.openStream();
/**
* Write the PDF bytes to response.
*/
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = inputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
inputStream.close();
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-store");
response.setDateHeader("Expires",0);
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
}
}
The source of the PDF can be anything i.e. a local file system, database or any internal file server.
For this example I am using an Http URL for PDF resource.
Things to notice in this program :
- Content Type: I am setting content type to application/pdf, it can be changed it something else if your file type is different like image, text, xml etc.
- response.setContentType("application/pdf");
- I check for the Response Code before I try to get the PDF file to make sure URL is valid and the resource exists.
- if(conn.getResponseCode() != 200)
- Get the InputStream from the remote file if the reponse code is 200 (file exists).
inputStream = url.openStream();
This is available in java 7 and above.
- Write the binary file stream in response object
- OutputStream responseOutputStream = response.getOutputStream();
- Also set caching parameters in response header in case you want your servlet to provide latest copy every time.
response.setHeader("cache-control","no-store");
response.setDateHeader("Expires",0);response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
Program.
Here is the code. (Please change the PDF_URL constant in the program to a valid url you would like to use)
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
* This Servlet downloads a document from a given url and return as part of the reponse.
*
* Servlet implementation class PDFDataServlet
*/
@WebServlet("/pdfdata")
public class PDFDataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Http URL to PDF resource.
*/
private static String PDF_URL = "<<URL>>";
/**
* @see HttpServlet#HttpServlet()
*/
public PDFDataServlet() {
super();
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
InputStream inputStream = null;
try {
URL url = new URL(PDF_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if(conn.getResponseCode() != 200){
System.out.println("Error Getting the PDF doc");
} else {
inputStream = url.openStream();
/**
* Write the PDF bytes to response.
*/
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = inputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
inputStream.close();
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-store");
response.setDateHeader("Expires",0);
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
}
}
No comments:
Post a Comment