@RequestMapping("/imageupload.action") //@ResponseBody public String searchMember(@RequestParam(value = "file")MultipartFile file,NXSY image){ try { InputStream ins = file.getInputStream(); byte[] buffer=new byte[1024]; int len=0; ByteArrayOutputStream bos=new ByteArrayOutputStream(); while((len=ins.read(buffer))!=-1){ bos.write(buffer,0,len); } bos.flush(); byte data[] = bos.toByteArray(); image.setImg(data); image.setDate(image.getDate()); image.setTitle(image.getTitle()); //image.setImage("/testimage"); int result = this.nxsyService.addImg(image); return "admin2"; /* Student s=new Student(); s.setId(sId); s.setPhoto(data); studentService.insertPhoto(s);*/ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "admin2"; } @RequestMapping(value = "/showimage",produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public byte[] toImageShow(NXSY nx){ //这里是从数据库读取图片,你可以根据自己的业务读取你的数据 NXSY image =this.nxsyService.findOne(nx.getId()); return image.getImg(); }
package com.hts.controller; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.List; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.swing.ImageIcon; import javax.swing.JLabel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.hts.po.NXSY; import com.hts.service.NXSYService; import sun.misc.BASE64Decoder; @Controller public class NXSYController { @Autowired private NXSYService nxsyService; // 跳转到首页 @RequestMapping(value = "/index.action") public String toindex(Model model, NXSY n) { List<NXSY> nx = this.nxsyService.findAll(n); NXSY O = this.nxsyService.findFirst(n); model.addAttribute("O", O); model.addAttribute("NX", nx); return "index"; } //搜索 @RequestMapping(value = "/find.action") public String find(Model model, String title,String date) { List<NXSY> nx = this.nxsyService.find(title,date); model.addAttribute("NX", nx); return "index"; } // //搜索日期 // @RequestMapping(value = "/findtime.action") // public String findtime(Model model, String date) { // List<NXSY> nx = this.nxsyService.findtime(date); // // model.addAttribute("NX", nx); // return "index"; // } // 单个显示 @RequestMapping(value = "/findOne.action") public String findOne(Model model, HttpServletResponse response, NXSY nx) throws IOException, SQLException { NXSY one = this.nxsyService.findOne(nx.getId()); List<NXSY> nxx = this.nxsyService.find(nx.getTitle(),nx.getDate()); model.addAttribute("NX", nxx); model.addAttribute("O", one); return "index"; } }
package com.hts.po; import java.sql.Blob; import java.util.Arrays; public class NXSY { private Integer id;//排序 private String title;//标题 private byte[] img;//图片 private String date;//日期 private String image;//图片路径 public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public byte[] getImg() { return img; } public void setImg(byte[] img) { this.img = img; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } @Override public String toString() { return "NXSY [id=" + id + ", title=" + title + ", img=" + Arrays.toString(img) + ", date=" + date + "]"; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hts.dao.NXSYDao"><!--命名空间属性,用于对sql分类化管理 ,来实现不同业务的隔离 --> <!--parameterType接收输入参数类型String --> <!--resultType查询得到的结果的类型(输出参数的类型) --> <select id="findAll" parameterType="String" resultType="NXSY"> select top(10) * FROM nxsy order by id desc </select> <select id="findFirst" parameterType="String" resultType="NXSY"> select top(1) * FROM nxsy order by id desc </select> <select id="find" parameterType="String" resultType="NXSY"> SELECT * FROM nxsy <where> <if test="title != null and title != '' "> <bind name="title" value=" '%'+title+'%' " /> title like #{title} </if> <if test="date != null and date != '' "> and date = #{date} </if> </where> order by id desc </select> <select id="findtime" parameterType="String" resultType="NXSY"> SELECT * FROM nxsy where date =#{date} order by id desc </select> <select id="findOne" parameterType="Integer" resultType="NXSY"> SELECT * FROM nxsy where id =#{id} </select> <insert id="addImg" parameterType="NXSY"> insert into nxsy(title,img,date,image) values (#{title},#{img},#{date},#{image}); </insert> </mapper>
package com.hts.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.hts.po.NXSY; public interface NXSYDao { List<NXSY> findAll(NXSY n); NXSY findFirst(NXSY n); List<NXSY> find(@Param("title") String title ,@Param("date") String date); List<NXSY> findtime(@Param("date") String date); NXSY findOne(@Param("id") Integer id); int addImg(NXSY nx); }
Service层以及ServiceImpl层在此省略
<%@ 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> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" /> <script src="js/bootstrap.min.js"></script> <link rel="stylesheet" type="text/css" href="css/sousuo.css" /> <style type="text/css"> </style> <title></title> </head> <body> <form action="${pageContext.request.contextPath }/find.action" class="form-inline"> <nav class="navbar navbar-light bg-light" style="text-align: center;"> <input type="date" name="date" id="date" style="width: 272px; text-align: center; font-size: 30px; font-family: '楷体'; height: 50px;" /> <input class="form-control mr-sm-2" type="text" name="title" placeholder="搜索内容内容为空时则为搜索全部" aria-label="Search" id="title" style="width: 800px; text-align: center; height: 60px;" autocomplete="on"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit" style="height: 60px; width: 150px; font-size: 25px;">搜索</button> </nav> </form> <!-- <form action="${pageContext.request.contextPath }/findtime.action" class="form-inline"> <div style="margin-left: 20px;margin-top: -30px"> <input type="date" name="date" style="width: 272px; text-align: center; font-size: 20px; font-family: '楷体';" /> <button type="submit" style="height: 36px; width: 70px;">检索日期</button> </div> </form> <br /><br /> --> <div style="margin-left: 20px; width: 350px; font-size: 20px; font-family: '楷体';"> <!-- <button style="width: 450px;height: 100px;font-size: 20px;border: 0.5px solid black;">sss</button> <br /><br /> --> <c:forEach items="${NX}" var="n"> <a class="btn-draw" href="${pageContext.request.contextPath }/findOne.action?id=${n.id}"> <!-- 重定向到该页面 --> <span>${n.title }<br /> <p style="font-size: 15px;">${n.date }</p> </span> </a> <br /> <br /> </c:forEach> </div> <div style="text-align: center;"> <!--<img src="upload/${O.image}" class="img-fluid" alt="Responsive image" style="max-width: 100%; position: fixed; left: 500px; top: 100px;"> background-size:100% 100%;width: 50% --> <img src="${pageContext.request.contextPath }/showimage.action?id=${O.id}"style="max-width: 100%; position: fixed; left: 500px; top: 100px;" alt="失败" /> </div> </body> </html>
网上找的大部分的都不能直接用,缺这缺那的,不过皇天不负有心人,对一个有所启发,灵感源于https://www.cnblogs.com/Anxc/p/12462549.html,要是这人是个女的,我都想抱着亲一口了,解决我大问题了,以上希望能帮助到你。