文件提交注意点:
首先导入架包·
1.form提交方式是post
2.form必须添加 enctype="multipart/form-data"
3.这个jsp 提交的文件是 个人照 file文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'addData.jsp' starting page</title>
</head>
<body>
<!--该页面用于向数据库添加数据-->
<h2>填写需要添加的数据</h2>
<form method="post" action="doaddupload.jsp" enctype="multipart/form-data">
<table border="1px">
<tr>
<td>学生编号</td>
<td><input type="text" name="studentno"/></td>
</tr>
<tr>
<td>登录密码</td>
<td><input type="text" name="loginpwd"/></td>
</tr>
<tr>
<td>学生姓名</td>
<td><input type="text" name="studentname"/></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" name="sex"/></td>
</tr>
<tr>
<td>年级号</td>
<td><input type="text" name="gradeID"/></td>
</tr>
<tr>
<td>手机号码</td>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td>出生日期</td>
<td><input type="date" name="borndate"/></td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>身份证号</td>
<td><input type="text" name="identifyId"/></td>
</tr>
<tr>
<td>个人照</td>
<td><input type="file" name="photo"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
提交跳转页面
<%@ page import="com.hfxt.entity.Student" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<%@ page import="com.hfxt.service.StudentService" %>
<%@ page import="com.hfxt.service.impl.StudentServiceImpl" %><%--
Created by IntelliJ IDEA.
User: LiuJun
Date: 2020/10/19 0019
Time: 10:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Student stu=new Student();
// 1,判断是否是上传类型
boolean isMultipart= ServletFileUpload.isMultipartContent(request);
//获取上传的路径
String uploadpath=request.getSession().getServletContext().getRealPath("images");
if (isMultipart){//上传表单
//1.创建文件工厂对象
FileItemFactory factory=new DiskFileItemFactory();
//2.创建上传对象
ServletFileUpload upload=new ServletFileUpload(factory);
//3、解析form表单所有文件
List<FileItem> items = upload.parseRequest(request);
//4.通过迭代器遍历集合
Iterator<FileItem> iter=items.iterator();
//5.从迭代器中取数据
while (iter.hasNext())
{
FileItem item=iter.next();
if (item.isFormField()){//判断是否是普通表单
if (item.getFieldName().equals("studentno"))
{
stu.setStudentNo(Integer.parseInt(item.getString()));
}
if (item.getFieldName().equals("loginpwd"))
{
stu.setLoginPWd(item.getString("utf-8"));
} if (item.getFieldName().equals("studentname"))
{
stu.setStudentName(item.getString("utf-8"));
} if (item.getFieldName().equals("sex"))
{
stu.setSex(Integer.parseInt(item.getString()));
} if (item.getFieldName().equals("gradeID"))
{
stu.setGradeId(Integer.parseInt(item.getString("utf-8")));
} if (item.getFieldName().equals("phone"))
{
stu.setPhone(item.getString());
}
if (item.getFieldName().equals("address"))
{
stu.setAddress(item.getString("utf-8"));
}
if (item.getFieldName().equals("borndate"))
{
stu.setBornDate(item.getString("utf-8"));
}
if (item.getFieldName().equals("email"))
{
stu.setEmail(item.getString("utf-8"));
}
if (item.getFieldName().equals("identifyId"))
{
stu.setIdentityCard(item.getString("utf-8"));
}
}
else {//上传表单
String filename=item.getName();
stu.setPhoto(filename);
//封装文件
File myfile=new File(uploadpath,filename);
item.write(myfile);
}
}
StudentService ss=new StudentServiceImpl();
int result=ss.add(stu);
if(result==1){
out.print("<script>alert('添加成功');location.href='index.jsp'</script>");
}else{
out.print("<script>alert('添加失败');location.href='addUpData.jsp'</script>");
}
}
%>
下载 文件
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.OutputStream" %><%--
Created by IntelliJ IDEA.
User: LiuJun
Date: 2020/10/19 0019
Time: 14:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
out.clear();
out=pageContext.pushBody();
String filename=request.getParameter("filename");
if (filename == null) {
return;
}
//创建输入流读取文件
InputStream is=request.getServletContext().getResourceAsStream("images/"+filename);
//输出流写入新位置
OutputStream os=response.getOutputStream();
//设置下载参数
response.setHeader("Content-Type","application/octet-stream");
response.setHeader("Content-Length",String.valueOf(is.available()));
response.setHeader("Content-Disposition","attachment;filename="+filename);
byte[] arr=new byte[1024];
int len=0;
while ((len=is.read(arr))!=-1)
{
os.write(arr,0,len);
}
os.close();
is.close();
%>