📄 fileuploadservlet.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;/** Servlet to allow uploading files to the system. * * @author dmurphy */public class FileUploadServlet extends HttpServlet { static Logger logger = Logger.getLogger(FileUploadServlet.class); private String fileRepository = null; private long maxSize = 1000000; public static String PARAM_FILE_LOCATION = "file-location"; public static String PARAM_MAX_SIZE = "max-size"; /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); this.fileRepository = config.getInitParameter(PARAM_FILE_LOCATION); logger.debug("Set repository path to: " + fileRepository); this.maxSize = Long.parseLong(config.getInitParameter(PARAM_MAX_SIZE)); logger.debug("Set max upload size to: " + maxSize + " bytes"); } /** 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 {// response.setContentType("text/html");// PrintWriter out = response.getWriter();// out.close(); logger.debug("In FileUploadServlet"); try { // Check that we have a file upload request boolean isMultipart = FileUpload.isMultipartContent(request); if (!isMultipart) { throw new RuntimeException("Can't process upload for non-multipart request!"); } logger.debug("Response is multipart!"); FileUpload upload = new FileUpload(); upload.setSizeMax((int)maxSize); upload.setRepositoryPath("/tmp"); ArrayList items = (ArrayList) upload.parseRequest(request); logger.debug("Created " + items.size() + " items for upload."); FileAttachmentModule module = new FileAttachmentModule(fileRepository); module.attachFile(items); } catch (FileUploadException ex) { logger.error(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 + -