使用common-fileupload框架实现文件上传

到http//jakarta.apache.org/commons/fileupload站点下载commons-fileupload-1.2.1-bin
到http//jakarta.apache.org/commons/io站点下载commons-io-1.4-bin
同时将两个文件解压开后,将commons-io-1.4.jar,commons-fileupload-1.2.1.jar文件拷贝到tomcat服务器Lib文件夹下面
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>   <head>     <base href="<%=basePath%>">         <title>了解文件上传的原理</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!--  <link rel="stylesheet" type="text/css" href="styles.css">  -->   </head>     <body>    <form action="pro2.jsp" enctype="multipart/form-data" method="post">     上传文件:<input type="file" name="file" /><br>     请求参数:<input type="text" name="wawa" /><br>     <input type="submit" name="dd" value="提交">    </form>   </body> </html>

pro2.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*, java.io.* "  %>
<%@ page import="java.util.*"%>
<%
	DiskFileItemFactory factory = new DiskFileItemFactory();
	factory.setSizeThreshold(1024*1024*20);
	factory.setRepository(new File(request.getRealPath("/")));
	ServletFileUpload upload = new ServletFileUpload(factory);
	upload.setSizeMax(1024*1024*20);
	List items = upload.parseRequest(request);
	for(Iterator it=items.iterator(); it.hasNext();){
		FileItem item = (FileItem)it.next();
		//判断是否为表单域
		if(item.isFormField()){
			String name = item.getFieldName();
			String value = item.getString("UTF-8");
			out.println("name=value:"+"name="+name+",value="+value);
		}else {
			//获取文件域名
			String fieldName = item.getFieldName();
			//获取文件名
			String fileName = item.getName();
			//获得文件类型
			String contentType = item.getContentType();
			FileOutputStream fos = new FileOutputStream(request.getRealPath("/") + System.currentTimeMillis()+
			fileName.subSequence(fileName.indexOf("."), fileName.length()));
			if(item.isInMemory()){
				fos.write(item.get());
			}else {
				InputStream is = item.getInputStream();
				byte [] buffer = new byte[1024];
				int len;
				while((len = is.read(buffer)) >1){
					fos.write(buffer, 0, len);
				}
				is.close();
				fos.close();
			}
			
		}
	}
	
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用common-fileupdate框架上传文件</title>
</head>
<body>

</body>
</html>

你可能感兴趣的:(使用common-fileupload框架实现文件上传)