From bfa6d91601607e3c31a8b672767177e190c7c709 Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Wed, 5 Jul 2023 21:34:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/articleDao.java | 24 +++++++ .../hellogithub/service/articleService.java | 5 ++ .../servlet/article/ArticlePublish.java | 71 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/com/hellogithub/servlet/article/ArticlePublish.java diff --git a/src/com/hellogithub/dao/articleDao.java b/src/com/hellogithub/dao/articleDao.java index b2b2c78..93bc3f6 100644 --- a/src/com/hellogithub/dao/articleDao.java +++ b/src/com/hellogithub/dao/articleDao.java @@ -137,4 +137,28 @@ public class articleDao { } return entity; } + wen + public boolean insertArticle(int userid,String articleContent,String articleTitle,String articleIco){ + Connection conn = null; + PreparedStatement preparedStatement = null; + String sql = "INSERT INTO article(articleId,userId,articleContent,publishTime,isValid,readCount,articleTitle,articleIco)\n" + + " VALUES(NULL,?,?,?,1,0,?,?);"; + Date time= new java.sql.Date(new java.util.Date().getTime()); + boolean bool = false; + try{ + conn = JdbcUtils.getConnection(); + preparedStatement = conn.prepareStatement(sql); + preparedStatement.setInt(1,userid); + preparedStatement.setString(2, articleContent); + preparedStatement.setDate(3, time); + preparedStatement.setString(4,articleTitle); + preparedStatement.setString(5, articleIco); + + bool = preparedStatement.execute(); + JdbcUtils.closeConnection(preparedStatement, conn); + }catch (Exception e){ + e.printStackTrace(); + } + return bool; + } } \ No newline at end of file diff --git a/src/com/hellogithub/service/articleService.java b/src/com/hellogithub/service/articleService.java index ab773f5..fc8917c 100644 --- a/src/com/hellogithub/service/articleService.java +++ b/src/com/hellogithub/service/articleService.java @@ -81,4 +81,9 @@ public class articleService { return JSONObject.toJSONString(dataMap); } + public boolean insertArticle(int userid,String articleContent,String articleTitle,String articleIco){ + boolean bool = articleDao.insertArticle(userid, articleContent, articleTitle, articleIco); + return bool; + } + } \ No newline at end of file diff --git a/src/com/hellogithub/servlet/article/ArticlePublish.java b/src/com/hellogithub/servlet/article/ArticlePublish.java new file mode 100644 index 0000000..acd3b0a --- /dev/null +++ b/src/com/hellogithub/servlet/article/ArticlePublish.java @@ -0,0 +1,71 @@ +package com.hellogithub.servlet.article; + +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.service.UserService; +import com.hellogithub.service.articleService; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; + +@WebServlet("/articlepublish") +public class ArticlePublish extends HttpServlet { + protected articleService articleservice = new articleService(); + private UserService userservice = new UserService(); + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doPost(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setCharacterEncoding("UTF-8"); + // 设置响应头允许ajax跨域访问 + String curOrigin = req.getHeader("Origin"); + resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin); + resp.setHeader("Access-Control-Allow-Credentials", "true"); + resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); + resp.setHeader("Access-Control-Max-Age", "3600"); + resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); + resp.setContentType("application/json;charset=UTF-8"); + + PrintWriter writer = resp.getWriter(); + HttpSession session = req.getSession(); + if(session == null){ + setResultError("用户未登录", writer); + writer.close(); + }else{ + String username = req.getParameter("username"); + String articlecontent = req.getParameter("articleContent"); + String articletitle = req.getParameter("articleTitle"); + String articleico = req.getParameter("articleIco"); + int userid = userservice.selectIdByName(username); + Boolean bool = articleservice.insertArticle( + userid,articlecontent,articletitle,articleico + ); + if(bool == true){ + setResultOk("发布成功",writer); + writer.close(); + } + } + } + public void setResult(Integer code, String msg, PrintWriter writer) { + HashMap result = new HashMap<>(); + result.put("code", code); + result.put("msg", msg); + String jsonString = JSONObject.toJSONString(result); + writer.println(jsonString); + writer.close(); + } + public void setResultError(String msg, PrintWriter writer) { + setResult(500, msg, writer); + } + public void setResultOk(String msg, PrintWriter writer) {setResult(200, msg, writer);} + +}