⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileupload.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
package org.jahia.tools.files;/* * FileUpload * Copyright (c) 2000 Jahia Ltd  All rights reserved. *  */import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import com.oreilly.servlet.*;/** * Class FileUpload. * Class to handle fileupload * It's essentially a wrapper for the Multipart Request class * of the com.oreilly.servlet.Multipart class *  * @author Khue ng * @version 1.0 */public class FileUpload {	private static final String REQ_CONTENT_TYPE = "multipart/form-data";   	private MultipartRequest m_MultiPartReq = null;   	private Hashtable m_QueryStringParams;   	private ServletContext m_Context;   	private HttpServletRequest m_Req;   	private String m_SavePath = "";   	private int m_FileMaxSize = 1024 * 1024; // 1 MB	/**	 * Constructor	 */		 public FileUpload(ServletContext context, HttpServletRequest req) throws IOException{	   m_Context = context; 	   m_Req = req;	   init();	 }	/**	 * Constructor	 *	 * @param context the servletContext object	 * @param HttpservletRequest req 	 * @param HttpservletResponse res	 * @param savePath the path where files should be saved	 */	public FileUpload(ServletContext context, HttpServletRequest req, String savePath) throws IOException{			 	m_Context = context;		m_Req = req;		m_SavePath = savePath;		init();	}	/**	 * Constructor	 *	 * @param context the servletContext object	 * @param HttpservletRequest req 	 * @param HttpservletResponse res	 * @param savePath the path where files should be saved	 * @param fileMaxSize the max size of file to upload	 */	public FileUpload(ServletContext context, HttpServletRequest req, String savePath, int fileMaxSize) throws IOException{			 	m_Context = context;		m_Req = req;		m_FileMaxSize = fileMaxSize;	   	m_SavePath = savePath;  	   	init();	}   /**    * Init the MultiPartReq object if it's actually null    *    * @exception IOException    */   protected void init() throws IOException {      if	( m_MultiPartReq == null ) {		   if ( checkSavePath(m_SavePath) ){                     try {                              m_MultiPartReq = new MultipartRequest(m_Req, m_SavePath, m_FileMaxSize);      	         	   } catch (IOException ioe) {         	         	   toConsole ("FileUpload::init, IOException " + ioe.getMessage());         	   throw new IOException(ioe.getMessage());      	   }      	      	} else {           	toConsole("FileUpload::init storage path does not exists or can write");           	throw new IOException("FileUpload::init storage path does not exists or cannot write");		   }   		     }   	 parseQueryString();      }      	/**	 * Method getFiles()	 *	 * @return a Vector of uploaded files	 * @exception IOException	 */	public Vector getFiles() throws IOException {		toConsole("FileUpload::getFiles() , fileMaxSize = " + m_FileMaxSize );		toConsole("FileUpload::getFiles() , SavePath = " + m_SavePath );    	Enumeration fileNames = m_MultiPartReq.getFileNames();   	  		  		File tmpFile = null;	   	Vector files = new Vector();   	  		  		while (fileNames.hasMoreElements())		{     	 	tmpFile = m_MultiPartReq.getFile( (String)fileNames.nextElement() );     	 	if ( tmpFile != null ) {     	 		files.addElement( tmpFile );     	 	}		}				return files;	}   /**	 * Return an enumeration of the filenames	 *	 * @return a Enumeration of filenames	 * @exception IOException	 */	public Enumeration getFileNames() {       return m_MultiPartReq.getFileNames();   }	/**	 * Return a file object	 *	 * @param filename the name of the file	 * @return a File object	 */	public File getFile(String filename) {      return m_MultiPartReq.getFile(filename);   }	/**	 * Return a file contentType	 *	 * @param filename the name of the file	 * @return the Content Type as String	 */	public String getFileContentType(String filename) {      return m_MultiPartReq.getContentType(filename);   }	/**	 * Return a file SystemName ( the real name )	 *	 * @param filename the name of the file	 * @return the ystem Name as String	 */	public String getFileSystemName(String filename) {       return m_MultiPartReq.getFilesystemName(filename);   }	/**	 * Return an enumeration of parameters name	 *	 * @return an enumeration of parameters name	 */	public Enumeration getParameterNames() {       return m_MultiPartReq.getParameterNames();   }	/**	 * Return the value of a parameter	 *	 * @param paramName the name of the parameter	 * @return the value of a paramater as String	 */	public String getParameter(String paramName) {			    if ( m_MultiPartReq.getParameter(paramName)==null ){	    	return (String)m_QueryStringParams.get(paramName);	   	}	   	return	m_MultiPartReq.getParameter(paramName);   	}	   	/**	 * Return the value of a parameter	 *	 * @param paramName the name of the parameter	 * @return the value of a paramater as String	 */	public String getQueryParameter(String paramName) {				return (String)m_QueryStringParams.get(paramName);	}		/**	 * Return the values of a parameter	 *	 * @param paramName the name of the parameter	 * @return the values of a paramater as a String Array	 */	public String[] getParameterValues(String paramName) {       return m_MultiPartReq.getParameterValues(paramName);   	}		/**	 * Method checkSavePath	 * Check if the path where to save files is valid	 *	 * @param path the relative path where to save files to	 * @return true if this directory exists, and can be write to 	 */	protected boolean checkSavePath(String path){				if ( path != null && (path.length()>0)	){						toConsole("FileUpload::checkSavePath(), path is " + path);			File tmpFile = new File(path);			if ( tmpFile != null && tmpFile.isDirectory() && tmpFile.canWrite() ) {				m_SavePath = path;				return true;			} else {				return false;			}		} else {			return false;		}	}	  	/**   	 * delete temporary storage folder   	 */  	public void deleteUploadFolder(){         	File tmpDir = new File(m_SavePath);     	if ( tmpDir != null && tmpDir.isDirectory() && tmpDir.canWrite() ) {        	tmpDir.delete();     	}  	}  			/**  	 * Parse a query String and create an hashtable of parameter/value 	 *  	 * @return java.lang.String 	 */ 	protected void parseQueryString() {   		 		m_QueryStringParams = new Hashtable();		toConsole(m_Req.getQueryString());  		StringTokenizer tokenizer = new StringTokenizer(m_Req.getQueryString(),"&");   		Vector params = new Vector();  		while (tokenizer.hasMoreTokens())     	{   	     	params.add(tokenizer.nextToken());  	    } 				String param = null;		int size = params.size();		int pos = 0;		for (int i=0 ; i<size ; i++){			param = (String)params.get(i);			pos = param.indexOf("=");			if ( pos>0 ){				m_QueryStringParams.put(param.substring(0,pos), param.substring(pos+1,param.length()) );				}					}    }     //-------------------------------------------------------------------------    private synchronized void toConsole (String message)    {        if (false)        {            //System.out.println (message);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -