javaweb文件上传,图片地址上传到数据库,其他jsp页面通过查询数据库直接显示图片

先看效果
在这里插入图片描述
这里的头像就是通过查询数据库里存的图片的地址显示的
在这里插入图片描述
在这里插入图片描述

数据库中就会多出地址
在这里插入图片描述

提交之后,在管理员页面或者查看个人信息页面就能看到在这里插入图片描述
原理:通过文件上传的方式将图片的地址上传到数据库,其他jsp页面查询数据库时通过地址直接显示图片
所需的jar包:
在这里插入图片描述
jsp页面(部分):

                <form action="${pageContext.request.contextPath}/addInfoma" method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <ul>
                            <li style="display: none">
                                <label><span>报修人id:</span></label>
                                <input type="text" name="r_id" value="${r_id}">
                            </li>
                            <li>
                                <label><span>&nbsp;&nbsp;&nbsp;: </span></label>
                                <input style="display: none" type="text" name="r_name" value="${r_name}" >
                                <input disabled="disabled" type="text" value="${r_name}">
                            
  • 19
    点赞
  • 144
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
JavaWeb中上传图片数据库需要进行以下几个步骤: 1.在数据库中创建表,其中包含一个用于存储图片的BLOB类型的字段。 2.在JSP页面中添加一个表单,用于上传图片。 3.在Servlet中获取表单数据,包括上传的图片。 4.将图片转换为字节数组,并将其存储到数据库中。 5.在JSP页面显示数据库中检索到的图片。 以下是一个简单的示例代码: 1.创建数据库表 ```sql CREATE TABLE `image` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `data` longblob, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 2.添加上传表单 ```html <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form> ``` 3.处理上传请求 ```java @WebServlet("/upload") @MultipartConfig public class UploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream inputStream = null; OutputStream outputStream = null; Connection connection = null; PreparedStatement statement = null; try { Part filePart = request.getPart("file"); if (filePart != null) { inputStream = filePart.getInputStream(); Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); statement = connection.prepareStatement("INSERT INTO image (name, data) values (?, ?)"); statement.setString(1, filePart.getName()); statement.setBlob(2, inputStream); statement.executeUpdate(); } response.sendRedirect("index.jsp"); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } } ``` 4.从数据库中检索图片显示 ```java @WebServlet("/image") public class ImageServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; OutputStream outputStream = null; try { int id = Integer.parseInt(request.getParameter("id")); Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); statement = connection.prepareStatement("SELECT * FROM image WHERE id = ?"); statement.setInt(1, id); resultSet = statement.executeQuery(); if (resultSet.next()) { Blob blob = resultSet.getBlob("data"); byte[] bytes = blob.getBytes(1, (int) blob.length()); response.setContentType("image/jpeg"); outputStream = response.getOutputStream(); outputStream.write(bytes); } } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { outputStream.close(); } if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值