2014年2月28日 星期五

PDFView.java 20140228

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PDFView extends javax.servlet.http.HttpServlet {
static String db = "";
static String viewFile = "";
// The MIME type associated with the kind of media to be served.// For more info see
// http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html

//static String CONTENT_TYPE = "application/pdf";
static String CONTENT_TYPE = "";
public PDFView() {
}

public void doGet(HttpServletRequest request, HttpServletResponse response) {
processRequest(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) {

try {
String ContentType="";
db=request.getParameter("db");
viewFile=request.getParameter("viewFile");
HttpSession session = request.getSession();

ContentType=request.getParameter("ContentType");
if (ContentType.indexOf("ms-excel")>=0){
ContentType="application/vnd.ms-excel";
}
CONTENT_TYPE = ContentType;
response.setContentType(this.CONTENT_TYPE);
response.setHeader("Content-Disposition",
"inline; filename="+viewFile);
if (this.CONTENT_TYPE.indexOf("excel")>=0){}

if (session.isNew() || session.getAttribute("Login") == null) {
//response.sendRedirect("index.jsp");
}
File file1 = new File("/erp/"+db+"/"+viewFile);
FileInputStream fs = new FileInputStream(file1);
byte[] byteArray = new byte[(int) file1.length()];
fs.read(byteArray);
fs.close();
response.getOutputStream().write(byteArray);
} catch (Exception e) {
this.outErrorMessage(response,
" Some exception occurred: " + e.getMessage());
return;
}
}

private void outErrorMessage(HttpServletResponse response, String errorMsg) {
response.setContentType("text/plain");
try {
PrintWriter out = response.getWriter();
out.println(errorMsg);
} catch (Exception e) {
}
}

}

沒有留言:

張貼留言