一,封装数据库操作:将所有数据库的操作存放在dao包下。
(1)运用单列模式设计一个数据库连接的工具类,提供一个Connection对象。
package dao;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @ClassName DBUtil
* @Description 获取数据库连接
* @Auther danni
* @Date 2020/2/15 20:49]
* @Version 1.0
**/
public class DBUtil {
private static String URL="jdbc:mysql://127.0.0.1:3306/java_image_server?characterEncoding=utf8&useSSL=true";
private static String USER="root";
private static String PASSWORD="";
private volatile static DataSource dataSource=null;
private static DataSource getDataSource(){
if(dataSource==null){
synchronized (DBUtil.class){
if(dataSource==null){
dataSource=new MysqlDataSource();
((MysqlDataSource)dataSource).setURL(URL);
((MysqlDataSource)dataSource).setUser(USER);
((MysqlDataSource)dataSource).setPassword(PASSWORD);
}
}
}
return dataSource;
}
public static Connection getConnection() {
try {
return getDataSource().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
(2)为图片和用户分别创建一个类mage,user.对应数据库中的两张表,类中属性和表中属性一一对应。
user类
package dao;
/**
* @ClassName User
* @Description TODO
* @Auther danni
* @Date 2020/2/19 11:04]
* @Version 1.0
**/
public class User {
private int userId;
private String name;
private String password;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Image类
package dao;
/**
* @ClassName Image
* @Description 表结构
* @Auther danni
* @Date 2020/2/16 9:21]
* @Version 1.0
**/
public class Image {
private int imageId;
private String imageName;
private int size;
private String uploadTime;
private String contentType;
private String path;
private String md5;
private int userId;
public Image() {
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getUploadTime() {
return uploadTime;
}
public void setUploadTime(String uploadTime) {
this.uploadTime = uploadTime;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
@Override
public String toString() {
return "Image{" +
"imageId=" + imageId +
", imageName='" + imageName + '\'' +
", size=" + size +
", uploadTime='" + uploadTime + '\'' +
", contentType='" + contentType + '\'' +
", path='" + path + '\'' +
", md5='" + md5 + '\'' +
'}';
}
}
(3)为所有有关数据库操作的增删查改构建一个操作类imageDao。
package dao;
import com.ImageServerException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName ImageDao
* @Description TODO
* @Auther danni
* @Date 2020/2/16 9:26]
* @Version 1.0
**/
public class ImageDao {
/* *
* @Author danni
* @Description 注册用户
* @Date 11:08 2020/2/19
* @Param [image]
* @return void
**/
public boolean register(User user){
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
//构建SQL语句
String sql="insert into user values(?,?,null)";
PreparedStatement statement=null;
try {
statement=connection.prepareStatement(sql);
//执行SQL查询
statement.setString(1,user.getName());
statement.setString(2,user.getPassword());
int res=statement.executeUpdate();
if(res!=1){
throw new ImageServerException("用户注册失败,该用户已存在!");
}
return true;
} catch (SQLException e) {
e.printStackTrace();
} catch (ImageServerException e) {
e.printStackTrace();
} finally {
//关闭连接
DBUtil.close(connection,statement,null);
}
return false;
}
/* *
* @Author danni
* @Description 登录用户
* @Date 11:13 2020/2/19
* @Param [image]
* @return void
**/
public boolean login(User user){
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
//构建SQL语句
String sql="select * from user where name =?and password=?";
PreparedStatement statement=null;
ResultSet set=null;
try {
statement=connection.prepareStatement(sql);
//执行SQL查询
statement.setString(1,user.getName());
statement.setString(2,user.getPassword());
set=statement.executeQuery();
if(!set.next()) {
throw new ImageServerException("登录失败,该用户不存在!");
}
return true;
} catch (SQLException e) {
e.printStackTrace();
} catch (ImageServerException e) {
e.printStackTrace();
} finally {
//关闭连接
DBUtil.close(connection,statement,null);
}
return false;
}
/* *
* @Author danni
* @Description 根据userName查询userId
* @Date 9:56 2020/2/20
* @Param [image]
* @return void
**/
public int getUserId(String userName){
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
//构建SQL语句
String sql="select * from user where name =?";
PreparedStatement statement=null;
ResultSet set=null;
try {
statement=connection.prepareStatement(sql);
//执行SQL查询
statement.setString(1,userName);
set=statement.executeQuery();
if(set.next()) {
return set.getInt("userId");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭连接
DBUtil.close(connection,statement,null);
}
return 0;
}
/* *
* @Author danni
* @Description 把image对象插入表中
* @Date 9:27 2020/2/16
* @Param []
* @return void
**/
public void insert(Image image){
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
PreparedStatement statement=null;
try {
//2,创建并拼装SQL语句
String sql="insert into image values(null,?,?,?,?,?,?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,image.getImageName());
statement.setInt(2,image.getSize());
statement.setString(3,image.getUploadTime());
statement.setString(4,image.getContentType());
statement.setString(5,image.getPath());
statement.setString(6,image.getMd5());
statement.setInt(7,image.getUserId());
//3,执行SQL语句
int res=statement.executeUpdate();
if(res!=1){
throw new ImageServerException("插入异常!");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ImageServerException e) {
e.printStackTrace();
}finally {
//4,关闭连接
DBUtil.close(connection,statement,null);
}
}
/* *
* @Author danni
* @Description 查看数据库中的所有图片信息
* @Date 9:36 2020/2/16
* @Param
* @return
**/
public List selectAll(int userId){
List list=new ArrayList<>();
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
PreparedStatement statement=null;
ResultSet set=null;
try {
//2,创建并拼装SQL语句
String sql="select * from image where userId=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,userId);
//3,执行SQL语句
set=statement.executeQuery();
while(set.next()){
Image image=new Image();
image.setImageId(set.getInt("imageId"));
image.setImageName(set.getString("imageName"));
image.setSize(set.getInt("size"));
image.setUploadTime(set.getString("uploadTime"));
image.setContentType(set.getString("contentType"));
image.setPath(set.getString("path"));
image.setMd5(set.getString("md5"));
image.setUserId(set.getInt("userId"));
list.add(image);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally {
//4,关闭连接
DBUtil.close(connection,statement,set);
}
return null;
}
/* *
* @Author danni
* @Description 根据imageID查找指定图片信息
* @Date 9:37 2020/2/16
* @Param
* @return
**/
public Image select(int imageId){
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
PreparedStatement statement=null;
ResultSet set=null;
try {
//2,创建并拼装SQL语句
String sql="select * from image where imageId=? ";
statement=connection.prepareStatement(sql);
statement.setInt(1,imageId);
//statement.setInt(2,userId);
//3,执行SQL语句
set=statement.executeQuery();
Image image=null;
while(set.next()){
image=new Image();
image.setImageId(set.getInt("imageId"));
image.setImageName(set.getString("imageName"));
image.setSize(set.getInt("size"));
image.setUploadTime(set.getString("uploadTime"));
image.setContentType(set.getString("contentType"));
image.setPath(set.getString("path"));
image.setMd5(set.getString("md5"));
image.setUserId(set.getInt("userId"));
}
return image;
} catch (SQLException e) {
e.printStackTrace();
}finally {
//4,关闭连接
DBUtil.close(connection,statement,set);
}
return null;
}
/* *
* @Author danni
* @Description 删除指定imageId图片信息
* @Date 9:38 2020/2/16
* @Param
* @return
**/
public void delete(int imageId){
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
PreparedStatement statement=null;
try {
//2,创建并拼装SQL语句
String sql="delete from image where imageId=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,imageId);
//3,执行SQL语句
int res=statement.executeUpdate();
if(res!=1){
throw new ImageServerException("删除图片异常!");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ImageServerException e) {
e.printStackTrace();
} finally {
//4,关闭连接
DBUtil.close(connection,statement,null);
}
}
public int selectByMd5(String md5,int userId){
int num=0;
//1,获取数据库连接
Connection connection=DBUtil.getConnection();
PreparedStatement statement=null;
ResultSet set=null;
try {
//2,创建并拼装SQL语句
String sql="select * from image where md5=?and userId=?";
statement=connection.prepareStatement(sql);
statement.setString(1,md5);
statement.setInt(2,userId);
//3,执行SQL语句
set=statement.executeQuery();
while(set.next()){
num++;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//4,关闭连接
DBUtil.close(connection,statement,set);
return num;
}
}
public List selectByTime(int userId,String time){
List list=new ArrayList<>();
Connection connection=DBUtil.getConnection();
PreparedStatement statement=null;
ResultSet set=null;
String sql="select * from image where userId=? and uploadTime=?";
try {
statement=connection.prepareStatement(sql);
statement.setInt(1,userId);
statement.setString(2,time);
set=statement.executeQuery();
while(set.next()){
Image image=new Image();
image.setImageId(set.getInt("imageId"));
image.setImageName(set.getString("imageName"));
image.setSize(set.getInt("size"));
image.setUploadTime(set.getString("uploadTime"));
image.setContentType(set.getString("contentType"));
image.setPath(set.getString("path"));
image.setMd5(set.getString("md5"));
image.setUserId(set.getInt("userId"));
list.add(image);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection,statement,set);
}
return null;
}
}
二,根据HTTP协议API的设计实现特定Servlet来响应用户的HTTP请求
(1)imageServlet:实现doGet方法来响应用户的查询请求,实现doDelete方法来响应用户的删除请求,实现doPost方法来实现用户上传传图片请求。
package api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dao.Image;
import dao.ImageDao;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
/**
* @ClassName ImageServlet
* @Description TODO
* @Auther danni
* @Date 2020/2/16 11:43]
* @Version 1.0
**/
public class ImageServlet extends HttpServlet {
private static HashSet set=new HashSet<>();
static {
set.add("image/jpeg");
set.add("image/gif");
set.add("image/png");
}
/* *
* @Author danni
* @Description 查看图片属性,全部/指定
* @Date 9:59 2020/2/18
* @Param [req, resp]
* @return void
**/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//根据请求url是否包含ImageId来判断是查看指定图片或者查看全部图片。
String imageId=req.getParameter("imageId");
if(imageId==null||imageId==""){
selectAll(req,resp);
}else{
selectOne(req,imageId,resp);
}
}
private void selectOne(HttpServletRequest req,String imageId, HttpServletResponse resp) throws IOException {
//从数据库查询指定图片记录
ImageDao imageDao=new ImageDao();
Image image=imageDao.select(Integer.valueOf(imageId));
if(image==null){
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write("{\"ok\":false,\"reason\":指定图片不存在}");
return;
}
Gson gson=new GsonBuilder().create();
String mess=gson.toJson(image);
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write(mess);
}
private void selectAll( HttpServletRequest req,HttpServletResponse resp) throws IOException {
//查询全部图片信息
ImageDao imageDao=new ImageDao();
String cookie= req.getHeader("cookie");
if(cookie.indexOf(";")>0){
cookie=cookie.substring(0,cookie.indexOf(";"));
}
String userName=URLDecoder.decode(cookie,"UTF-8");
int userId=imageDao.getUserId(userName);
List images=imageDao.selectAll(userId);
//将全部图片信息转为gson格式输出。
Gson gson=new GsonBuilder().create();
String message=gson.toJson(images);
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write(message);
}
/* *
* @Author danni
* @Description 删除图片
* @Date 9:59 2020/2/18
* @Param [req, resp]
* @return void
**/
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1,获取请求消息中要删除的图片中的ImageId
String imageId=req.getParameter("imageId");
if(imageId==null||imageId.equals("")){
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":}"+imageId);
return;
}
//a)根据ImageId从数据库中查找图片记录
ImageDao imageDao=new ImageDao();
Image image=imageDao.select(Integer.valueOf(imageId));
if(image==null){
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":删除图片不存在}");
return;
}
//2,根据ImageId删除数据库中的记录
imageDao.delete(Integer.valueOf(imageId));
//若数据库中不存在相同图片的记录则删除。
String md5=image.getMd5();
int userId=image.getUserId();
if(imageDao.selectByMd5(md5,userId)==0){
//3,将本地磁盘中保存的图片删除。
File file=new File(image.getPath());
file.delete();
}
}
/* *
* @Author danni
* @Description 上传图片
* @Date 9:59 2020/2/18
* @Param [req, resp]
* @return void
**/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1,获取图片的属性信息,并且存入数据库
// a) 需要创建一个 factory 对象 和 upload 对象, 这是为了获取到图片属性做的准备工作
// 固定的逻辑
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
// b)使用upload来解析请求中的图片信息, FileItem 就代表一个上传的文件对象,HTTP 支持一个请求中同时上传多个文件
List items=null;
try {
//c)使用items来保存上传图片的信息,每张图片对应items中一个FileItem
items=upload.parseRequest(req);
} catch (FileUploadException e) {
// 出现异常说明解析出错!
e.printStackTrace();
// 告诉客户端出现的具体的错误
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":解析请求错误}");
return;
}
//d)默认一次上传一张图片,先处理一张图片,创建一个Image对象来保长传图片的属性
ImageDao imageDao=new ImageDao();
FileItem item=items.get(0);
if(!set.contains(item.getContentType())){
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":图片格式错误}");
return;
}
Image image=new Image();
image.setImageName(item.getName());
image.setSize((int)item.getSize());
SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
image.setUploadTime(format.format(new Date()));
image.setContentType(item.getContentType());
String md5=DigestUtils.md5Hex(item.get());
image.setMd5(md5);
String cookie= req.getHeader("cookie");
if(cookie.indexOf(";")>0){
cookie=cookie.substring(0,cookie.indexOf(";"));
}
String userName=URLDecoder.decode(cookie,"UTF-8");
int userId=imageDao.getUserId(userName);
image.setPath("E:\\Image\\"+userName+"\\"+md5);
image.setUserId(userId);
//e)将image写入数据库
imageDao.insert(image);
if(imageDao.selectByMd5(md5,userId)==1){
//2,获取图片的内容信息,并且写入磁盘文件
File file=new File(image.getPath());
try {
item.write(file);
} catch (Exception e) {
file.delete();
imageDao.delete(image.getImageId());
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":无法写入磁盘}");
return;
}
}
//3,给客户端返回一个结果数据。
resp.sendRedirect("lw-img.html");
}
}
(2)imageShowServlet:实现doGet方法来响应用户的查看图片请求。
package api;
import dao.Image;
import dao.ImageDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
/**
* @ClassName ImageShowServlet
* @Description 查看指定图片
* @Auther danni
* @Date 2020/2/18 17:05]
* @Version 1.0
**/
public class ImageShowServlet extends HttpServlet {
private static HashSet whiteList=new HashSet<>();
static{
whiteList.add("http://localhost:9999/java_image_server/lw-img.html");
whiteList.add("http://localhost:9999/java_image_server/index.html");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String referer=req.getHeader("Referer");
if(referer.startsWith("http://localhost:9999/java_image_server")){
//获取指定图片ImageId
String imageId=req.getParameter("imageId");
if(imageId==null|imageId.equals("")){
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":解析请求错误}");
return;
}
//从数据库中查看指定图片记录
ImageDao imageDao=new ImageDao();
Image image=imageDao.select(Integer.valueOf(imageId));
if(image==null){
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("{\"ok\":fasle,\"reason\":指定图片不存在}");
return;
}
resp.setContentType(image.getContentType());
OutputStream os=resp.getOutputStream();
File file=new File(image.getPath());
FileInputStream fis=new FileInputStream(file);
int len=-1;
byte[] buffer=new byte[1024];
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
if(fis!=null){
fis.close();
}
if(os!=null){
os.close();
}
} else if (!whiteList.contains(referer)){
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("未授权!");
return;
}
}
}
(3) loginSevlet:实现doGet方法来响应用户的登录请求
package api;
import dao.ImageDao;
import dao.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
;
/**
* @ClassName LoginServlet
* @Description TODO
* @Auther danni
* @Date 2020/2/19 11:03]
* @Version 1.0
**/
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1,获取登录信息。
String name=req.getParameter("name");
String password=req.getParameter("password");
//2,构建User对象
User user=new User();
user.setName(name);
user.setPassword(password);
//查询数据库是否存在用户
ImageDao imageDao=new ImageDao();
if(imageDao.login(user)){
resp.setContentType("text/html;charset=utf-8");
resp.setHeader("Set-Cookie",URLEncoder.encode(user.getName(),"utf-8"));
resp.sendRedirect("lw-img.html");
return;
}
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("登录失败,请检查用户名和密码是否正确");
}
}
(4) registSevlet:实现doGet方法来响应用户的注册请求
package api;
import dao.ImageDao;
import dao.User;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
/**
* @ClassName RegistServlet
* @Description TODO
* @Auther danni
* @Date 2020/2/19 11:28]
* @Version 1.0
**/
public class RegistServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取注册用户信息
String name=req.getParameter("name");
String password=req.getParameter("password");
//构建User对象,将user对象写进数据库
ImageDao imageDao=new ImageDao();
User user=new User();
user.setName(name);
user.setPassword(password);
if(imageDao.register(user)){
resp.setContentType("text/html;charset=utf-8");
resp.setHeader("Set-Cookie",URLEncoder.encode(user.getName(),"utf-8"));
resp.sendRedirect("lw-img.html");
return;
}
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("注册失败!");
}
}
(5)实现SelectServlet对用户按照上传时间查询图片的请求
package api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dao.Image;
import dao.ImageDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
/**
* @ClassName selectServlet
* @Description TODO
* @Auther danni
* @Date 2020/2/21 9:54]
* @Version 1.0
**/
public class SelectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String time=req.getParameter("time");
String cookie= req.getHeader("cookie");
if(cookie.indexOf(";")>0){
cookie=cookie.substring(0,cookie.indexOf(";"));
}
String userName=URLDecoder.decode(cookie,"UTF-8");
ImageDao imageDao=new ImageDao();
int userId=imageDao.getUserId(userName);
List images=imageDao.selectByTime(userId,time);
if(images!=null){
Gson gson=new GsonBuilder().create();
String message=gson.toJson(images);
resp.setContentType("appliction/json;charset=utf-8");
resp.getWriter().write(message);
}else{
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("当前日期未上传图片"+userId+time);
}
}
}
三,配置web.xml,由Tomcat组件来解析xml通过反射动态的根据url获取到对应的servlet类。
四,前端操作
1,使用模板来实现简单的图片显示
2,使用Vue渲染来动态显示图片
3,使用Script来获取图片属性