开发工具:IDEA
开发环境:jdk1.8+Tomcat8.5
一,先采用IDEA新建一个web项目
二,创建数据库和表
三,导入项目开发中需要的jar包
四,构建项目目录结构
五,创建Image实体类
public class Image {
private int id;
private String title;//图片名称
public Image() {
}
public Image(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
六,编写jdbc链接数据库
public class JDBC {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8";
private static final String USERNAME = "root";
private static final String PWD = "12345678";
//加载驱动
static {
try {
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
//链接数据库
public static Connection getCon(){
Connection con = null;
try {
con = DriverManager.getConnection(URL,USERNAME,PWD);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
//关闭链接
public static void closeCon(Connection con, PreparedStatement pre, ResultSet rst){
try {
if (con!=null)
con.close();
if (pre!=null)
pre.close();
if (rst!=null)
rst.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
七,编写service业务类
/**
* 编写service
*/
public class ImageService {
Connection con = null;
PreparedStatement pre = null;
ResultSet rst = null;
//添加
public void addImage(Image image){
try {
con = JDBC.getCon();
String sql = "insert into images(title)values(?)";
pre = con.prepareStatement(sql);
pre.setString(1,image.getTitle());
int i = pre.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBC.closeCon(con,pre,rst);
}
}
//查询全部
public List<Image> selectAll(){
List<Image> list = new ArrayList<>();
try {
con = JDBC.getCon();
String sql = "select * from images";
pre = con.prepareStatement(sql);
rst = pre.executeQuery();
while (rst.next()){
int id = rst.getInt(1);
String title = rst.getString(2);
Image image = new Image(id, title);
list.add(image);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBC.closeCon(con,pre,rst);
}
return list;
}
}
七,编写index.jsp页面作为图片上传页面
八,编写上传图片的servlet类
/**
* 图片上传
*/
@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {
protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
//创建解析类的实例
ServletFileUpload sfu = new ServletFileUpload(factory);
//开始解析
sfu.setFileSizeMax(1024*400);
//每个表单域中数据会封装到一个对应的FileItem对象上
try {
List<FileItem> items = sfu.parseRequest(request);
//区分表单域
for (int i = 0; i < items.size(); i++) {
FileItem item = items.get(i);
//isFormField为true,表示这不是文件上传表单域
if(!item.isFormField()){
ServletContext sctx = getServletContext();
//获得存放文件的物理路径
// upload下的某个文件夹 得到当前在线的用户 找到对应的文件夹
String path = "d:\\images";
System.out.println(path);
//获得文件名
String fileName =UUID.randomUUID()+item.getName();
System.out.println(fileName);
//该方法在某些平台(操作系统),会返回路径+文件名
fileName = fileName.substring(fileName.lastIndexOf("/")+1);
File file = new File(path+"\\"+fileName);
if(!file.exists()){
item.write(file);
//将上传图片的名字记录到数据库中
Image image = new Image();
image.setTitle(fileName);
new ImageService().addImage(image);
response.sendRedirect("ALL");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
九,编写展示图片的servlet类
/**
* 图片展示
*/
@WebServlet("/ALL")
public class ALLServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Image> images = new ImageService().selectAll();
request.setAttribute("images",images);
request.getRequestDispatcher("List.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
十,编写list.jsp页面用来展示图片
十一,配置图片存放的文件夹
1)首先在D盘创建一个文件夹命名为images
2,在tomcat配置中选择此文件夹
十二,启动服务器访问操作就OK了
点击“”“保存”按钮