两个JSP页面
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>File控件</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="doupload.jsp" enctype="multipart/form-data" method="post">
<p>姓名:<input type="text" name="user"></p>
<p>选择图片:<input type="file" name="nfile"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
JSP 页面!
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.util.*"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传处理页面</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String uploadFileName = ""; //上传的文件名
String fieldName = ""; //表单字段元素的name属性值
//请求信息中的内容是否是multipart类型
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//上传文件的存储路径(服务器文件系统上的绝对文件路径)
String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/" );
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//解析form表单中所有文件
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
String owid = null;
String owname = null;
String owuser = null;
String owidCard = null;
String owpassword = null;
String owhouseNumber = null; 赋值
String owhouseType = null;
String owcheckData = null;
String nfile = null;
String owphone = null;
String owhouseNature = null;
while (iter.hasNext()) { //依次处理每个文件
FileItem item = iter.next();
if (item.isFormField()){ //普通表单字段
fieldName = item.getFieldName(); //表单字段的name属性值
if(fieldName.equals("owid")){
owid = item.getString("UTF-8");
}else if(fieldName.equals("owname")){
owname = item.getString("UTF-8");
}else if(fieldName.equals("owuser")){
owuser = item.getString("UTF-8");
}else if(fieldName.equals("owidCard")){
owidCard = item.getString("UTF-8");
}else if(fieldName.equals("owpassword")){ 获取页面值
owpassword = item.getString("UTF-8");
}else if(fieldName.equals("owhouseNumber")){
owhouseNumber = item.getString("UTF-8");
}else if(fieldName.equals("owhouseType")){
owhouseType = item.getString("UTF-8");
}else if(fieldName.equals("owcheckData")){
owcheckData = item.getString("UTF-8");
}else if(fieldName.equals("owphone")){
owphone = item.getString("UTF-8");
}else if(fieldName.equals("owhouseNature")){
owhouseNature = item.getString("UTF-8");
}
if (fieldName.equals("user")){
//输出表单字段的值
out.print(item.getString("UTF-8")+"上传了文件。<br/>");
}
}else{ //文件表单字段
String fileName = item.getName();
if (fileName != null && !fileName.equals("")) {
File fullFile = new File(item.getName());
File saveFile = new File(uploadFilePath, fullFile.getName());
item.write(saveFile);
uploadFileName = fullFile.getName();
out.print("上传成功后的文件名是:"+uploadFileName);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>