记得改replace的G的大小写,在retUserAvServlet
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
74
src/com/hellogithub/servlet/retUserAvServlet.java
Normal file
74
src/com/hellogithub/servlet/retUserAvServlet.java
Normal file
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user