docadminservlet.java

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

JAVA
89
字号
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 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 docAdminServlet extends HttpServlet {

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

	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_ADMIN) + NameConstants.DOC_ADMIN.length());
		//remove the /
		if (docpath.indexOf("/") == 0)
			docpath = docpath.substring(1);
		FileObject file = null;
		InputStream ips = null;
		BufferedOutputStream bops = null;
		try {
			file = docManager.getFile(docpath);
			ips = file.getInputStream();
			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();
			
		} catch (DocAccessException e) {
			e.printStackTrace();
			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 + -
显示快捷键?