From 85bc4f6984c4e9496c45066b99b82aa28e02f84b Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Fri, 7 Jul 2023 13:08:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BE=97=E6=94=B9replace=E7=9A=84G?= =?UTF-8?q?=E7=9A=84=E5=A4=A7=E5=B0=8F=E5=86=99=EF=BC=8C=E5=9C=A8retUserAv?= =?UTF-8?q?Servlet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 18 +++++ src/com/hellogithub/service/UserService.java | 4 + .../servlet/UploadHandleServlet.java | 18 +++-- .../hellogithub/servlet/retUserAvServlet.java | 74 +++++++++++++++++++ 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 src/com/hellogithub/servlet/retUserAvServlet.java diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 179ba54..55651f8 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -327,4 +327,22 @@ public class UserDao { e.printStackTrace(); } } + + + + + public int updateUserAv(String name,String uri){ + String label=""; + int num; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql="UPDATE user SET userAvatar = ? WHERE userName= ? and isValid = 1"; + num = runner.update(conn,sql,uri,name); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return num; + } } diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 268dd34..cd595bf 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -81,4 +81,8 @@ public class UserService { * userAvatar */ public void insertUserAvatar(String username,String url){userDao.insertUserAvatar(username,url);} + + public int updateUserAv(String name,String uri){ + return userDao.updateUserAv(name,uri); + } } diff --git a/src/com/hellogithub/servlet/UploadHandleServlet.java b/src/com/hellogithub/servlet/UploadHandleServlet.java index c787777..89b083b 100644 --- a/src/com/hellogithub/servlet/UploadHandleServlet.java +++ b/src/com/hellogithub/servlet/UploadHandleServlet.java @@ -4,6 +4,8 @@ package com.hellogithub.servlet; import javax.servlet.annotation.WebServlet; import com.alibaba.fastjson.JSONObject; +import com.hellogithub.entity.userEntity; +import com.hellogithub.service.UserService; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.ProgressListener; @@ -14,6 +16,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.*; import java.util.HashMap; import java.util.List; @@ -21,6 +24,7 @@ import java.util.UUID; @WebServlet("/UploadHandle") public class UploadHandleServlet extends HttpServlet { + private UserService userService = new UserService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); @@ -34,15 +38,14 @@ public class UploadHandleServlet extends HttpServlet { response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); response.setContentType("application/json;charset=UTF-8"); PrintWriter writer = response.getWriter(); - - + HttpSession session = request.getSession(); + userEntity userEntity =null; + userEntity = (userEntity)session.getAttribute("user"); //得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全 String savePath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload"); - savePath= this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\helloGithub_war_exploded\\WEB-INF\\upload","\\web\\uploadFile"); + savePath= this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"upload").replace("\\out\\artifacts\\hellogithub_war_exploded\\WEB-INF\\upload","\\web\\uploadFile"); System.out.println(savePath); -// savePath="D:\\Java\\helloG\\hellogithub\\web\\uploadFile"; -// String savePath="E:\\Desktop\\out"; //上传时生成的临时文件保存目录 String tempPath = this.getServletContext().getRealPath(File.separator+"WEB-INF"+File.separator+"tmp"); File tmpFile = new File(tempPath); @@ -160,6 +163,11 @@ public class UploadHandleServlet extends HttpServlet { //删除处理文件上传时生成的临时文件 item.delete(); message = "文件上传成功!"; + if(userEntity != null) + { + userService.updateUserAv(userEntity.getUserName(),realSavePath+"\\"+saveFilename); + } + } } }catch (FileUploadBase.FileSizeLimitExceededException e) { diff --git a/src/com/hellogithub/servlet/retUserAvServlet.java b/src/com/hellogithub/servlet/retUserAvServlet.java new file mode 100644 index 0000000..692340e --- /dev/null +++ b/src/com/hellogithub/servlet/retUserAvServlet.java @@ -0,0 +1,74 @@ +package com.hellogithub.servlet; + +import com.hellogithub.entity.userEntity; +import com.hellogithub.service.UserService; + +import javax.servlet.ServletConfig; +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 javax.servlet.http.HttpSession; +import java.io.*; +import java.nio.channels.Channels; + +@WebServlet("/retUserAv") +public class retUserAvServlet extends HttpServlet { + private UserService userService = new UserService(); + @Override + public void init(ServletConfig config) throws ServletException { + + } + + @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 { + + + HttpSession session = req.getSession(); + userEntity userEntity= null; + userEntity=(userEntity)session.getAttribute("user"); + if(userEntity != null) + { + userEntity = userService.selectUserByName(userEntity.getUserName()); + FileInputStream fis = new FileInputStream (userEntity.getUserAvatar()); + + int size = fis.available(); + if(size != 0) + { + byte data[] = new byte[size] ; + fis.read(data) ; + fis.close(); + resp.reset(); + resp.setHeader("Content-Type", "image/jpg"); +// resp.setContentType("image/jpg"); + OutputStream os = resp.getOutputStream() ; + os.write(data); + os.flush(); + os.close(); + } + + + + +// resp.setContentType("image/jpeg"); +// 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"); + } + + + + } +}