第九章 分页 上传的实现

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Mythology_px/article/details/82765807

分页

           封装分页属性为实体类

package com.entity;

import java.util.List;

/*
 * 跳转页面时封装成一个实体类放入域
 */
public class Page {
	// 当前页
	private int currentPage;
	// 总记录数
	private int count;
	// 每页的记录数
	private static int rows = 5;
	// 总页数
	private int countPage;;
	// 数据库分页查询出来的数据
	private List<Emp> list;
	
	// 公有的方法提供操作
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
		countPage= (int) Math.ceil(count*1.0/rows);
	}
	public static int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		Page.rows = rows;
		countPage= (int) Math.ceil(count*1.0/rows);
	}
	public List<Emp> getList() {
		return list;
	}
	public void setList(List<Emp> list) {
		this.list = list;
	}
	public int getCountPage() {
		return countPage;
	}

}

           封装分页查询的方法:数据库的连接也进行了封装了

// 按照传入的参数分页查询数据库中的数据
	@Override
	public List<Emp> findCurrentPage(int currentPage) {
		// 创建Oracle数据库对象
		OracleConne ocnn = new OracleConne();
		// 获取Oracle的数据库执行体对象
		Statement exeSQL = ocnn.getOracle();
		// 执行SQL语句,操作结果集
		/*
		 * 每页记录数Page.getRows() 当前页是currentPage
		 * 
		 * select * from (select rownum R, emp.* from emp) where R>(当前页-1)*记录数
		 * and R<=当前页*记录数
		 */
		int minRows = (currentPage - 1) * Page.getRows();
		int maxRows = currentPage * Page.getRows();
		String sql = "select * from (select rownum R, emp.* from emp) where R>"+minRows+" and R<="+maxRows;
		ResultSet rs = (ResultSet) ocnn.executeSQL(exeSQL, sql);
		List<Emp> list = new ArrayList<Emp>();
		try {
			// 遍历数据存入集合
			while (rs.next()) {
				int empno = rs.getInt(2);
				String ename = rs.getString(3);
				String sex = rs.getString(4);
				int sal = rs.getInt(5);
				long phone = rs.getLong(6);
				Date birthday = rs.getDate(7);
				String addr = rs.getString(8);
				Emp emp = new Emp(empno, ename, sex, sal, phone, birthday, addr);
				list.add(emp);
			}
		} catch (SQLException e) {
			System.err.println("员工表数据分页提取失败");
		}
		ocnn.closeDB(rs);
		return list;
	}

           业务层将分页数据封装进对象

// 在业务层将DAO层分页的数据进行封装进Page对象
	public Page findCurrentPage(int currentPage){
		FindEmpImp finde = new FindEmpImp();
		Page page = new Page();
		page.setCount(finde.findCount());
		page.setCurrentPage(currentPage);
		page.setList(finde.findCurrentPage(page.getCurrentPage()));
		return page;	
	}

           在控制层Servlet中将对象加载,存入作用域中,在页面就可以通过EL表达式和JSTL获取分页数据

 

上传

           文件上传的方法: .SmartUpload  .ApacheCommons-fileupload

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>文件上传</title>
  </head>
  
  <body>
  	<%
  		/*
  			文件上传的form表单与传统的form表单提交有所区别:
  			① 引入jar包在web-inf下的lib文件夹: commons-fileupload,commons-io
  			② 设置form表单的属性: method属性必须为post,设置enctype="multipart/form-data"
  		*/
  	 %>
  	<form action="uploadServlet" method="post" enctype="multipart/form-data">
  		标题:<input type="text" name="tittle" /><br />
  		<!--在JSP页面设置file类型的上传框:name属性必须写,当文件上传之后,name的值为文件名--> 
  		文件:<input type="file" name="file" /><br />
  		<input type="submit" />
  	</form>
  </body>
</html>
package com.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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 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;

/*
 * 	控制页面上传的Servlet
 */
public class UploadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置post请求方式的编码
		request.setCharacterEncoding("utf-8");
		// 获取提交的form表单数据
		// ① 创建文件组工厂
		FileItemFactory fif = new DiskFileItemFactory();
		// ② 通过文件工厂创建文件上传对象
		ServletFileUpload sfu = new ServletFileUpload(fif);
		// ③ 获取form表单中的所有数据,返回list集合
		List<FileItem> list = null;
		try {
			list =sfu.parseRequest(request);
		} catch (FileUploadException e) {
			System.out.println("文件解析请求失败");
		}
		// ④ 便利集合中的数据
		Iterator<FileItem> it =list.iterator();
		while(it.hasNext()){
			FileItem fileItem = it.next();
			// 判断该元素是否是一个普通的form元素,即不是文件
			if(fileItem.isFormField()){
				// 获取该form元素的name的值
				System.out.println(fileItem.getFieldName());
				// 获取该form元素的value值--中文必须指定编码集
				System.out.println(fileItem.getString("utf-8"));
			}else{
				// 文件
				// 获取该文件对象的名字
				String fileName = fileItem.getName();
				// 将文件通过流写出到服务器的项目目录中
				// ① 获取服务器的动态路径
				String path = request.getRealPath("/img/"+fileName);
				// ② 创建流和文件对象
				FileOutputStream fos = new FileOutputStream(new File(path));
				// ③ 将文件对象写出(将对象转换为字节数组)
				fos.write(fileItem.get());
				fos.flush();
				fos.close();
				// 将图片名存入session对象,用于跳转页面输出文件
				HttpSession session = request.getSession();
				session.setAttribute("name", fileName);
				request.getRequestDispatcher("/show.jsp").forward(request, response);
			}
		}
	}

}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>显示上传后的图片</title>
  </head>
  
  <body>
  	<img src="./img/${name}">
  </body>
</html>

猜你喜欢

转载自blog.csdn.net/Mythology_px/article/details/82765807
今日推荐