001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): ______________________________________.
018: *
019: ********************************************************************************/package org.openi.web;
020:
021: import org.apache.commons.fileupload.DiskFileUpload;
022: import org.apache.commons.fileupload.FileItem;
023: import org.apache.commons.fileupload.FileUploadException;
024: import org.apache.log4j.Logger;
025: import java.io.File;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import javax.servlet.http.HttpServletRequest;
030: import org.openi.application.Application;
031: import org.springframework.web.multipart.MultipartFile;
032: import org.springframework.web.multipart.MultipartHttpServletRequest;
033:
034: /**
035: * @author paullucas <br>
036: * This class handles file upload request.
037: * <br>
038: * Revision :
039: * Uddhab Pant - 07/07/2005
040: * Moved file upload code from servlet to this class.
041: */
042: public class UploadFile {
043: private static Logger logger = Logger.getLogger(UploadFile.class);
044:
045: public UploadFile() {
046: }
047:
048: /**
049: * This method uploads files
050: * @param request
051: * @param targetDirectory
052: * @return list of uploaded file path
053: * @throws Exception
054: */
055:
056: public List uploadFile(HttpServletRequest request,
057: String targetDirectory) throws Exception {
058: logger.debug("begining file upload");
059:
060: if (!new File(targetDirectory).exists()) {
061: throw new Exception("Path '" + targetDirectory
062: + "' does not exist ");
063: }
064:
065: if (!(request instanceof MultipartHttpServletRequest)) {
066: throw new Exception(
067: "Invalid upload. Not a MultipartHttpServletRequest");
068: }
069:
070: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
071: List uploadedFiles = new ArrayList();
072: for (Iterator iter = multipartRequest.getFileNames(); iter
073: .hasNext();) {
074: MultipartFile file = multipartRequest.getFile((String) iter
075: .next());
076: if (file.getSize() <= 0)
077: continue;
078: String filename = parseFilename(file.getOriginalFilename());
079: filename = targetDirectory + "/" + filename;
080: file.transferTo(new File(filename));
081: uploadedFiles.add(filename);
082: }
083: return uploadedFiles;
084: }
085:
086: /**
087: * needed to support IE problems - when uploading a file in IE,
088: * the entire filename including full canonical path is used.
089: */
090: private String parseFilename(String inputName) {
091: String cleanFilename = inputName;
092: int idx = cleanFilename.lastIndexOf('/');
093:
094: if ((idx >= 0) && (idx < cleanFilename.length())) {
095: cleanFilename = cleanFilename.substring(idx + 1);
096: }
097:
098: idx = cleanFilename.lastIndexOf('\\');
099:
100: if ((idx >= 0) && (idx < cleanFilename.length())) {
101: cleanFilename = cleanFilename.substring(idx + 1);
102: }
103:
104: return cleanFilename;
105: }
106: }
|