fileupload上传文件的简单例子

下面是用fileupload上传文件的简单工具类:

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;

public class UpLoadUtil2 {
	
	public static String upload(HttpServletRequest request){
		String msg = null;
		
		try{
			boolean isMultipart = FileUploadBase.isMultipartContent(request);
			if(isMultipart){//判断Form是否为 multipart form
				FileItemFactory fileItemFactory = new DiskFileItemFactory();//设置工厂
				FileUpload fileUpload = new FileUpload(fileItemFactory);//上传组件
				
				//这里就是中文文件名处理的代码,其实只有一行,setHeaderEncoding就可以了
				fileUpload.setHeaderEncoding("utf-8");
				
				List fileItemList = fileUpload.parseRequest(request);//解析上传文件数据包
				
				for(int i=0;i<fileItemList.size();i++){
					FileItem fileItem = (FileItem)fileItemList.get(i);//遍历每个上传文件
					
					if(!fileItem.isFormField()){//判断是不是上传的文件
						String fullFileName = fileItem.getName();//文件全名
						String fileName = getFileName(fullFileName);//文件名
						//设置文件存储在服务器上的路径
						String path = request.getRealPath("/upload");
						if(!new File(path).isDirectory())//如果不存在,则创建目录
							new File(path).mkdirs(); 
						File file = new File(path, fileName);
						fileItem.write(file);
						
						msg = file.getAbsolutePath();
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return msg;
	}
	
	public static String getFileName(String fullFileName){
		String fileName = fullFileName.substring(fullFileName.lastIndexOf("\\")+1);
		return fileName;
	}

}

 如上upload方法中传入request对象参数,fileupload组件通过解析request得到上传文件。

下面是页面的部分代码,用来发送上传文件请求:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'uploadExcel.jsp' starting page</title>
		<script type="text/javascript">
			function tosubmit(){
				var filename = document.getElementById('fileid').value;
				if(filename =='' || filename == null){
					alert("请先选择要上传的文件");
				}else{
					window.document.form1.submit();
				}
			}
		</script>
	</head>

	<body>
		<form method="post" name="form1"
			action="<%=request.getContextPath()%>/uploadExcelServlet"
			enctype="multipart/form-data" >
			<table class="formLayout">
				<tr>
					<td align="right">
						选择要上传的文件:
					</td>
					<td align="left">
						<input type="file" id="fileid" name="filename"/>
					</td>
				</tr>
				<tr>
					<td align="right">
						&nbsp;
					</td>
					<td align="left">
						<input type="button" value="确定上传" class="btn_width"
							onclick="tosubmit()">
						&nbsp;&nbsp;
						<input type="button" value="返回" onclick="window.history.back()"
							class="btn_width">
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

 

你可能感兴趣的:(JavaScript,apache,html,jsp,servlet)