JavaWeb利用jsp 选取图片并将相对路径修改为绝对路径upload上传到Mysql数据库中(环境:IDEA+Tomcat9.0.4+mysql)

5 篇文章 0 订阅
3 篇文章 0 订阅

先看下运行结果:
在这里插入图片描述
点击选取文件,上传图片,点击提交,出现上传成功恭喜的界面:
在这里插入图片描述
数据库中的结果:
在这里插入图片描述
,这里我要说明一点,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包配置:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好的!文西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值