📄 filedownloadservlet.java
字号:
/* CRMS, customer relationship management system Copyright (C) 2003 Service To Youth Council This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For further information contact the SYC ICT department on GPL@syc.net.au 98 Kermode Street North Adelaide South Australia SA 5006 +61 (0)8 8367 0755 *//* * FileUploadServlet.java * * Created on 5 May 2003, 15:58 */package crms.servlet;import java.io.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;import org.apache.commons.fileupload.*;import java.util.*;import org.apache.log4j.*;import crms.module.FileAttachmentModule;import crms.util.*;import crms.dao.*;import crms.vo.*;import java.sql.*;/** Download a file from within CRMS. * * @author dmurphy */public class FileDownloadServlet extends HttpServlet { static Logger logger = Logger.getLogger(FileDownloadServlet.class); private String fileRepository = null; public static String PARAM_FILE_LOCATION = "file-location"; public static String PARAM_USER = "user"; public static String PARAM_ATTACHMENT_TYPE = "type"; public static String PARAM_ATTACHMENT_ID = "id"; private FileAttachmentDAO fileDAO = DAOFactory.getInstance().getFileAttachmentDAO(); private ContactDAO contactDAO = DAOFactory.getInstance().getContactDAO(); /** Initializes the servlet. */ public void init() throws ServletException { this.fileRepository = getInitParameter(PARAM_FILE_LOCATION); logger.debug("Set repository path to: " + fileRepository); } /** Destroys the servlet. */ public void destroy() { } /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); String mimeType = null; File file = null; String user = request.getParameter(PARAM_USER); String attachID = request.getParameter(PARAM_ATTACHMENT_ID); EntityType attachType = (EntityType) AbstractCode.decode(request.getParameter(PARAM_ATTACHMENT_TYPE), EntityType.class); String fileName = fileRepository + "/" + attachType.getCode() + "-" + attachID; FileAttachment attach = fileDAO.getFileAttachment(attachID); response.addHeader("Content-Disposition", "attachment; filename=" + attach.getLocation()); mimeType = getServletContext().getMimeType(attach.getLocation()); file = new File(fileName); if (mimeType != null){ response.setContentType(mimeType); } else { response.setContentType("application/octet-stream"); } response.setContentLength((int)file.length()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] bytes = new byte[1024]; while (in.read(bytes) != -1) { out.write(bytes); } in.close(); out.flush(); out.close(); } private void handleContactExport(BufferedOutputStream out, String user, int CompanyID) throws IOException { Connection con = null; ResultSet rs = null; Statement stmt = null; String sql = "SELECT c.*, " +" cm.\"CompanyName\" AS \"CompanyName\", \n" +" d1.\"ContactValue\" AS \"home-phone\", \n" +" d2.\"ContactValue\" AS \"home-postcode\", \n" +" d3.\"ContactValue\" AS \"home-address\", \n" +" d4.\"ContactValue\" AS \"home-suburb\", \n" +" d5.\"ContactValue\" AS \"home-state\", \n" +" d6.\"ContactValue\" AS \"home-country\", \n" +" d7.\"ContactValue\" AS \"title\", \n" +" d8.\"ContactValue\" AS \"work-phone\", \n" +" d9.\"ContactValue\" AS \"work-postcode\", \n" +" d10.\"ContactValue\" AS \"work-address\", \n" +" d11.\"ContactValue\" AS \"work-suburb\", \n" +" d12.\"ContactValue\" AS \"work-state\", \n" +" d13.\"ContactValue\" AS \"work-country\", \n" +" d14.\"ContactValue\" AS \"work-fax\", \n" +" d15.\"ContactValue\" AS \"work-phone\", \n" +" d16.\"ContactValue\" AS \"work-other-phone\", \n" +" d17.\"ContactValue\" AS \"cont-email\", \n" +" d18.\"ContactValue\" AS \"cont-www\", \n" +" d19.\"ContactValue\" AS \"gender\", \n" +" d20.\"ContactValue\" AS \"birthdate\", \n" +" d21.\"ContactValue\" AS \"role\", \n" +" d22.\"ContactValue\" AS \"position\" \n" +" FROM \"Contacts\" AS c \n" +" LEFT JOIN \"DataStore\" AS d1 on c.\"ContactID\" = d1.\"ContactNumber\" AND d1.\"ContactValueType\"='home-phone' \n" +" LEFT JOIN \"DataStore\" AS d2 on c.\"ContactID\" = d2.\"ContactNumber\" AND d2.\"ContactValueType\"='home-postcode' \n" +" LEFT JOIN \"DataStore\" AS d3 on c.\"ContactID\" = d3.\"ContactNumber\" AND d3.\"ContactValueType\"='home-address' \n" +" LEFT JOIN \"DataStore\" AS d4 on c.\"ContactID\" = d4.\"ContactNumber\" AND d4.\"ContactValueType\"='home-suburb' \n" +" LEFT JOIN \"DataStore\" AS d5 on c.\"ContactID\" = d5.\"ContactNumber\" AND d5.\"ContactValueType\"='home-state' \n" +" LEFT JOIN \"DataStore\" AS d6 on c.\"ContactID\" = d6.\"ContactNumber\" AND d6.\"ContactValueType\"='home-country' \n" +" LEFT JOIN \"DataStore\" AS d7 on c.\"ContactID\" = d7.\"ContactNumber\" AND d7.\"ContactValueType\"='title' \n" +" LEFT JOIN \"DataStore\" AS d8 on c.\"ContactID\" = d8.\"ContactNumber\" AND d8.\"ContactValueType\"='work-phone' \n" +" LEFT JOIN \"DataStore\" AS d9 on c.\"ContactID\" = d9.\"ContactNumber\" AND d9.\"ContactValueType\"='work-postcode' \n" +" LEFT JOIN \"DataStore\" AS d10 on c.\"ContactID\" = d10.\"ContactNumber\" AND d10.\"ContactValueType\"='work-address' \n" +" LEFT JOIN \"DataStore\" AS d11 on c.\"ContactID\" = d11.\"ContactNumber\" AND d11.\"ContactValueType\"='work-suburb' \n" +" LEFT JOIN \"DataStore\" AS d12 on c.\"ContactID\" = d12.\"ContactNumber\" AND d12.\"ContactValueType\"='work-state' \n" +" LEFT JOIN \"DataStore\" AS d13 on c.\"ContactID\" = d13.\"ContactNumber\" AND d13.\"ContactValueType\"='work-country' \n" +" LEFT JOIN \"DataStore\" AS d14 on c.\"ContactID\" = d14.\"ContactNumber\" AND d14.\"ContactValueType\"='work-fax' \n" +" LEFT JOIN \"DataStore\" AS d15 on c.\"ContactID\" = d15.\"ContactNumber\" AND d15.\"ContactValueType\"='work-phone' \n" +" LEFT JOIN \"DataStore\" AS d16 on c.\"ContactID\" = d16.\"ContactNumber\" AND d16.\"ContactValueType\"='work-other-phone' \n" +" LEFT JOIN \"DataStore\" AS d17 on c.\"ContactID\" = d17.\"ContactNumber\" AND d17.\"ContactValueType\"='cont-email' \n" +" LEFT JOIN \"DataStore\" AS d18 on c.\"ContactID\" = d18.\"ContactNumber\" AND d18.\"ContactValueType\"='cont-www' \n" +" LEFT JOIN \"DataStore\" AS d19 on c.\"ContactID\" = d19.\"ContactNumber\" AND d19.\"ContactValueType\"='gender' \n" +" LEFT JOIN \"DataStore\" AS d20 on c.\"ContactID\" = d20.\"ContactNumber\" AND d20.\"ContactValueType\"='birthdate' \n" +" LEFT JOIN \"DataStore\" AS d21 on c.\"ContactID\" = d21.\"ContactNumber\" AND d21.\"ContactValueType\"='role' \n" +" LEFT JOIN \"DataStore\" AS d22 on c.\"ContactID\" = d22.\"ContactNumber\" AND d22.\"ContactValueType\"='position' \n" +" LEFT JOIN \"Company\" AS cm on c.\"CompanyID\" = cm.\"CompanyID\" \n" +" WHERE d1.\"ContactType\" = 'contact' \n" + DAOFactory.getInstance().getPermissionDAO().getPermissionForReadSQL("c.\"ContactID\"", EntityType.CONTACT, user); if (CompanyID >= 0) { sql += "AND c.\"CompanyID\" = " + CompanyID + "\n"; } ArrayList results = new ArrayList(); try { con = contactDAO.getFactory().getInstance().getConnection(); stmt = con.createStatement(); logger.debug("Executing query:"); logger.debug(sql); rs = stmt.executeQuery(sql); String str = null; // add CSV header str = new String("title,firstname,lastname,company,position,role,email,url," + "work-phone,work-fax,work-otherphone,work-address,work-suburb,work-state,work-country,work-postcode," + "home-phone,home-address,home-suburb,home-state,home-country,home-postcode," + "gender,birth\r\n"); out.write(str.getBytes()) ; while (rs.next()) { str = new String( AbstractDAO.decode( rs.getString("title") ) + "," + AbstractDAO.decode( rs.getString("FirstName") ) + "," + AbstractDAO.decode( rs.getString("LastName") ) + "," + AbstractDAO.decode( rs.getString("CompanyName") ) + "," + AbstractDAO.decode( rs.getString("position") ) + "," + AbstractDAO.decode( rs.getString("role") ) + "," + AbstractDAO.decode( rs.getString("cont-email") ) + "," + AbstractDAO.decode( rs.getString("cont-www") ) + "," + AbstractDAO.decode( rs.getString("work-phone") ) + "," + AbstractDAO.decode( rs.getString("work-fax") ) + "," + AbstractDAO.decode( rs.getString("work-other-phone") ) + "," + AbstractDAO.decode( rs.getString("work-address") ) + "," + AbstractDAO.decode( rs.getString("work-suburb") ) + "," + AbstractDAO.decode( rs.getString("work-state") ) + "," + AbstractDAO.decode( rs.getString("work-country") ) + "," + AbstractDAO.decode( rs.getString("work-postcode") ) + "," + AbstractDAO.decode( rs.getString("home-phone") ) + "," + AbstractDAO.decode( rs.getString("home-address") ) + "," + AbstractDAO.decode( rs.getString("home-suburb") ) + "," + AbstractDAO.decode( rs.getString("home-state") ) + "," + AbstractDAO.decode( rs.getString("home-country") ) + "," + AbstractDAO.decode( rs.getString("home-postcode") ) + "," + AbstractDAO.decode( rs.getString("gender") ) + "," + AbstractDAO.decode( rs.getString("birthdate") ) + "\r\n"); out.write(str.getBytes()) ; } } catch (Exception ex) { System.out.println(sql); throw new RuntimeException(ex); } finally { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { throw new RuntimeException(ex); } } } /** Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -