您的位置:首页 > 其它

使用common-fileUpload实现文件上传

2015-08-13 20:37 561 查看
通过common-fileUpload文件上传组件,可以实现上传文档、图片等资料。根据程序的不同要求,它可以有多种方式的应用。

我们这里介绍一种简单的例子,来实现文件上传功能。

准备:

引入相关组建的jar包



首先,我们进入上传页面index.html.



<body>
		<form action="./servlet/item/FileUploadServlet" method="post"
			enctype="multipart/form-data" name="form1">
			<input type="file" name="file">
			<input type="submit" name="Submit" value="upload">
		</form>
		<form action="./servlet/HelloWord" method="post">
			<input type="submit" />
		</form>
		<form name="uploadform" method="POST" action="./servlet/item/FileUploadServlet"
			ENCTYPE="multipart/form-data">
			<table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9BD7FF">
				<tr>
					<td width="100%" colspan="2">
						文件
						<input name="x" size="40" type="file">
					</td>
				</tr>
			</table>
			<table>
				<tr>
					<td align="center">
						<input name="upload" type="submit" value="开始上传" />
					</td>
				</tr>

			</table>

		</form>

	</body>


选择要上传的文件,点击开始上传,跳转到servlet页面,即下面提到的FileUploadServlet。

/**
 * 实现文件上传的Servlet
 * @author Administrator
 *
 */
public class FileUploadServlet extends HttpServlet {

	
	//上传路径
	private File uploadPath;
	
	
	//当文件过大时,需要设置一个临时路径
	private File tempPath;
	
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 内存存储的最大值
		factory.setSizeThreshold(4096);
		
		factory.setRepository(tempPath);

		ServletFileUpload upload = new ServletFileUpload(factory);
		//设置文件上传大小
		upload.setSizeMax(1000000 * 20);
		try {
			List fileItems = upload.parseRequest(req);
			String itemNo = "";
			for (Iterator iter = fileItems.iterator(); iter.hasNext();) {
				FileItem item = (FileItem) iter.next();
				
				//是普通的表单输入域
				if(item.isFormField()) {
					if ("itemNo".equals(item.getFieldName())) {
						itemNo = item.getString();
					}
				}
				//是否为input="type"输入域
				if (!item.isFormField()) {
					String fileName = item.getName();
					long size = item.getSize();
					if ((fileName == null || fileName.equals("")) && size == 0) {
						continue;
					}
					//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG
					fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
					//item.write(new File(uploadPath + itemNo + ".gif"));
					item.write(new File(uploadPath, itemNo + ".gif"));
				}
			}
			//重定向页面
			res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化方法,设定目录
	 */
	public void init() throws ServletException {
		uploadPath = new File(getServletContext().getRealPath("upload"));
		//System.out.println("uploadPath=====" + uploadPath);
		//如果目录不存在
		if (!uploadPath.exists()) {
			//创建目录
			uploadPath.mkdir();
		}
		
		//临时目录
		tempPath = new File(getServletContext().getRealPath("temp"));
		if (!tempPath.exists()) {
			tempPath.mkdir();
		}
	}
}
小结:

FileUpload上传一个文件的过程中可以分为三个部分:首先,客户端与服务器端建立连接,把要上传的文件生成request数据流。其次服务器端接收request流,将流缓存到内存或磁盘中。当文件过大时,文件将被缓存到内存,否则将被缓存到磁盘的临时文件。最后由服务器端的内存或是临时文件中把文件输出到指定的目录,这个目录才是指定的文件上传目录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: