先看下运行结果:
点击选取文件,上传图片,点击提交,出现上传成功恭喜的界面:
数据库中的结果:
,这里我要说明一点,jsp中设置的上传到指定文件upload,也就意味着你要在web项目里建一个upload文档,用来当作读取的时候的路径条件(可以这么理解)同时你要确保输出文件out中有upload,因为读出照片的时候需要从数据库导出到该文档:
这里我使用的平台为Mac + IDEA2020
首先创建数据库,test2,再创建一个存储图片的表filedata,包含两个属性path存储照片绝对路径 filename存储照片名称
创建一个test.html作为主界面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
选择一个文件:<input type="file" name="myfile">
<input type="submit" value="提交">
</form>
</body>
</html>
下面是upload.jsp:
<%--
Created by IntelliJ IDEA.
User: wangzeyi
Date: 2020/12/18
Time: 下午2:18
To change this template use File | Settings | File Templates.
--%>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
<%
request.setCharacterEncoding("utf-8");
//判断是普通表单还是文件上传
boolean flag = ServletFileUpload.isMultipartContent(request);
if(flag){
//定义一个解析器,来分析请求中各个项目
DiskFileItemFactory factory = new DiskFileItemFactory();
//解析upload创建成功
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem>fileList = upload.parseRequest(request);//在未加入commons-io-2.8.0.jar之前会出现500错误
//java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
//原因可能是:使用commons-fileupload的版本大于1.0
//每一个fileItem都代表一个表单元素
Iterator<FileItem>myitor = fileList.iterator();
while (myitor.hasNext()){
FileItem item = myitor.next();
if(item!=null){
String filename = item.getName();
//服务器目录下相对目录 相对路径
String path = "upload";
String absolutepath = this.getServletConfig().getServletContext().getRealPath(path);//这个地方直接用this.getServletContext().getRealPath(path)我是显示没有getServletContext()这个函数的,不知道是不是配置有问题
File file = new File(filename);
File uploadFile = new File(absolutepath,file.getName());
try { //这里必须加一个异常处理不然会报错
item.write(uploadFile);
} catch (Exception e) {
e.printStackTrace();
}
out.println("上传成功");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//加载JDBC驱动程序
String url = "jdbc:mysql://localhost:3306/test2";
//file_db为数据库的名称
String user = "root";
//连接Mysql数据库的密码
String password = "Wzy768291";
Connection conn = DriverManager.getConnection(url,user,password);
//创建连接
try{
String sql="insert into filedata(filename,path) values('"+file.getName()+"',?)";
//插入记录的sql语句
PreparedStatement pstmt = conn.prepareStatement(sql);//创建PreparedStatement对象
pstmt.setString(1,path);//将字符path存储到pstmt对象中
pstmt.execute();//patmt将记录插入到数据库中
out.println("恭喜");
}
catch (SQLException e){
out.println("出现SQLException异常");//如果重复上传同一张,即数据库中保存的有的话,会抛出异常,并不再添加
}
}
}
}
%>
要用到的包比较多这里不再一一举例,tomcat应该含的有,我的可能是tomcat没配置完整,所以找了好多包,这里展示一下我的jar包配置: