From 5a78a5f7ead6f13578eec91f83a1c89260c0c793 Mon Sep 17 00:00:00 2001 From: Qing Date: Tue, 11 Jul 2023 19:42:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/AdminDao.java | 2 +- src/com/hellogithub/dao/UserDao.java | 4 +- src/com/hellogithub/dao/categoryDao.java | 2 +- src/com/hellogithub/dao/fileDao.java | 8 +- .../entity/projectCategoryEntity.java | 6 +- src/com/hellogithub/service/UserService.java | 5 +- .../hellogithub/service/oneFileService.java | 3 +- .../hellogithub/servlet/DownLoadServlet.java | 121 ++++++++++++++++++ .../servlet/insertFileByAdminServlet.java | 4 +- .../systemAdmin/AdminLoginServlet.java | 1 + .../systemAdmin/retProjectByTimeServlet.java | 7 + .../retProjectNumByCategoryServlet.java | 7 + .../systemAdmin/updateFileByAdminServlet.java | 4 +- 13 files changed, 157 insertions(+), 17 deletions(-) create mode 100644 src/com/hellogithub/servlet/DownLoadServlet.java diff --git a/src/com/hellogithub/dao/AdminDao.java b/src/com/hellogithub/dao/AdminDao.java index 54ac9e4..012a902 100644 --- a/src/com/hellogithub/dao/AdminDao.java +++ b/src/com/hellogithub/dao/AdminDao.java @@ -308,7 +308,7 @@ public class AdminDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="UPDATE article SET isValid = ? WHERE id = ?"; + String sql="UPDATE onefile SET isValid = ? WHERE id = ?"; num = runner.update(conn, sql, isvalid,id); conn.close(); } catch (SQLException e) { diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 5858803..ea76218 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -200,7 +200,7 @@ public class UserDao { * * 返回用户总数 * */ - public long queryUserCount(){ + public userEntity queryUserCount(){ userEntity userEntity; long num; try { @@ -214,7 +214,7 @@ public class UserDao { throw new RuntimeException(e); } - return num; + return userEntity; } diff --git a/src/com/hellogithub/dao/categoryDao.java b/src/com/hellogithub/dao/categoryDao.java index c55fcbb..02d2bb2 100644 --- a/src/com/hellogithub/dao/categoryDao.java +++ b/src/com/hellogithub/dao/categoryDao.java @@ -86,7 +86,7 @@ public class categoryDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="delete from category where articleId = ?"; + String sql="delete from category where categoryId = ?"; num = runner.update(conn,sql,id); conn.close(); } catch (SQLException e) { diff --git a/src/com/hellogithub/dao/fileDao.java b/src/com/hellogithub/dao/fileDao.java index 2cae2a1..ac2fb5e 100644 --- a/src/com/hellogithub/dao/fileDao.java +++ b/src/com/hellogithub/dao/fileDao.java @@ -21,7 +21,7 @@ public class fileDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="select * from onefile "; + String sql="select * from onefile where isValid = 1"; fileEntityList = runner.query(conn,sql,new BeanListHandler<>(fileEntity.class)); conn.close(); } catch (SQLException e) { @@ -80,8 +80,8 @@ public class fileDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="update onefile set oneFileName = ? , language = ? ,description =?, userId=?, url=?, lookNum=?,Content=?,isValid=? where id =? "; - num = runner.update(conn,sql, oneFileName ,language ,description,userId, url, lookNum ,contextFile ,isValid,fileId); + String sql="update onefile set oneFileName = ? , language = ? ,description =?, userId=?, url=?, lookNum=?,isValid=? where id =? "; + num = runner.update(conn,sql, oneFileName ,language ,description,userId, url, lookNum ,isValid,fileId); conn.close(); } catch (SQLException e) { throw new RuntimeException(e); @@ -110,7 +110,7 @@ public class fileDao { try{ Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "INSERT INTO onefile(oneFileName,language,description,userId,url,lookNum,Content,isValid) VALUES(?,?,?,?,?,0,?,1);"; + String sql = "INSERT INTO onefile(oneFileName,language,description,userId,url,lookNum,Content,isValid) VALUES(?,?,?,?,?,0,?,0);"; int bool = 0; num = runner.update(conn, sql,oneFileName,language,description,userId,url,contextFile); conn.close(); diff --git a/src/com/hellogithub/entity/projectCategoryEntity.java b/src/com/hellogithub/entity/projectCategoryEntity.java index b3cd76f..765c59b 100644 --- a/src/com/hellogithub/entity/projectCategoryEntity.java +++ b/src/com/hellogithub/entity/projectCategoryEntity.java @@ -29,13 +29,13 @@ public class projectCategoryEntity { this.projectId = projectId; } - int num ; + long num ; - public int getNum() { + public long getNum() { return num; } - public void setNum(int num) { + public void setNum(long num) { this.num = num; } String categoryName; diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 0e3b966..60e3bcf 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -62,7 +62,10 @@ public class UserService { * @param name * */ public int dedleteAllLabel(String name){return userDao.dedleteAllLabel(name);} - public long queryUserCount(){return userDao.queryUserCount();} + public long queryUserCount(){ + userEntity userEntity = userDao.queryUserCount(); + return userEntity.getNum(); + } /** * 根据用户名返回贡献值 diff --git a/src/com/hellogithub/service/oneFileService.java b/src/com/hellogithub/service/oneFileService.java index d4aaf00..b0bae34 100644 --- a/src/com/hellogithub/service/oneFileService.java +++ b/src/com/hellogithub/service/oneFileService.java @@ -52,8 +52,9 @@ public class oneFileService { return fileDao.selectAllFile(); } - public int insertOneFile(int userId ,String oneFileName, String language,String description , + public int insertOneFile(String username ,String oneFileName, String language,String description , String url,String contextFile){ + int userId = userDao.selectUserByName(username).getUserId(); return fileDao.insertOneFile(userId,oneFileName,language,description,url,contextFile); } } diff --git a/src/com/hellogithub/servlet/DownLoadServlet.java b/src/com/hellogithub/servlet/DownLoadServlet.java new file mode 100644 index 0000000..58238f1 --- /dev/null +++ b/src/com/hellogithub/servlet/DownLoadServlet.java @@ -0,0 +1,121 @@ +package com.hellogithub.servlet; + +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.service.projectService; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.UUID; + +@WebServlet("/DownLoad") +public class DownLoadServlet extends HttpServlet { + private projectService projectService = new projectService(); + + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + response.reset(); + response.setContentType("application/x-msdownload;charset=utf-8"); + //得到要下载的文件名 + String projectId = request.getParameter("id"); + String address = projectService.selectProById(projectId).getFileAddress(); + String[] liss = address.split("\\\\"); + String fileName = liss[liss.length - 1]; + System.out.println(fileName); + //本地需要进行转码,linux服务器使用就会出现下载乱码 + //fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8"); +// System.out.print("转码后"+fileName); + //上传的文件都是保存在/WEB-INF/upload目录下的子目录当中 + String fileSaveRootPath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\helloGithub_war_exploded\\WEB-INF\\upload","\\web\\projectFile");; + //通过文件名找出文件的所在目录 + //String path = findFileSavePathByFileName(fileName,fileSaveRootPath); +// String path = "/WEB-INF/upload"; + //得到要下载的文件 + File file = new File(fileSaveRootPath + File.separator + fileName); + //File file = new File(fileName); + System.out.println(file); + System.out.print("\n"); + // 浏览器请求响应转码防止乱码 + response.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding("UTF-8"); + //如果文件不存在 + if (!file.exists()) { + request.setAttribute("message", "wrong"); + request.getRequestDispatcher("/message.jsp").forward(request, response); + return; + } + //处理文件名 + String realname = fileName.substring(fileName.indexOf("_") + 1); +// System.out.println(realname); + //设置响应头,控制浏览器下载该文件 + response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); + //读取要下载的文件,保存到文件输入流 + FileInputStream in = new FileInputStream(fileSaveRootPath + File.separator + fileName); + + //创建输出流 + OutputStream out = response.getOutputStream(); + + //创建缓冲区 + byte buffer[] = new byte[1024]; + int len = 100; + //循环将输入流中的内容读取到缓冲区当中 + while ((len = in.read(buffer)) > 0) { + //输出缓冲区的内容到浏览器,实现文件下载 + out.write(buffer, 0, len); + } + //关闭文件输入流 + in.close(); + //关闭输出流 + out.close(); +// setResultOK("下载成功",writer); + } + + /** + * @param filename 要下载的文件名 + * @param saveRootPath 上传文件保存的根目录,也就是/WEB-INF/upload目录 + * @return 要下载的文件的存储目录 + * @Method: findFileSavePathByFileName + * @Description: 通过文件名和存储上传文件根目录找出要下载的文件的所在路径 + * @Anthor: + */ + public String findFileSavePathByFileName(String filename, String saveRootPath) { +// int hashcode = filename.hashCode(); +// int dir1 = hashcode&0xf; //0--15 +// int dir2 = (hashcode&0xf0)>>4; //0-15 + //String dir = saveRootPath + "\\" + dir1 + "\\" + dir2; //upload\2\3 upload\3\5 + String dir = saveRootPath; + File file = new File(dir); + if (!file.exists()) { + //创建目录 + file.mkdirs(); + } + return dir; + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } + + 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); + } +} \ No newline at end of file diff --git a/src/com/hellogithub/servlet/insertFileByAdminServlet.java b/src/com/hellogithub/servlet/insertFileByAdminServlet.java index 1d45307..494cbda 100644 --- a/src/com/hellogithub/servlet/insertFileByAdminServlet.java +++ b/src/com/hellogithub/servlet/insertFileByAdminServlet.java @@ -38,11 +38,11 @@ public class insertFileByAdminServlet extends HttpServlet { 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"); + //插入文件 String oneFileName=req.getParameter("oneFileName"); String language=req.getParameter("language"); String description=req.getParameter("description"); - int userId= Integer.parseInt(req.getParameter("userId")); String Content=req.getParameter("Content"); String url=req.getParameter("url"); @@ -53,7 +53,7 @@ public class insertFileByAdminServlet extends HttpServlet { setResultError("用户未登录",writer); }else{ String username=userEntity.getUserName(); - int num = oneFileService.insertOneFile(userId,oneFileName,language,description,url,Content); + int num = oneFileService.insertOneFile(username,oneFileName,language,description,url,Content); if(num == 0){ setResultError("添加失败",writer); }else{ diff --git a/src/com/hellogithub/servlet/systemAdmin/AdminLoginServlet.java b/src/com/hellogithub/servlet/systemAdmin/AdminLoginServlet.java index 2923096..edd9799 100644 --- a/src/com/hellogithub/servlet/systemAdmin/AdminLoginServlet.java +++ b/src/com/hellogithub/servlet/systemAdmin/AdminLoginServlet.java @@ -94,6 +94,7 @@ public class AdminLoginServlet extends HttpServlet { public void setResult(Integer code, String msg, PrintWriter writer) { HashMap result = new HashMap<>(); result.put("msg", msg); + result.put("code", code); String jsonString = JSONObject.toJSONString(result); writer.println(jsonString); writer.close(); diff --git a/src/com/hellogithub/servlet/systemAdmin/retProjectByTimeServlet.java b/src/com/hellogithub/servlet/systemAdmin/retProjectByTimeServlet.java index e044e7a..022df47 100644 --- a/src/com/hellogithub/servlet/systemAdmin/retProjectByTimeServlet.java +++ b/src/com/hellogithub/servlet/systemAdmin/retProjectByTimeServlet.java @@ -1,5 +1,6 @@ package com.hellogithub.servlet.systemAdmin; +import com.hellogithub.entity.adminEntity; import com.hellogithub.service.projectService; import javax.servlet.ServletException; @@ -7,6 +8,7 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/retProjectByTime") @@ -30,6 +32,11 @@ public class retProjectByTimeServlet extends HttpServlet { 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"); + HttpSession session = req.getSession(); + adminEntity user = (adminEntity)session.getAttribute("admin"); + if(user == null) { + return; + } String str = projectService.retProjectByTime(); PrintWriter writer = resp.getWriter(); writer.println(str); diff --git a/src/com/hellogithub/servlet/systemAdmin/retProjectNumByCategoryServlet.java b/src/com/hellogithub/servlet/systemAdmin/retProjectNumByCategoryServlet.java index f6b96f2..63efd65 100644 --- a/src/com/hellogithub/servlet/systemAdmin/retProjectNumByCategoryServlet.java +++ b/src/com/hellogithub/servlet/systemAdmin/retProjectNumByCategoryServlet.java @@ -1,5 +1,6 @@ package com.hellogithub.servlet.systemAdmin; +import com.hellogithub.entity.adminEntity; import com.hellogithub.service.categoryService; import javax.servlet.ServletException; @@ -7,6 +8,7 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/retProjectNumByCategory") @@ -30,6 +32,11 @@ public class retProjectNumByCategoryServlet extends HttpServlet { 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"); + HttpSession session = req.getSession(); + adminEntity user = (adminEntity)session.getAttribute("admin"); + if(user == null) { + return; + } String str = categoryService.retProjectNumByCategory(); PrintWriter writer = resp.getWriter(); writer.println(str); diff --git a/src/com/hellogithub/servlet/systemAdmin/updateFileByAdminServlet.java b/src/com/hellogithub/servlet/systemAdmin/updateFileByAdminServlet.java index 145d1f5..0f566a3 100644 --- a/src/com/hellogithub/servlet/systemAdmin/updateFileByAdminServlet.java +++ b/src/com/hellogithub/servlet/systemAdmin/updateFileByAdminServlet.java @@ -46,9 +46,9 @@ public class updateFileByAdminServlet extends HttpServlet { String oneFileName=req.getParameter("oneFileName"); String language=req.getParameter("language"); String description =req.getParameter("description"); - int isValid = Integer.parseInt(req.getParameter("isValid ")); + int isValid = Integer.parseInt(req.getParameter("isValid")); String url=req.getParameter("url"); - String Content=req.getParameter("Content"); + String Content=""; int bool = oneFileService.updateFileDetail(fileId,userId,lookNum,oneFileName,language,description,isValid,url,Content); if(bool == 1){ setResultOk("更新成功",writer);