Jsp页面实现文件上传下载
po层
public class Up {
private int id;
private String yn;
private String pn;
public int getId() {
return id;
}
public Up(int id, String yn, String pn) {
this.id = id;
this.yn = yn;
this.pn = pn;
}
public void setId(int id) {
this.id = id;
}
public String getYn() {
return yn;
}
public void setYn(String yn) {
this.yn = yn;
}
public String getPn() {
return pn;
}
public void setPn(String pn) {
this.pn = pn;
}
}
dao层
public interface UpDao {
public void add(Up up);
public List<Up> list();
}
连接数据库方法
public abstract class BaseDao {
private final String user = "scott";
private final String pass = "tiger";
private final String url = "jdbc:oracle:thin:@192.168.90.38:1521:orcl";
public ResultSet rs = null;
public PreparedStatement pstm = null;
public Connection conn = null;
public Connection getConn() {
try {
DataSource ds = new ComboPooledDataSource ();
conn = ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void close() {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(pstm != null) {
pstm.close();
pstm = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public class UpDaoImpl extends BaseDao implements UpDao{
@Override
public void add(Up up) {
// TODO Auto-generated method stub
try {
String sql="insert into up(id,yn,pn) values(seq_gz_id.nextval,?,?)";
conn = getConn();
pstm = conn.prepareStatement(sql);
pstm.setString(1, up.getYn());
pstm.setString(2,up.getPn());
pstm.execute();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
close();
}
}
@Override
public List<Up> list() {
// TODO Auto-generated method stub
List<Up> list = new ArrayList<Up>();
try {
String sql = "select id,yn,pn from up";
conn= getConn();
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while(rs.next()) {
Up up = new Up(rs.getInt("id"), rs.getString("yn"),rs.getString("pn"));
list.add(up);
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
}
service层
public interface UpService {
public void add(Up up);
public List<Up> list();
}
public class UpServiceImpl implements UpService{
private UpDao upDao = new UpDaoImpl();
@Override
public void add(Up up) {
// TODO Auto-generated method stub
upDao.add(up);
}
@Override
public List<Up> list() {
// TODO Auto-generated method stub
return upDao.list();
}
}
jsp
对于文件上传表单处理其中method必须为post,也要增加类型enctype=“multipart/form-data”。这样就可以把文件中的数据作为流式数据上传。当然无论是什么文件格式,均可以。。。
up.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>上传</title>
</head>
<body>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<input type="text" name="username"/><br>
<input type="file" name="upload"/><br>
<input type="submit" value="上传"/>
</form>
</body>
</html>
load.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
th {
width: 200px;
height: 100px;
background-color: gray;
}
td, th {
border: solid 1px gray;
text-align: center;
}
a {
margin-left: 10px;
}
</style>
</head>
<body>
<%
List<Up> us = (List) session.getAttribute("us");
%>
<table>
<tr>
<th>id</th>
<th>用户头像</th>
<th>文件名称</th>
<th>下载</th>
</tr>
<%
for (int i = 0; i < us.size(); i++) {
Up u = us.get(i);
%>
<tr style="background-color: skyblue">
<td><%=u.getId() %></td>
<td><img src="../<%=u.getYn()%>"></td>
<td><%=u.getPn() %></td>
<td><a href="../<%=u.getYn()%>">下载</a></td>
</tr>
<%
}
%>
</table>
</body>
</html>
upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//设置上传的目录路径
String path = request.getRealPath("/")+"temp\\";
String tpath = path.substring(path.indexOf("demo"))+"\\";//要上传的目录
// 获取当前文件和文件名称
boolean isMultipart = ServletFileUpload.isMultipartContent(request); //验证表单是否包含附件
DiskFileItemFactory disk = new DiskFileItemFactory();
disk.setSizeThreshold(1024*1024*5); //设置上传附件大小限制为5M
ServletFileUpload up = new ServletFileUpload(disk); //创建一个文件上传的处理器
List<FileItem> its = up.parseRequest(request);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
String time = sdf.format(date);
UpService upService = new UpServiceImpl();
Up u = null;
for(FileItem fileitem : its){
if(fileitem.isFormField()){ //文字
if(fileitem.getFieldName().equals("username")){
String username = fileitem.getString();
out.print(username);
}
}else{ //是附件
out.print("false");
tpath +=time+fileitem.getName(); //相对路径
path+= time+fileitem.getName(); //绝对路径
u = new Up(0,tpath,fileitem.getName());
File file = new File(path);
fileitem.write(file);
}
}
upService.add(u);
List<Up> us= upService.list();
session.setAttribute("us", us);
response.sendRedirect("load.jsp");
%>