servlet接受form表单d值和文件上传

时间:2022-11-22 14:14:34

ApacheCommons组件  commons-fileupload-1.3.2.jar和commons-io_2.4.jar

Servlet页面

package com.studentweb.mode;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.studentweb.control.StudentUtil;

@SuppressWarnings("serial")
public class AddStudent extends HttpServlet {

@SuppressWarnings("rawtypes")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StudentUtil showStudent = new StudentUtil();

DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload diskFileUpload = new ServletFileUpload(factory);
diskFileUpload.setHeaderEncoding("utf-8");
// 定义一个hashmap存放请求参数
HashMap<String, String> parameters = new HashMap<String, String>();
List paramItems = null;
try {
paramItems = diskFileUpload.parseRequest(request);

} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator i = paramItems.iterator();
// 依次出来每个文件 请求参数
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (!fi.isFormField()) {//判断是文件还是 value值
String fileName = fi.getName();
String savaFile = "images/" + fileName;
String filePath = getServletContext().getRealPath("/")
+ savaFile;
File file = new File(filePath);
try {
fi.write(file);
} catch (Exception e) {
e.printStackTrace();
}
parameters.put("header_url", savaFile);
} else {
String name = fi.getFieldName();
String value = null;
try {
value = fi.getString("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
parameters.put(name, value);
}
}
String name = parameters.get("name");
String Age = parameters.get("age");
String grade = parameters.get("grade");
String gender = parameters.get("gender");
String headerUrl = parameters.get("header_url");
int age = Integer.parseInt(Age);
boolean a = showStudent.SelectStudent(name);
if (a) {
response.sendRedirect("/StudentWebV_2.1/StudentThereare.html");
} else {
showStudent.addStudnet(grade, name, age, gender, headerUrl);
response.sendRedirect("/StudentWebV_2.1/jump.html");
}

}
}
JSP 页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<style type="text/css">
body {
font-size: 20px;
padding-bottom: 40px;
background-color: white;
}

.sidebar-nav {
padding: 9px 0;
}

@media ( max-width : 980px) { /* Enable use of floated navbar text */
.navbar-text.pull-right {
float: none;
padding-left: 5px;
padding-right: 5px;
}
}

.btn {
position: absolute;
top: 30%;
left: 40%;
color: blue;
font-size: 20px;
}
</style>
<script type="text/javascript">
function checkInput() {
var name = document.form1.name1.value;
var age = document.form1.age.value;
var a =isNaN(age);
if(name == "") {
alert("请输入姓名!");
return false;
} else if(age == "") {
alert("请输入年龄!");
return false;
} else if(a){
alert("年龄有误!");
return false;
}else{
return true;
}
}
</script>
</head>

<body>
<form name="form1" action="servlet/AddStudent" method="post"
onsubmit="return checkInput()" enctype="multipart/form-data">
<table class="table table-bordered table-hover m10"
style="margin-left:10px;margin-top:3px;">
<tr>
<td class="tableleft">姓名</td>
<td><input type="text" name="name" /></td>
<td class="tableleft">年龄</td>
<td><input type="text" name="age" /></td>
</tr>
<br>
<tr>
<br>
<td width="10%" class="tableleft">学生班级</td>
<td><select name="grade">
<option value='java' /> Java
<option value='安卓' /> 安卓
</select>
<td width="10%" class="tableleft">学生性别</td>
<td><select name="gender">
<option value='男' /> 男
<option value='女' /> 女
</select> <br />头像: <input type="file" name="myfile" /><br> <input type="submit" value="提交" class="btn">
</form>
</body>

</html>