docpubservlet.java

来自「Java的框架」· Java 代码 · 共 104 行

JAVA
104
字号
package mcaps.core.docman.webapp.servlet;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.*;
import javax.servlet.http.*;

import mcaps.core.docman.util.DocManUtil;
import mcaps.core.docman.util.NameConstants;
import mcap.core.docman.model.FileObject;
import mcap.core.docman.service.DocAccessException;
import mcap.core.docman.service.DocManManager;
import mcap.core.docman.service.DocPathNotFoundException;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * This class get the document from repository and serve to user.
 *
 * @author Chan Chin Wei
 * @date May 19, 2006
 * @version 1.0.1.0
 */
public class docPubServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5589114595085935156L;

	private static final String NOPAGE_VIEW = "/prrm/prrm/docMan/noPageReturned.action";
	
	public void doPost(HttpServletRequest req, HttpServletResponse res)	throws ServletException, IOException {
		
		ServletContext servletContext = getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext (servletContext);
		DocManManager docManager = (DocManManager) ctx.getBean("docManManagerService");
		
		String docpath = DocManUtil.urlDecodePath(StringUtils.substringAfter(req.getRequestURI(), req.getContextPath()));
		//remove the servlet name
		docpath = docpath.substring(docpath.indexOf(NameConstants.DOC_PUB) + NameConstants.DOC_PUB.length());
		//remove the /
		if (docpath.indexOf("/") == 0)
			docpath = docpath.substring(1);
		FileObject file = null;
		InputStream ips = null;
		BufferedOutputStream bops = null;
		try {
			file = docManager.getFilePublished(docpath);
			if (file != null) {
				ips = file.getInputStream();
				if (ips != null) {
					res.setContentType (file.getType());
					res.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
					bops = new BufferedOutputStream (res.getOutputStream());
					byte[] buf = new byte [1024];
					
					int size = ips.read (buf, 0, 1024);
					while (size != -1) {
						bops.write(buf, 0, size);
						size = ips.read (buf, 0, 1024);
					}
					bops.flush();
				}
				else
					res.sendRedirect(NOPAGE_VIEW);
			}
			else
				res.sendRedirect(NOPAGE_VIEW);

		} catch (DocAccessException e) {
			e.printStackTrace();
			if (e instanceof DocPathNotFoundException)
				res.sendRedirect(NOPAGE_VIEW);
			else
				throw new ServletException (e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
			try {
				bops.close ();
			} catch (Exception ex) {
				//catch io exception for closing outputstream
			}
		}
		
	}
	
	public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
		doPost (req, res);
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?