Date: 2005-03-06T00:31:31 Editor: DakotaJack Wiki: Apache Struts Wiki Page: StrutsUpload URL: http://wiki.apache.org/struts/StrutsUpload
no comment Change Log: ------------------------------------------------------------------------------ @@ -14,32 +14,27 @@ throws Exception { HttpSession sess = req.getSession(); Upload upload = (Upload)sess.getAttribute(UploadConstant.UPLOAD); - if(upload == null) { upload = new Upload(); sess.setAttribute(UploadConstant.UPLOAD,upload); } - if(upload.isMultipart(req)) { Monitor progress = (UploadMonitorProgress)sess.getAttribute(UploadConstant.MONITOR); - if(progress == null) { progress = new UploadMonitorProgress(); sess.setAttribute(UploadConstant.MONITOR,progress); } - List monitors = Collections.synchronizedList(new LinkedList()); + int size = UploadConstant.MAX_FILE_SIZE; + int type = UploadConstant.DIR; + String path = LoadFileUtil.getUploadPath(type,user.getId()); + String temp = LoadFileUtil.UPLOAD_TEMP_FILES; + String code = UploadConstant.DEFAULT_ENCODING; monitors.add(progress); - String userStore = LoadFileUtil.getUploadPath(user.getId()); - - upload.setStore(UploadConstant.DIR,userStore); - upload.setStoreTmpDir(LoadFileUtil.UPLOAD_TEMP_FILES); - upload.process(req,monitors,UploadConstant.DEFAULT_ENCODING,UploadConstant.MAX_UPLOADED_FILE_SIZE); - upload.store(); - + upload.process(req,monitors,code,size); + upload.store(type, path, temp); sess.setAttribute(UploadConstant.REPORT,UploadReport.getReport(upload)); } - return new ActionForward(...);; } } @@ -74,6 +69,7 @@ 2. !UploadFileItem (implements org.apache.commons.fileupload.!FileItem) 3. !UploadFileItemFactory (extends org.apache.commons.fileupload.!DefaultFileItemFactory) 4. Monitor + 5. Upload == Code == @@ -280,13 +276,13 @@ List monitors) throws UploadException, IOException { - this(req, monitors, UploadConstant.MAX_UPLOADED_FILE_SIZE); + this(req, monitors, UploadConstant.MAX_FILE_SIZE); } public UploadMultipartData(HttpServletRequest req) throws UploadException, IOException { - this(req, null, UploadConstant.MAX_UPLOADED_FILE_SIZE); + this(req, null, UploadConstant.MAX_FILE_SIZE); } public Iterator getParameterNames() { @@ -720,6 +716,404 @@ public void read(int contentLength); public void notify(UploadParams uploadParams, MultipartFile uploadFile); } +}}} + +=== Upload === + +{{{ +public class Upload { + private MultipartData data; + + private List monitors = Collections.synchronizedList(new LinkedList()); + private List history = Collections.synchronizedList(new LinkedList()); + + private List excludedExts = Collections.synchronizedList(new LinkedList()); + private List includedExts = Collections.synchronizedList(new LinkedList()); + private boolean isExcludedExts = true; + + private String storeLocation; + private int storeType = 0; + private List storeMemory = Collections.synchronizedList(new LinkedList()); + + private int maxFileNumber = -1; + private long maxFileSize = 0x100000000L; + + private boolean overwrite = false; + + public Upload() {} + + public int getStoreType() { return storeType; } + public List getStoreMemory() { return storeMemory; } + public String getStoreLocation() { return storeLocation; } + + public boolean isExcludedExts() { return isExcludedExts; } + public long getFileSizeLimit() { return maxFileSize; } + public boolean getOverwrite() { return overwrite; } + public int getMaxFiles() { return maxFileNumber; } + public String getSystemTempDir() { return UploadConstant.SYSTEM_TEMPDIR; } + public List getHistory() { return history; } + + public void store(int storeType, + String storeLocation, + String tmpPath) // location null if memory + throws UploadException, + IOException { + setStore(storeType,storeLocation); + setStoreTmpDir(tmpPath); + Map files = data.getFiles(); + Iterator iter = files.keySet().iterator(); + String fileName; + MultipartFile multipartFile; + + while(iter.hasNext()) { + fileName = (String)iter.next(); + multipartFile = (MultipartFile)files.get(fileName); + + if(storeType == UploadConstant.DIR && storeLocation != null) { + folder(multipartFile); + } else if(storeType == UploadConstant.ZIP && storeLocation != null) { + zip(multipartFile); + } else if(storeType == UploadConstant.MEM && storeMemory != null) { + memory(multipartFile); + } else { + throw new UploadException(UploadConstant.STORE_NOT_SET); + } + } + } + + public void setMaxFiles(int maxFileNumber) { this.maxFileNumber = maxFileNumber; } + public void setFileSizeLimit(long maxFileSize) { this.maxFileSize = maxFileSize; } + public void setOverwrite(boolean overwrite) { this.overwrite = overwrite; } + + public void setExcludedExts(String exts) { + StringTokenizer st = new StringTokenizer(exts, ","); + while(st.hasMoreElements()) { + excludedExts.add(st.nextElement()); + } + isExcludedExts = true; + } + + public void setIncludedExts(String exts) { + StringTokenizer st = new StringTokenizer(exts, ","); + + while(st.hasMoreElements()) { + includedExts.add(st.nextElement()); + } + + isExcludedExts = false; + } + + public String getExcludedExts() { + StringBuffer sb = new StringBuffer(); + Iterator iter = excludedExts.iterator(); + boolean first = true; + + while(iter.hasNext()) { + if(first) { + sb.append((String)iter.next()); + first = false; + } + sb.append(","); + sb.append((String)iter.next()); + } + + return sb.toString(); + } + + public String getIncludedExts() { + StringBuffer sb = new StringBuffer(); + Iterator iter = includedExts.iterator(); + boolean first = true; + + while(iter.hasNext()) { + if(first) { + sb.append((String)iter.next()); + first = false; + } + sb.append(","); + sb.append((String)iter.next()); + } + + return sb.toString(); + } + + public boolean isMultipart(HttpServletRequest req) { + return MultipartUtil.isMultipartFormData(req); + } + + public void process(HttpServletRequest req, + List monitors, + String encoding, + int maxFileSize) + throws UploadException, + IOException { + this.monitors = monitors; + data = new UploadMultipartData(req,monitors,UploadConstant.DEFAULT_ENCODING,UploadConstant.MAX_FILE_SIZE); + } + + public Iterator getParameterNames() { + return data.getParameterNames(); + } + + public void clear() + throws UploadException, + IOException { + if(storeType == UploadConstant.DIR && storeLocation != null) { + File file = new File(storeLocation + UploadConstant.SEPARATOR); + + if(file.exists()) { + if(!file.canWrite()) { + throw new UploadException(UploadConstant.READ_ONLY); + } + + File files[] = file.listFiles(); + + if(files != null) { + for(int i = 0; i < files.length; i++) { + if(!files[i].delete()) { + throw new UploadException(UploadConstant.CANNOT_DELETE); + } + } + } + + setStore(UploadConstant.DIR,storeLocation); + history.clear(); + } + } else if(storeType == UploadConstant.ZIP && storeLocation != null) { + File file = new File(storeLocation); + + if(!file.delete()) { + throw new UploadException(UploadConstant.CANNOT_DELETE); + } + + setStore(UploadConstant.ZIP,storeLocation); + history.clear(); + } else if(storeType == UploadConstant.MEM && storeMemory != null) { + Iterator iter = storeMemory.iterator(); + while(iter.hasNext()) { + ((MultipartFile)iter.next()).reset(); + } + + storeMemory.clear(); + history.clear(); + } + } + + //---------------------- UTILITY METHODS ---------------------- + + private void setStore(int storeType, + String storeLocation) // location null if memory + throws UploadException, + IOException { + if(storeType == UploadConstant.MEM) { + this.storeType = storeType; + } else if(storeType == UploadConstant.DIR) { + this.storeType = storeType; + this.storeLocation = storeLocation; + File file = new File(storeLocation + UploadConstant.SEPARATOR); + if(file.exists()) { + if(!file.canWrite()) { + throw new UploadException(UploadConstant.READ_ONLY); + } + } else if(!file.mkdirs()) { + throw new UploadException(UploadConstant.CANNOT_CREATE); + } + } else if(storeType == UploadConstant.ZIP) { + this.storeType = storeType; + this.storeLocation = storeLocation; + File file = new File(storeLocation); + this.storeLocation = storeLocation; + if(!file.exists()) { + FileOutputStream fos = new FileOutputStream(storeLocation); + ZipOutputStream zos = new ZipOutputStream(fos); + zos.putNextEntry(new ZipEntry(UploadConstant.BLANK)); + zos.closeEntry(); + zos.close(); + fos.close(); + } + } else { + throw new UploadException(UploadConstant.NONEXISTING_TYPE); + } + } + + private void setStoreTmpDir(String tmpPath) { + if(tmpPath != null) { + File file = new File(tmpPath); + + if(!file.exists()) { + file.mkdirs(); + } + } + } + + private boolean checkIncludedExts(String fileName) { + boolean included = false; + Iterator iter = includedExts.iterator(); + int i = fileName.lastIndexOf("."); + String extFile = null; + if(i != -1) { + extFile = fileName.substring(i + 1, fileName.length()); + while(iter.hasNext()) { + String ext = (String)iter.next(); + if(ext.equalsIgnoreCase(extFile)) { + return included = true; + } + } + } else { + return included = false; + } + return included = false; + } + + private void folder(MultipartFile uploadFile) + throws UploadException, + IOException { + if(uploadFile != null && + uploadFile.getName() != null && + !uploadFile.getName().equals("") && + uploadFile.getSize() >= 0L) { + if(uploadFile.getSize() > maxFileSize) { + throw new UploadException(UploadConstant.UPLOAD_FILE_SIZE_LIMIT_REACHED + " " + uploadFile.getName()); + } + + File directory = new File(getStoreLocation() + UploadConstant.SEPARATOR); + File files[] = directory.listFiles(); + int numberOfFiles = files.length; + boolean isSameName = false; + + for(int j = 0; j < numberOfFiles; j++) { + if(files[j].getName().equals(uploadFile.getName())) { + isSameName = true; + } + } + + if(maxFileNumber == -1 || numberOfFiles < maxFileNumber) { + if(extBarred(uploadFile.getName())) { + throw new UploadException(UploadConstant.UPLOAD_FILENAME_DENIED + " " + uploadFile.getName()); + } + + FileOutputStream fos = null; + String uploadFileName = null; + + if(!isSameName) { + fos = new FileOutputStream(getStoreLocation() + UploadConstant.SEPARATOR + uploadFile.getName()); + } else if(!overwrite) { + uploadFileName = uploadFile.getName() + ".overwrite." + new Date().getTime(); + fos = new FileOutputStream(getStoreLocation() + UploadConstant.SEPARATOR + uploadFileName); + } else { + fos = new FileOutputStream(getStoreLocation() + UploadConstant.SEPARATOR + uploadFile.getName()); + } + + InputStream is = uploadFile.getInputStream(); + + if(is != null) { + byte streamData [] = new byte[UploadConstant.BUFFER_SIZE]; + + for(int i = 0; (i = is.read(streamData)) != -1;) { + fos.write(streamData, 0, i); + } + } + + fos.close(); + is.close(); + UploadParams uploadParams = new UploadParams(uploadFile.getName(), uploadFile.getSize(), uploadFile.getContentType(), 1, storeLocation, uploadFileName); + history.add(uploadParams); + notify(uploadParams, uploadFile); + } else { + throw new UploadException(UploadConstant.UPLOAD_LIMIT_REACHED); + } + } + } + + private void zip(MultipartFile uploadFile) + throws UploadException, + IOException { + if(uploadFile.getName() != null && !uploadFile.getName().equals("") && uploadFile.getSize() >= 0L) { + if(uploadFile.getSize() > maxFileSize) { + throw new UploadException(UploadConstant.UPLOAD_FILE_SIZE_LIMIT_REACHED + " " + uploadFile.getName()); + } + + if(extBarred(uploadFile.getName())) { + throw new UploadException(UploadConstant.UPLOAD_FILENAME_DENIED + " " + uploadFile.getName()); + } + + UploadStore uploadStore = UploadStore.getInstance(); + String s1 = uploadStore.append(storeLocation, uploadFile, maxFileNumber, overwrite); + UploadParams uploadParams = new UploadParams(uploadFile.getName(), uploadFile.getSize(), uploadFile.getContentType(), 3, storeLocation, s1); + + history.add(uploadParams); + notify(uploadParams, uploadFile); + } + } + + private void memory(MultipartFile uploadFile) + throws UploadException, + IOException { + if(uploadFile.getName() != null && !uploadFile.getName().equals("") && uploadFile.getSize() >= 0L) { + if(uploadFile.getSize() > maxFileSize) { + throw new UploadException(UploadConstant.UPLOAD_FILE_SIZE_LIMIT_REACHED + " " + uploadFile.getName()); + } + + if(extBarred(uploadFile.getName())) { + throw new UploadException(UploadConstant.UPLOAD_FILENAME_DENIED + " " + uploadFile.getName()); + } + + if(maxFileNumber != -1 && storeMemory.size() >= maxFileNumber) { + throw new UploadException(UploadConstant.UPLOAD_LIMIT_REACHED); + } + + storeMemory.add(uploadFile); + UploadParams uploadParams = new UploadParams(uploadFile.getName(), uploadFile.getSize(), uploadFile.getContentType(), 0, UploadConstant.MEMORY, null); + history.add(uploadParams); + notify(uploadParams, uploadFile); + } + } + + private void notify(UploadParams uploadParams, + MultipartFile uploadFile) { + Iterator iter = monitors.iterator(); + while(iter.hasNext()) { + ((Monitor)iter.next()).notify(uploadParams,uploadFile); + } + } + + private void setStoreType(int storeType) + throws UploadException { + if(storeType == UploadConstant.MEM || + storeType == UploadConstant.DIR || + storeType == UploadConstant.ZIP) { + this.storeType = storeType; + } else { + throw new UploadException(UploadConstant.NONEXISTING_TYPE); + } + } + + private boolean extBarred(String fileName) { + if(isExcludedExts) { + return checkExcludedExts(fileName); + } + return !checkIncludedExts(fileName); + } + + private boolean checkExcludedExts(String fileName) { + boolean excluded = false; + Iterator iter = excludedExts.iterator(); + int i = fileName.lastIndexOf("."); + String extFile = null; + if(i != -1) { + extFile = fileName.substring(i + 1, fileName.length()); + while(iter.hasNext()) { + String ext = (String)iter.next(); + if(ext.equalsIgnoreCase(extFile)) { + return excluded = true; + } + } + } else { + return excluded = true; + } + return excluded; + } +} }}} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]