采用jsp+servlet+Javabean实现图片上传和展示

开发工具:IDEA
开发环境:jdk1.8+Tomcat8.5

一,先采用IDEA新建一个web项目
在这里插入图片描述
二,创建数据库和表
在这里插入图片描述
三,导入项目开发中需要的jar包
在这里插入图片描述在这里插入图片描述
四,构建项目目录结构
在这里插入图片描述
五,创建Image实体类

public class Image {

    private int id;
    private String title;//图片名称


    public Image() {
    }

    public Image(int id, String title) {
        this.id = id;
        this.title = title;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


}

六,编写jdbc链接数据库

public class JDBC {

    private static  final String DRIVER = "com.mysql.jdbc.Driver";
    private static  final String URL = "jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8";
    private static  final String USERNAME = "root";
    private static  final String PWD = "12345678";

    //加载驱动
    static {
        try {
            Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //链接数据库
    public static Connection getCon(){
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL,USERNAME,PWD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    //关闭链接
    public static void closeCon(Connection con, PreparedStatement pre, ResultSet rst){
        try {
            if (con!=null)
                con.close();
            if (pre!=null)
                pre.close();
            if (rst!=null)
                rst.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

七,编写service业务类

/**
 * 编写service
 */
public class ImageService {

    Connection con = null;
    PreparedStatement pre = null;
    ResultSet rst = null;

    //添加
    public void addImage(Image image){
        try {
            con = JDBC.getCon();
            String sql = "insert into images(title)values(?)";
            pre = con.prepareStatement(sql);
            pre.setString(1,image.getTitle());

            int i = pre.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.closeCon(con,pre,rst);
        }
    }
    //查询全部
    public List<Image> selectAll(){
        List<Image> list = new ArrayList<>();
        try {
            con = JDBC.getCon();
            String sql = "select * from images";
            pre = con.prepareStatement(sql);
            rst = pre.executeQuery();
            while (rst.next()){
                int id = rst.getInt(1);
                String title = rst.getString(2);

                Image image = new Image(id, title);
                list.add(image);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.closeCon(con,pre,rst);
        }
        return list;
    }
}

七,编写index.jsp页面作为图片上传页面
在这里插入图片描述
八,编写上传图片的servlet类

/**
 * 图片上传
 */
@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {

    protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        DiskFileItemFactory factory = new DiskFileItemFactory();
        //创建解析类的实例
        ServletFileUpload sfu = new ServletFileUpload(factory);
        //开始解析
        sfu.setFileSizeMax(1024*400);
        //每个表单域中数据会封装到一个对应的FileItem对象上
        try {
            List<FileItem> items = sfu.parseRequest(request);
            //区分表单域
            for (int i = 0; i < items.size(); i++) {
                FileItem item = items.get(i);
                //isFormField为true,表示这不是文件上传表单域
                if(!item.isFormField()){
                    ServletContext sctx = getServletContext();
                //获得存放文件的物理路径
                    // upload下的某个文件夹 得到当前在线的用户 找到对应的文件夹

                    String path = "d:\\images";
                    System.out.println(path);
                    //获得文件名
                    String fileName =UUID.randomUUID()+item.getName();

                    System.out.println(fileName);
                        //该方法在某些平台(操作系统),会返回路径+文件名
                    fileName = fileName.substring(fileName.lastIndexOf("/")+1);
                    File file = new File(path+"\\"+fileName);
                    if(!file.exists()){
                        item.write(file);
                    //将上传图片的名字记录到数据库中
                        Image image = new Image();
                        image.setTitle(fileName);
                        new ImageService().addImage(image);
                        response.sendRedirect("ALL");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

九,编写展示图片的servlet类

/**
 * 图片展示
 */

@WebServlet("/ALL")
public class ALLServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Image> images = new ImageService().selectAll();
        request.setAttribute("images",images);
        request.getRequestDispatcher("List.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

十,编写list.jsp页面用来展示图片
在这里插入图片描述
十一,配置图片存放的文件夹
1)首先在D盘创建一个文件夹命名为images
2,在tomcat配置中选择此文件夹
在这里插入图片描述
在这里插入图片描述

十二,启动服务器访问操作就OK了
在这里插入图片描述
点击“”“保存”按钮
在这里插入图片描述

  • 15
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

java庞

你的鼓励就是我们最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值