ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-util介绍与开发首页

ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-util介绍与开发首页

    下面把util工具类也分享出来吧,util弄好之后,就可以启动项目到tomcat7试试,如果没问题,就说明ssm框架的整合已经成功一大半了。  

    包package com.steadyjack.util,建立下面各个工具类用于后期各个组件使用。

    CryptographyUtil.java加密解密工具类:

 

package com.steadyjack.util;

import org.apache.shiro.crypto.hash.Md5Hash;

/**
 * title:CryptographyUtil.java
 * description:加密工具(主要是shiro的md5)
 * time:2017年1月16日 下午10:55:01
 * author:debug-steadyjack
 */
public class CryptographyUtil {

	/**
	 * title:CryptographyUtil.java
	 * description:Md5加密
	 * time:2017年1月16日 下午10:55:20
	 * author:debug-steadyjack
	 * @param str 密码
	 * @param salt 盐
	 * @return
	 */
	public static String md5(String str,String salt){
		return new Md5Hash(str,salt).toString();
	}
	
	public static void main(String[] args) {
		String password="linsen";
		
		System.out.println("Md5加密:"+CryptographyUtil.md5(password, "debug"));
	}
}


    DateUtil.java日期工具类:

 

 

package com.steadyjack.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * title:DateUtil.java
 * description:日期处理工具类
 * time:2017年1月16日 下午10:38:01
 * author:debug-steadyjack
 */
public class DateUtil {

	/**
	 * title:DateUtil.java
	 * description:日期对象转字符串
	 * time:2017年1月16日 下午10:40:49
	 * author:debug-steadyjack
	 * @param date
	 * @param format
	 * @return String
	 */
	public static String formatDate(Date date,String format){
		String result=null;
		try {
			SimpleDateFormat sdf=new SimpleDateFormat(format);
			if(date!=null){
				result=sdf.format(date);
			}
		} catch (Exception e) {
			System.out.println("日期转字符串发生异常: "+e.getMessage());
		}
		return result;
	}
	
	/**
	 * title:DateUtil.java
	 * description:字符串转日期对象
	 * time:2017年1月16日 下午10:43:14
	 * author:debug-steadyjack
	 * @param str
	 * @param format
	 * @throws Exception
	 */
	public static Date formatString(String str,String format){
		Date date=null;
		try {
			if(StringUtil.isNotEmpty(str)){
				SimpleDateFormat sdf=new SimpleDateFormat(format);
				date=sdf.parse(str);
			}
		} catch (Exception e) {
			System.out.println("字符串转日期出错: "+e.getMessage());
		}
		return date;
	}
	
	/**
	 * title:DateUtil.java
	 * description: 获取当前年月日时分秒组成的串:可用于命名图片、文件
	 * time:2017年1月16日 下午10:48:48
	 * author:debug-steadyjack
	 * @return String
	 * @throws Exception
	 */
	public static String getCurrentDateStr()throws Exception{
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
		return sdf.format(date);
	}
	
}


     PageUtil.java分页工具类:

 

 

package com.steadyjack.util;

/**
 * title:PageUtil.java
 * description:分页工具类
 * time:2017年1月16日 下午10:53:47
 * author:debug-steadyjack
 */
public class PageUtil {

	/**
	 * 同步生成分页代码  (参考网上的)
	 * @param targetUrl 目标地址
	 * @param totalNum 总记录数
	 * @param currentPage 当前页
	 * @param pageSize 每页大小
	 * @return
	 */
	public static String genPagination(String targetUrl,long totalNum,int currentPage,int pageSize,String param){
		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("
  • 首页
  • "); if(currentPage>1){ pageCode.append("
  • 上一页
  • "); }else{ pageCode.append("
  • 上一页
  • "); } for(int i=currentPage-2;i<=currentPage+2;i++){ if(i<1||i>totalPage){ continue; } if(i==currentPage){ pageCode.append("
  • "+i+"
  • "); }else{ pageCode.append("
  • "+i+"
  • "); } } if(currentPage下一页"); }else{ pageCode.append("
  • 下一页
  • "); } pageCode.append("
  • 尾页
  • "); return pageCode.toString(); } } }


        ResponseUtil.java响应工具类:

     

     

    package com.steadyjack.util;
    
    import java.io.PrintWriter;
    
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * title:ResponseUtil.java
     * description: 将数据对象Object写回响应流(可用于js异步调用)
     * time:2017年1月16日 下午10:52:43
     * author:debug-steadyjack
     */
    public class ResponseUtil {
    
    	/**
    	 * title:ResponseUtil.java
    	 * description: 将数据对象Object写回响应流
    	 * time:2017年1月16日 下午10:53:23
    	 * author:debug-steadyjack
    	 * @param response
    	 * @param o
    	 * @throws Exception
    	 */
    	public static void write(HttpServletResponse response,Object o)throws Exception{
    		response.setContentType("text/html;charset=utf-8");
    		PrintWriter out=response.getWriter();
    		out.println(o.toString());
    		out.flush();
    		out.close();
    	}
    }
    


        StringUtil.java字符串工具类:

     

     

    package com.steadyjack.util;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * title:StringUtil.java
     * description:字符串简单处理工具类
     * time:2017年1月16日 下午10:44:02
     * author:debug-steadyjack
     */
    public class StringUtil {
    
    	/**
    	 * title:StringUtil.java
    	 * description:判断是否是空
    	 * time:2017年1月16日 下午10:44:21
    	 * author:debug-steadyjack
    	 * @param str
    	 * @return boolean
    	 */
    	public static boolean isEmpty(String str){
    		if(str==null || "".equals(str.trim())){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	
    	/**
    	 * title:StringUtil.java
    	 * description:判断是否不是空
    	 * time:2017年1月16日 下午10:44:44
    	 * author:debug-steadyjack
    	 * @param str
    	 * @return boolean
    	 */
    	public static boolean isNotEmpty(String str){
    		if((str!=null)&&!"".equals(str.trim())){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	
    	/**
    	 * title:StringUtil.java
    	 * description: 格式化模糊查询
    	 * time:2017年1月16日 下午10:45:09
    	 * author:debug-steadyjack
    	 * @param str
    	 * @return String
    	 */
    	public static String formatLike(String str){
    		if(isNotEmpty(str)){
    			return "%"+str+"%";
    		}else{
    			return null;
    		}
    	}
    	
    	/**
    	 * title:StringUtil.java
    	 * description:过滤掉集合里的空格
    	 * time:2017年1月16日 下午10:45:34
    	 * author:debug-steadyjack
    	 * @param list
    	 * @return List
    	 */
    	public static List filterWhite(List list){
    		List resultList=new ArrayList();
    		for(String str:list){
    			if(isNotEmpty(str)){
    				resultList.add(str);
    			}
    		}
    		return resultList;
    	}
    
    }
    


        WebFileOperationUtil.java web环境下对于一些文件如百度编辑器上传的图片、独立上传的文件进行处理:

     

     

    package com.steadyjack.util;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class WebFileOperationUtil {
    	
    	/**
    	 * title:WebFileOperationUtil.java
    	 * description:将百度编辑器的内容的图片移动到指定的位置,并将新的位置替换内容中对于的旧位置
    	 * time:2017年2月7日 下午7:17:35
    	 * author:debug-steadyjack
    	 * @param request
    	 * @param content
    	 * @return
    	 */
    	public static String copyImageInUeditor(HttpServletRequest request,String content){
    		if (StringUtil.isNotEmpty(content)) {
    			Document doc=Jsoup.parse(content);
    			
    			Elements imageList=doc.select("img"); 
    			if (imageList!=null && imageList.size()>0) {
    				for(int i=0;i0) {
    						String srcImage=oldImage.substring(index);
    						String secIndex="\"";
    						String realImagePos=srcImage.substring(0,srcImage.indexOf(secIndex));
    						
    						String folder="ueditor";
    						String newImagePos=WebFileUtil.copyFileForUeditor(request, realImagePos, folder);
    						content = content.replace(realImagePos, newImagePos);
    					}
    				}
    			}
    		}
    		
    		return content;
    	}
    	
    	/**
    	 * title:WebFileOperationUtil.java
    	 * description:删除百度编辑器内容的图片
    	 * time:2017年2月7日 下午9:01:23
    	 * author:debug-steadyjack
    	 * @param request
    	 * @param content
    	 * @throws Exception
    	 */
    	public static void deleteImagesInUeditor(HttpServletRequest request,String content) throws Exception{
    		if (StringUtil.isNotEmpty(content)) {
    			Document doc=Jsoup.parse(content);
    			
    			Elements imageList=doc.select("img"); 
    			if (imageList!=null && imageList.size()>0) {
    				for(int i=0;i0) {
    						String srcImage=oldImage.substring(index+1);
    						String delImagePos=srcImage.substring(0,srcImage.indexOf(charIndex));
    						System.out.println(delImagePos);
    						
    						WebFileUtil.deleteFilePath(WebFileUtil.getSystemRootPath(request)+delImagePos);
    					}
    				}
    			}
    		}
    	}
    	
    	
    	public static void main(String[] args) {
    		String image="img src=\"/temporary/2017-02-06/image/1486394487364095169.jpg\"";
    		String str="/temporary/2017-02-06/image/1486394487364095169.jpg"; //51
    		
    		String charIndex="/temporary";
    		int index=image.indexOf(charIndex);
    		System.out.println(index);
    		String newStr=image.substring(index, str.length()+index);
    		System.out.println(newStr);
    	}
    	
    }
    


        WebFileUtil.java文件操作工具类:

     

     

    package com.steadyjack.util;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.util.FileCopyUtils;
    
    /**
     * title:WebFileUtil.java 
     * description:文件操作(包括网络上的文件)工具类 
     * time:2017年1月22日
     * 下午9:41:57 
     * author:debug-steadyjack
     */
    public class WebFileUtil {
    	
    	/**
    	 * title:WebFileUtil.java
    	 * description:获取web运行时项目的根路径
    	 * time:2017年1月22日 下午10:00:08
    	 * author:debug-steadyjack
    	 * @param request
    	 * @return
    	 */
    	public static String getSystemRootPath(HttpServletRequest request){
    		String rootPath=request.getServletContext().getRealPath("/");
    		//request.getContextPath();
    		return rootPath;
    	}
    
    	/**
    	 * title:WebFileUtil.java
    	 * description:下载文件(包括网络文件)
    	 * time:2017年1月22日 下午9:59:24
    	 * author:debug-steadyjack
    	 * @param request
    	 * @param response
    	 * @param url 文件路径
    	 * @param fileName 文件名
    	 */
    	public static void downloadFile(HttpServletRequest request,HttpServletResponse response, 
    			String url,String fileName) {
    
    		String root = getSystemRootPath(request);
    
    		//用给定的fileName与url的后缀名拼成一新的字符串作为新的文件名
    		fileName = fileName + url.substring(url.lastIndexOf("."));
    		
    		OutputStream os = null;
    		InputStream fis = null;
    		try {
    			//解决中文 文件名问题
    			fileName = new String(fileName.getBytes(), "iso8859-1");
    			response.setContentType("application/octet-stream");
    			response.addHeader("Content-Disposition", "attachment;filename=\""+ fileName + "\"");
    
    			os=response.getOutputStream();
    			if (url.startsWith("http:")) {
    				URL tmpURL = new URL(url);
    				URLConnection conn = tmpURL.openConnection();
    				fis = conn.getInputStream();
    			} else {
    				fis = new FileInputStream(new File(root + url));
    			}
    
    			byte[] b = new byte[1024];
    			int i = 0;
    
    			while ((i = fis.read(b)) > 0) {
    				os.write(b, 0, i);
    			}
    
    			os.flush();
    		} catch (Exception e) {
    			System.out.println("下载文件发生异常: "+e.getMessage());
    		} finally{
    			try {
    				os.close();
    				fis.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	/**
    	 * title:WebFileUtil.java
    	 * description:创建文件夹
    	 * time:2017年2月6日 下午10:50:17
    	 * author:debug-steadyjack
    	 * @param dFile
    	 */
        public static void createFold(File file) {
    		if (!file.exists()) file.mkdirs();
    	}
        
        /**
         * title:WebFileUtil.java
         * description:复制百度编辑器中的图片到指定的文件夹下,并返回图片存储的实际路径
         * time:2017年2月6日 下午10:58:58
         * author:debug-steadyjack
         * @param request
         * @param srcPath
         * @param floder
         * @return
         */
        public static String copyFileForUeditor(HttpServletRequest request,String srcPath, String folder){
        	
        	String newFolder="";
        	String fileName="";
        	
    		try {
    			File oldFile=new File(getSystemRootPath(request)+srcPath);
    			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    			
    			newFolder="/files/"+folder+"/"+sdf.format(new Date())+"/";
    			String newFilePath=getSystemRootPath(request)+newFolder;
    			createFold(new File(newFilePath));
    			
    			fileName=oldFile.getName();
    			File newFile=new File(newFilePath+fileName);
    			FileCopyUtils.copy(oldFile, newFile);
    			//FileUtils.copyFile(oldFile, saveFile);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		return newFolder+fileName;
        }
        
        
        /**
         * title:WebFileUtil.java
         * description:删除给定路径下的文件(包括单独的文件;文件夹)
         * time:2017年2月6日 下午11:02:12
         * author:debug-steadyjack
         * @param path
         * @return
         * @throws Exception
         */
        public static boolean deleteFilePath(String path) throws Exception {  
    		
    		try {
    			File file = new File(path);
    			
    			// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
    			if(!file.isDirectory()){
    				file.delete();
    			}else if(file.isDirectory()){
    				String[] filelist = file.list();
    				
    				for(int i = 0; i < filelist.length; i++) {
    					File delfile = new File(path + "/" + filelist[i]);
    					if (!delfile.isDirectory()) {
    						delfile.delete();
    					} else if (delfile.isDirectory()) {
    						deleteFilePath(path + "/" + filelist[i]);
    					}
    				}
    				
    				file.delete();
    			}
    		} catch (Exception e) {
    		}
    		
    		return true;  
    	}
        
    }
    


         就此以及介绍完util工具类的开发了。具体代码的意思我已经贴出注释了--其实都很基础啦,不懂的看一看Jdk文档就知道了!多动手,多尝试即可。

     

         启动项目。。。。没报错就可以了!

         下面就介绍一下前端首页的开发,先看一下效果图吧:

    ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-util介绍与开发首页_第1张图片

     

    ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-util介绍与开发首页_第2张图片

     

       然后,点击某一篇博客,查看详情:

    ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-util介绍与开发首页_第3张图片

     

       发表评论以及点击右边那些分类就不截图了。最后截一个关于lucene全文搜索实现的效果图:

    ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-util介绍与开发首页_第4张图片

        好了,总体上就介绍到这里吧!

        如果有相关问题:如想找我付费开发其他功能,讨论其中相关问题等等,可以来以下两群找我,我叫debug!

     

         Java开源技术交流:583522159     个人QQ:1948831260

    你可能感兴趣的:(第三方框架实战,ssh,ssm整合案例)