01: package com.bostechcorp.cbesb.console.server;
02:
03: import java.io.IOException;
04:
05: import javax.servlet.ServletException;
06: import javax.servlet.ServletOutputStream;
07: import javax.servlet.http.HttpServlet;
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10:
11: import com.bostechcorp.cbesb.common.i18n.CoreMessageConstants;
12: import com.bostechcorp.cbesb.common.i18n.Messages;
13: import com.bostechcorp.cbesb.runtime.ccsl.errordb.AttachmentVO;
14: import com.bostechcorp.cbesb.runtime.ccsl.errordb.ByteContentVO;
15: import com.bostechcorp.cbesb.runtime.ccsl.errordb.DaoConfig;
16: import com.bostechcorp.cbesb.runtime.ccsl.errordb.StringContentVO;
17: import com.ibatis.sqlmap.client.SqlMapClient;
18:
19: public class FileDownloadServlet extends HttpServlet {
20:
21: private static final long serialVersionUID = 1L;
22:
23: private static DaoConfig daoConfig;
24: private static SqlMapClient sqlMap;
25:
26: static {
27: if (daoConfig == null) {
28: daoConfig = new DaoConfig();
29: sqlMap = daoConfig.getSqlMapInstance();
30: }
31: }
32:
33: private String readStringContent(String exchangeId, String name,
34: String type) throws Exception {
35: AttachmentVO attachmentVO = new AttachmentVO();
36: attachmentVO.setExchangeId(Long.parseLong(exchangeId));
37: attachmentVO.setName(name);
38: attachmentVO.setType(type);
39: StringContentVO result = (StringContentVO) sqlMap
40: .queryForObject("selectStringContentByExchangeId",
41: attachmentVO);
42: return result.getContent();
43: }
44:
45: private byte[] readByteContent(String exchangeId, String name,
46: String type) throws Exception {
47: AttachmentVO attachmentVO = new AttachmentVO();
48: attachmentVO.setExchangeId(Long.parseLong(exchangeId));
49: attachmentVO.setName(name);
50: attachmentVO.setType(type);
51: ByteContentVO result = (ByteContentVO) sqlMap.queryForObject(
52: "selectByteContentByExchangeId", attachmentVO);
53: return result.getContent();
54: }
55:
56: protected void doGet(HttpServletRequest request,
57: HttpServletResponse response) throws ServletException,
58: IOException {
59: try {
60: String exchangeId = request.getParameter("ExchangeID");
61: String contentType = request.getParameter("contentType");
62: String type = request.getParameter("type");
63: String name = request.getParameter("name");
64: byte[] content = null;
65: String attachmentContent = "";
66: if (contentType.equals("string")) {
67: attachmentContent = readStringContent(exchangeId, name,
68: type);
69: content = attachmentContent.getBytes();
70: } else if (contentType.equals("byte")) {
71: content = readByteContent(exchangeId, name, type);
72: }
73:
74: response.setContentType("text/html");
75: response.setHeader("Content-Disposition",
76: "attachment;filename=" + name);
77:
78: ServletOutputStream out = response.getOutputStream();
79: out.write(content, 0, content.length);
80: out.close();
81: } catch (Exception ex) {
82: request
83: .setAttribute((Messages
84: .get(CoreMessageConstants.ERRORS)), ex
85: .getMessage());
86: }
87: }
88:
89: }
|