使用資料庫和javabean實現上傳與下載
首先,我是部分參考網址,其中寫的最好的莫過於https://blog.csdn.net/a15920804969/article/details/78497668
總體構造圖
(1)domain
file.java
package download_upload.domian;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
public class file {
private Long id;
private String uuidname;
private String filename;
private String savepath;
private String uploadtime;
private String description;
private String username;
public file(){}
public file(String uuidname, String filename, String savepath, String uploadtime, String description,
String username) {
super();
this.uuidname = uuidname;
this.filename = filename;
this.savepath = savepath;
this.uploadtime = uploadtime;
this.description = description;
this.username = username;
}
}
前端頁面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/upload.jsp">上傳檔案</a><br/>
<!-- <a href="/file_upload_download/upload.jsp">上傳檔案</a><br/> -->
<a href="${pageContext.request.contextPath }/listfile">查詢檔案</a><br/>
</body>
</html>
upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> 檔案上傳</h1>
<!--
privateString savepath; //記住檔案的位置
privateDate uploadtime; //檔案的上傳時間
privateString description; //檔案的描述
-->
<form action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>上傳檔名</td><td><input type="file"name="filename"></td>
</tr>
<tr>
<td>上傳者</td><td><input type="text"name="username"></td>
</tr>
<tr>
<td>上傳描述</td><td><textarea name="description"rows="5" cols="30"></textarea></td>
</tr>
<tr>
<tr>
<td colspan="2"align="center"><input type="submit"value="提交" /> <input type="reset"value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
message.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message}
</body>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table align="center"border="1">
<tr>
<th>上傳者</th><th>上傳檔名</th><th>上傳時間</th><th>檔案位置</th><th>檔案描述</th><th>下載</th>
</tr>
<c:forEach var="file" items="${list }">
<tr>
<td>${file.username }</td>
<td>${file.filename }</td>
<td>${file.uploadtime }</td>
<td>${file.savepath }</td>
<td>${file.description }</td>
<td><font color="red"><a href="${pageContext.request.contextPath}/download?id=${file.id}">下載</a></font></td>
</tr>
</c:forEach>
</table>
</body>
</html>
type介面的編寫
type.java
package download_upload.javabean;
import java.sql.ResultSet;
public interface type <T>{
T hanlder(ResultSet rs)throws Exception;
}
javabeanlist的編寫
javabeanlist.java
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.omg.CORBA.PRIVATE_MEMBER;
public class javabeanlist<T>implements type<List<T>> {
private Class<T>classtype;
public javabeanlist(Class<T>classtype) {
this.classtype=classtype;
}
@Override
public List<T> hanlder(ResultSet rs) throws Exception {
// TODO Auto-generated method stub
List<T>list=new ArrayList<>();
while(rs.next())
{
T obj=classtype.newInstance();
BeanInfo beanInfo=Introspector.getBeanInfo(classtype,Object.class);
PropertyDescriptor[] pro=beanInfo.getPropertyDescriptors();
for(PropertyDescriptor ps:pro)
{
String name=ps.getName();
Object value=rs.getObject(name);
ps.getWriteMethod().invoke(obj, value);
}
list.add(obj) ;
}
return list;
}
}
Javabean的編寫
javabean.java
package download_upload.javabean;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
public class javabean<T> implements type<T>{
private Class<T>classtype;
public javabean(Class<T>classtype) {
// TODO Auto-generated constructor stub
this.classtype=classtype;
}
@Override
public T hanlder(ResultSet rs) throws Exception {
// TODO Auto-generated method stub
if(rs.next())
{
T obj=classtype.newInstance();
BeanInfo beanInfo=Introspector.getBeanInfo(classtype,Object.class);
PropertyDescriptor[] pro=beanInfo.getPropertyDescriptors();
for(PropertyDescriptor ps:pro)
{
String name=ps.getName();
Object value=rs.getObject(name);
ps.getWriteMethod().invoke(obj, value);
}
return obj;
}
return null;
}
}
模板類的編寫
template.java
package download_upload.template;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import download_upload.javabean.type;
import download_upload.utils.utils;
/*DML操作*/
public class template {
public static int DMLoperator(String sql,Object...param) {
Connection conn=utils.conn();
PreparedStatement ps=null;
try {
ps=conn.prepareStatement(sql);
for(int i=0;i<param.length;i++) {
ps.setObject(i+1,param[i]);
}
return ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
utils.close(conn,ps,null);
}
return 0;
}
/*DQL操作*/
public static <T>T DQLoperator(String sql,type<T> type,Object...para){
Connection conn=utils.conn();
PreparedStatement ps=null;
ResultSet re=null;
try {
ps=conn.prepareStatement(sql);
for(int i=0;i<para.length;i++) {
ps.setObject(i+1, para[i]);
}
re=ps.executeQuery();
return type.hanlder(re);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
utils.close(conn, ps, re);
}
return null;
}
}
utils工具類
utils.java
package download_upload.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class utils {
private static DataSource datasource;
static{
Properties pro=new Properties();
ClassLoader cla=Thread.currentThread().getContextClassLoader();
InputStream in=cla.getResourceAsStream("db.properties");
try {
pro.load(in);
Class.forName(pro.getProperty("driverClassName"));
datasource=DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection conn() {
try {
return datasource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("資料庫連線異常");
}
}
public static void close(Connection conn,Statement st,ResultSet re) {
try {
if(re!=null)
re.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(st!=null)
st.close();
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}finally {
try {
if(conn!=null)
conn.close();
} catch (Exception e4) {
// TODO: handle exception
e4.printStackTrace();
}
}
}
}
}
webutils工具類
webutils.java
package download_upload.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import download_upload.domian.file;
public class webutils {
public static file doFileUpload(HttpServletRequest req) throws FileSizeLimitExceededException {
file fud = new file();// 檔案資訊物件
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 臨時資料夾temp
factory.setRepository(new File(req.getSession().getServletContext().getContextPath()+"/temp"));
// factory.setRepository(new File("/temp"));// 臨時資料夾temp
factory.setSizeThreshold(1024 * 1024);// 臨時緩衝區大小為1M
ServletFileUpload parse = new ServletFileUpload(factory);// 解析器
//上傳檔案大小
parse.setFileSizeMax(1024 * 1024 * 2);// 單個檔案大小限制為2M
parse.setSizeMax(1024 * 1024 * 20);// 總的檔案大小限制為20M
//解決中文檔名的亂碼
parse.setHeaderEncoding("utf-8");
List<FileItem> list = parse.parseRequest(req);
for (FileItem fileItem : list) {
// 普通表單
if (fileItem.isFormField()) {
String fieldName = fileItem.getFieldName();
// String value = fileItem.getString();
String value = fileItem.getString("utf-8");// 解決欄位的中文亂碼問題
System.out.println("fieldName:" + fieldName);
System.out.println("value:" + value);
// 將當前欄位封裝到fud物件中對應的欄位中去
//普通欄位都通過這個儲存到fud中
BeanUtils.setProperty(fud, fieldName, value);
}
// 檔案
else {
String filename = fileItem.getName();// 獲取檔名
//檔名:aa.txt 與c:\a\b\c\aa.txt的處理 統一
int index=filename.lastIndexOf("\\");
if(index!=-1){
filename=filename.substring(index+1);
}
String realPath=req.getSession().getServletContext().getRealPath("/wenjian/upload");
//生成隨機資料夾
String savePath=generateSavePath(realPath,filename);
//生成唯一的檔名
String uuidname=generateUUIDName(filename);
// 上傳檔案
InputStream in = fileItem.getInputStream();// 獲取檔案讀取流
// OutputStream out = new FileOutputStream("d:/" + name);
//儲存資料夾:savePath 唯一檔名:uuidname
OutputStream out = new FileOutputStream(new File(savePath,uuidname));
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
//刪除臨時檔案
fileItem.delete();
fud.setFilename(filename);//檔名
fud.setUuidname(uuidname);//唯一檔名
fud.setSavepath(savePath);//儲存路徑
fud.setUploadtime(new Date().toLocaleString());
}
}
return fud;//返回檔案資訊封裝物件
} catch (FileUploadBase.FileSizeLimitExceededException e) {
// e.printStackTrace();//僅僅只是列印異常錯誤資訊
//使用失敗,因為此處並沒有response
// request.setAttribute("message", "對不起,您上傳的檔案大小超過了大小的限制");
// request.getRequestDispatcher("/message.jsp").forward(request,response);
//怎麼辦?
//丟擲一個異常出去 實際上異常也是一個返回值
//拋異常【編譯時異常 還是 執行時異常】
//編譯時異常
throw e;//記得丟擲異常要在方法中進行宣告
}
catch(Exception e){
throw new RuntimeException(e);//丟擲執行時異常
}
}
//生成唯一的檔名
private static String generateUUIDName(String filename) {
return UUID.randomUUID().toString()+"_"+filename;
}
//生成隨機資料夾
private static String generateSavePath(String realPath, String filename) {
int hashCode=filename.hashCode();
//通過位運算,計算出一級和二級目錄的數字
int first=hashCode & (0xf);//以及目錄
int second=(hashCode>>4)&(0xf);//二級目錄
String savePath=realPath+"/"+first+"/"+second;
File f=new File(savePath);
if(!f.exists()){
f.mkdirs();//建立多級目錄
}
return savePath;//儲存路徑
}
}
DAO設計模式
ifile.java
package download_upload.impl.filedao;
import java.util.List;
import download_upload.domian.file;
public interface Ifile {
public void insert(file fud);//上傳檔案
public List<file> list();//查詢檔案
public file select(Long id);//通過id獲得檔案的全部資訊
}
fileimpl.java
package download_upload.impl;
import java.util.List;
import download_upload.javabean.javabean;
import download_upload.javabean.javabeanlist;
import download_upload.domian.file;
import download_upload.impl.filedao.Ifile;
import download_upload.template.template;
public class fileimpl implements Ifile{
@Override
public void insert(file fud) {
// TODO Auto-generated method stub
String sql="INSERT INTO baocun (uuidname,filename,savepath,uploadtime,description,username)VALUES(?,?,?,?,?,?)";
template.DMLoperator(sql, fud.getUuidname(),fud.getFilename(),fud.getSavepath(),fud.getUploadtime(),fud.getDescription(),fud.getUsername());
}
@Override
public List<file> list() {
// TODO Auto-generated method stub
String sql="SELECT *FROM baocun";
return template.DQLoperator(sql, new javabeanlist<>(file.class));
}
@Override
public file select(Long id) {
// TODO Auto-generated method stub
String sql="SELECT *FROM baocun where id=?";
return template.DQLoperator(sql, new javabean<>(file.class),id);
}
}
db.properties
#key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/upload?rewriteBatchedStatements=true
username=root
password=123456
maxActive=5
servlet的編寫
down.java
package download_upload.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
importjava.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import download_upload.domian.file;
import download_upload.impl.fileimpl;
import download_upload.impl.filedao.Ifile;
@WebServlet("/download")
public class down extends HttpServlet{
/**
*
*/
private Ifile ifile;
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
ifile=new fileimpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String id = req.getParameter("id");
file fud=ifile.select(Long.valueOf(id));//通過id獲取檔案相關資訊
String path=fud.getSavepath();//儲存路徑
String uuidname=fud.getUuidname();//儲存檔名
File f=new File(path,uuidname);//要下載的檔案的存放位置 path/name
//健壯性判斷
if(!f.exists()){
req.setAttribute("message", "對不起,當前檔案已刪除");
req.getRequestDispatcher("/message.jsp").forward(req, resp);
}
//將中文的檔名編碼後再放到http的響應頭中去,編碼之後瀏覽器收到後會自動解碼
String filename=URLEncoder.encode(fud.getFilename(),"utf-8");
//設定引數,使得瀏覽器可以以下載的方式開啟檔案。
resp.setHeader("content-disposition", "attachement;filename="+filename);
//將要下載的檔案當做一個inputStream讀取進來
InputStream in=new FileInputStream(f);
//讀進來後,再寫到response.getOutputStream()去就可以了
//相應的資料
OutputStream out=resp.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
out.write(buf, 0, len);
System.out.println(buf);
System.out.println(len);
}
in.close();
out.close();
System.out.println("下載檔案成功");
}
}
downtown.java
package download_upload.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import download_upload.impl.fileimpl;
import download_upload.impl.filedao.Ifile;
@WebServlet("/listfile")
public class download extends HttpServlet{
private Ifile file;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
file=new fileimpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws