分页逻辑java_Java逻辑分页代码

packagecom.ksource.platform.controller.system;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.ksource.platform.model.system.SysAuditModelType;importcom.ksource.platform.model.system.SysFile;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.multipart.MultipartFile;importorg.springframework.web.multipart.MultipartHttpServletRequest;importorg.springframework.web.servlet.ModelAndView;importcom.ksource.core.annotion.Action;importcom.ksource.core.annotion.ActionExecOrder;importcom.ksource.core.util.AppConfigUtil;importcom.ksource.core.util.FileUtil;importcom.ksource.core.web.ResultMessage;importcom.ksource.core.web.controller.BaseController;importcom.ksource.core.web.query.QueryFilter;importcom.ksource.core.web.util.RequestUtil;importcom.ksource.platform.service.system.SysFileService;importcom.ksource.pwlp.controller.system.SysOffice;

@Controller

@RequestMapping({"/platform/system/sysOffice/"})

@Action(ownermodel=SysAuditModelType.BASICDATA_MANAGEMENT)public class SysOfficeController extendsBaseController {

@ResourceprivateSysFileService sysFileService;/*** 查看系统文书模版分页列表

*@paramrequest

*@return*@throwsException*/@RequestMapping({"list"})

@Action(description= "查看系统文书模版分页列表", detail = "查看系统文书模版分页列表")public ModelAndView list(HttpServletRequest request) throwsException {

QueryFilter filter= new QueryFilter(request, "sysOfficeItem");

String officePath= request.getSession().getServletContext().getRealPath("/")+"WEB-INF/fileTemplate/office";

List sysOfficeList = new ArrayList();

findFileList(newFile(officePath),sysOfficeList,filter);//pageSize 每页展示个数

int pageSize = RequestUtil.getInt(request, "pageSize",10);//currentPage 当前页

int currentPage = RequestUtil.getInt(request, "currentPage",1);//totalNum 总数

int totalNum =sysOfficeList.size();//totalPage 总页数

int totalPage = 0;if(sysOfficeList.size() % pageSize==0){//说明整除,正好每页显示pageSize条数据,没有多余一页要显示少于pageSize条数据的

totalPage = sysOfficeList.size() /pageSize;

}else{//不整除,就要在加一页,来显示多余的数据。

totalPage = sysOfficeList.size() / pageSize + 1;

}

List officeList = new ArrayList();for (int i = (currentPage-1)*pageSize; i < currentPage*pageSize; i++) {if(i

officeList.add(sysOfficeList.get(i));

}

}

ModelAndView mv= getAutoView().addObject("sysOfficeList", officeList).addObject("pageSize", pageSize).addObject("currentPage", currentPage).addObject("totalNum", totalNum).addObject("totalPage", totalPage);

Object obj= filter.getFilters().get("officeName");if(obj != null){

String officeName=obj.toString();

mv.addObject("officeName", officeName);

}returnmv;

}/*** 查看系统文书模版明细

*@paramrequest

*@return*@throwsException*/@RequestMapping({"get"})

@Action(description= "查看系统文书模版明细", detail = "查看系统文书模版明细")public ModelAndView get(HttpServletRequest request) throwsException {

String officePath= RequestUtil.getString(request, "officePath");

SysOffice sysOffice= newSysOffice();

File file= newFile(officePath);if(file != null){

String officeName= file.getName().substring(0, file.getName().lastIndexOf("."));

sysOffice.setOfficeName(officeName);

String officeExt= file.getName().substring(file.getName().lastIndexOf(".")+1, file.getName().length());

sysOffice.setOfficeExt(officeExt);

sysOffice.setOfficePath(officePath);

sysOffice.setHideOfficePath(sysOffice.getOfficePath().replace("\\", "//"));

}return getAutoView().addObject("sysOffice", sysOffice);

}/*** 编辑文书模板页面

*@paramresponse

*@paramrequest

*@return*@throwsException*/@RequestMapping("/add")

@Action(description= "编辑文书模板页面", detail = "编辑文书模板页面")public ModelAndView add(HttpServletResponse response, HttpServletRequest request) throwsException {

String officePath= RequestUtil.getString(request, "officePath", "");

SysOffice sysOffice= newSysOffice();if(!"".equals(officePath)){

File file= newFile(officePath);if(file != null){

String officeName= file.getName().substring(0, file.getName().lastIndexOf("."));

sysOffice.setOfficeName(officeName);

String officeExt= file.getName().substring(file.getName().lastIndexOf(".")+1, file.getName().length());

sysOffice.setOfficeExt(officeExt);

sysOffice.setOfficePath(officePath);

sysOffice.setHideOfficePath(sysOffice.getOfficePath().replace("\\", "//"));

}

}

ModelAndView mv= this.getAutoView();return mv.addObject("sysOffice", sysOffice);

}/*** 编辑文书模板页面

*@paramresponse

*@paramrequest

*@return*@throwsException*/@RequestMapping("/edit")

@Action(description= "编辑文书模板页面", detail = "编辑文书模板页面")public ModelAndView edit(HttpServletResponse response, HttpServletRequest request) throwsException {

String officePath= RequestUtil.getString(request, "officePath", "");

SysOffice sysOffice= newSysOffice();if(!"".equals(officePath)){

File file= newFile(officePath);if(file != null){

String officeName= file.getName().substring(0, file.getName().lastIndexOf("."));

sysOffice.setOfficeName(officeName);

String officeExt= file.getName().substring(file.getName().lastIndexOf(".")+1, file.getName().length());

sysOffice.setOfficeExt(officeExt);

sysOffice.setOfficePath(officePath);

sysOffice.setHideOfficePath(sysOffice.getOfficePath().replace("\\", "//"));

}

}

ModelAndView mv= this.getAutoView();return mv.addObject("sysOffice", sysOffice);

}/*** 加载文书模板

*@paramresponse

*@paramrequest

*@return*@throwsException*/@RequestMapping("/editTemplate")public ModelAndView editTemplate(HttpServletResponse response, HttpServletRequest request) throwsException {

String hideOfficePath= RequestUtil.getString(request, "hideOfficePath");

ModelAndView mv= this.getAutoView();if(!"".equals(hideOfficePath)){

File file= new File(hideOfficePath.replace("//", "\\"));if(file != null){

String officeName= file.getName().substring(0, file.getName().lastIndexOf("."));

String officeExt= file.getName().substring(file.getName().lastIndexOf(".")+1, file.getName().length());

mv.addObject("officeName", officeName);

mv.addObject("officeExt", officeExt);

}

}

mv.addObject("hideOfficePath", hideOfficePath);returnmv;

}/*** 获取文书模板

*@paramrequest

*@paramresponse

*@throwsException*/@RequestMapping("getTemplate")

@Action(description= "获取文书模板", detail = "获取文书模板")public void getTemplate(HttpServletRequest request, HttpServletResponse response) throwsException {

String hideOfficePath= RequestUtil.getString(request, "hideOfficePath");

hideOfficePath= java.net.URLDecoder.decode(hideOfficePath , "UTF-8");

hideOfficePath= hideOfficePath.replace("/", "\\");byte[] readByte =FileUtil.readByte(hideOfficePath);try{

response.getOutputStream().write(readByte);

}catch(IOException e) {

e.printStackTrace();

}

}

@RequestMapping({"saveOffice"})

@Action(description= "保存文书模板", execOrder =ActionExecOrder.AFTER)public void saveFile(MultipartHttpServletRequest request, HttpServletResponse response) throwsException {

String hideOfficePath= RequestUtil.getString(request, "hideOfficePath");

PrintWriter writer=response.getWriter();try{

Map files=request.getFileMap();

Iterator it=files.values().iterator();while(it.hasNext()) {

MultipartFile f=(MultipartFile) it.next();

File file= newFile(hideOfficePath);if(file.exists()) {

file.delete();

}

FileUtil.writeByte(hideOfficePath, f.getBytes());

writeResultMessage(writer,"保存文书模板成功!", ResultMessage.Success);

}

}catch(Exception e) {

writeResultMessage(writer,"保存文书模板失败!", ResultMessage.Fail);

}

}/*** 删除系统文书模版

*@paramrequest

*@paramresponse

*@throwsException*/@RequestMapping({"del"})

@Action(description= "删除系统文书模版", execOrder = ActionExecOrder.BEFORE, detail = "删除系统文书模版:【${entity.subject}】#list>")public void del(HttpServletRequest request, HttpServletResponse response) throwsException {

String preUrl=RequestUtil.getPrePage(request);

ResultMessage message;try{

String[] officePath= RequestUtil.getStringAryByStr(request, "officePath");for (int i = 0; i < officePath.length; i++) {

File file= newFile(officePath[i]);if(file.isFile()) {

file.delete();

}

}

message= new ResultMessage(1, "删除文书成功!");

}catch(Exception ex) {

message= new ResultMessage(0, "删除文书失败:" +ex.getMessage());

}

addMessage(message, request);

response.sendRedirect(preUrl);

}/*** 文件下载

*

* @Description:

*@paramfileName

*@paramrequest

*@paramresponse

*@return*@throwsException*/@RequestMapping("/download")publicString downloadFile(HttpServletRequest request,

HttpServletResponse response)throwsException {

String officeName= RequestUtil.getString(request, "officeName");

officeName= java.net.URLDecoder.decode(officeName , "UTF-8");

String officeExt= RequestUtil.getString(request, "officeExt");

String fullOfficeName= officeName + "." +officeExt;

fullOfficeName= fullOfficeName.replace(" ", "");

fullOfficeName= URLEncoder.encode(fullOfficeName,"UTF-8");

String hideOfficePath= RequestUtil.getString(request, "hideOfficePath");

String officePath= hideOfficePath.replace("//", "\\");if (officePath != null && !"".equals(officePath)) {

File file= newFile(officePath);if(file.exists()) {

response.setContentType("application/force-download");//设置强制下载不打开

response.setHeader("Content-disposition","attachment; filename="+fullOfficeName);byte[] buffer = new byte[1024];

FileInputStream fis= null;

BufferedInputStream bis= null;try{

fis= newFileInputStream(file);

bis= newBufferedInputStream(fis);

OutputStream os=response.getOutputStream();int i =bis.read(buffer);while (i != -1) {

os.write(buffer,0, i);

i=bis.read(buffer);

}

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}finally{if (bis != null) {try{

bis.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}if (fis != null) {try{

fis.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}return null;

}/*** 上传文书到指定目录

*@paramrequest

*@paramresponse

*@throwsException*/@RequestMapping({"upload"})

@ResponseBody

@Action(description= "上传文书到指定目录")public Map upload(HttpServletRequest request, HttpServletResponse response) throwsException {

Long[] sysOfficeIds= RequestUtil.getLongAryByStr(request, "sysOfficeIds");

Map map= newHashMap();for (int m = 0; m < sysOfficeIds.length; m++) {long fileId =sysOfficeIds[m];if (fileId == 0L) {

map.put("result", "fileIdIsZero");returnmap;

}

SysFile sysFile= this.sysFileService.getById(Long.valueOf(fileId));if (sysFile == null) {

map.put("result", "fileNoExist");returnmap;

}

String fileName= sysFile.getFileName() + "." +sysFile.getExt();

String fileServicePath= AppConfigUtil.get("file_server_addr");

String fullPath= "http://" + fileServicePath + File.separator + sysFile.getFilePath().replace("/", File.separator);

fullPath= fullPath.replace("\\", "/");

String uploadlujing= request.getSession().getServletContext().getRealPath("/")+"WEB-INF/fileTemplate/office";

saveUrlAs(fullPath, uploadlujing, fileName,"GET");

}

map.put("result", "fileUploadEnd");returnmap;

}public static void findFileList(File dir, ListsysOfficeList, QueryFilter queryFilter) {if (!dir.exists() || !dir.isDirectory()) {//判断是否存在目录

return;

}

String[] files= dir.list();//读取目录下的所有目录文件信息

for (int i = 0; i < files.length; i++) {//循环,添加文件名或回调自身

File file = newFile(dir, files[i]);if (file.isFile()) {//如果文件

SysOffice sysOffice = newSysOffice();if(file.getName() != null){

String officeName= file.getName().substring(0, file.getName().lastIndexOf("."));

Object obj= queryFilter.getFilters().get("officeName");if(obj != null){

String ofn=obj.toString();if(!"".equals(ofn) && ofn != null){if(!officeName.contains(ofn)){continue;

}

}

}

sysOffice.setOfficeName(officeName);

String officeExt= file.getName().substring(file.getName().lastIndexOf(".")+1, file.getName().length());

sysOffice.setOfficeExt(officeExt);

}

sysOffice.setOfficePath(dir+ "\\" +file.getName());

sysOffice.setHideOfficePath(sysOffice.getOfficePath().replace("\\", "//"));

sysOfficeList.add(sysOffice);//添加文件全路径名

} else {//如果是目录

findFileList(file, sysOfficeList, queryFilter);//回调自身继续查询

}

}

}public staticFile saveUrlAs(String url, String filePath, String fileName, String method) {//System.out.println("fileName---->"+filePath);//创建不同的文件夹目录

File file = newFile(filePath);//判断文件夹是否存在

if (!file.exists()) {//如果文件夹不存在,则创建新的的文件夹

file.mkdirs();

}

FileOutputStream fileOut= null;

HttpURLConnection conn= null;

InputStream inputStream= null;try{//建立链接

URL httpUrl = newURL(url);

conn=(HttpURLConnection) httpUrl.openConnection();//以Post方式提交表单,默认get方式

conn.setRequestMethod(method);

conn.setDoInput(true);

conn.setDoOutput(true);//post方式不能使用缓存

conn.setUseCaches(false);//连接指定的资源

conn.connect();//获取网络输入流

inputStream =conn.getInputStream();

BufferedInputStream bis= newBufferedInputStream(inputStream);//判断文件的保存路径后面是否以/结尾

if (!filePath.endsWith("/")) {

filePath+= "/";

}//写入到文件(注意文件保存路径的后面一定要加上文件的名称)

fileOut = new FileOutputStream(filePath +fileName);

BufferedOutputStream bos= newBufferedOutputStream(fileOut);byte[] buf = new byte[4096];int length =bis.read(buf);//保存文件

while (length != -1) {

bos.write(buf,0, length);

length=bis.read(buf);

}

bos.close();

bis.close();

conn.disconnect();

}catch(Exception e) {

e.printStackTrace();

System.out.println("抛出异常!!");

}returnfile;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值