📄 operateutility.java
字号:
package com.huawei.icd30.agt.util;
import java.io.*;
import java.util.Map;
import java.util.Iterator;
//import com.huawei.icd30.bulletin.entity.ConfigInfoBean;
/**
* <p>本类封装业务操作的方法</p>
* @author ICD业务V3.0 B组-外包-黎新朝
* @version 1.0
*/
public class OperateUtility
{
/**
* 进行公告/便笺导出时根据ID和标题信息构造index.htm的内容
* @param info 以key为ID ,value为标题的信息
* @return 构造后的内容
*/
public static StringBuffer constructIndex(Map info)
{
StringBuffer indexBuffer = new StringBuffer() ;
indexBuffer.append("<html>\r\n<body>\r\n") ;
indexBuffer.append("<p>export information.</p>\r\n") ;
indexBuffer.append("<table width=50% align=left>\r\n") ;
if (info != null)
{
Iterator keys = info.keySet().iterator() ;
while(keys.hasNext())
{
String key = (String) keys.next() ;
String subject = (String) info.get(key) ;
indexBuffer.append("<tr><td><a href=\"./") ;
indexBuffer.append(key + File.separatorChar + key + ".htm\">") ;
indexBuffer.append(subject) ;
indexBuffer.append("</a></td></tr>\r\n") ;
}
}
indexBuffer.append("\r\n</table>\r\n</body>") ;
return indexBuffer ;
}
/**
* 删除文件f.如f为目录,则删除该目录,包括其下的所有子文件.
* @param f
* @return 如文件f还存在则返回false.
* 反之,如文件f不再存在则返回true.
*/
public static boolean delFile(File f)
{
boolean rflag = false ;
try
{
if(f == null || !f.exists())
{
return true ;
}
if(f.isDirectory())
{
File[] subfiles = f.listFiles() ;
if(subfiles != null)
{
for(int i=0 ;i<subfiles.length ;i++)
{
delFile(subfiles[i]) ;
}
}
}
rflag = f.delete() ;
}
catch(Exception e)
{
}
return rflag ;
}
/**
* 用dictionaryMap的名字映射替换content字符串
* 中<IMG ..>的图像域的名字,并在图像域中加入source="server"属性.
* 对<IMG .. source="server">的图像域不作替换.
* 被BulletinIssueServlet.java,BulletinEditServlet.java
* NoteSendServlet.java和TBNoteServlet.java调用。
* @param content 表示内容的字符串.
* @param dictionaryMap 替换的图片映射集.
* @return 替换后的字符串
*/
public static String convertImgName(String content ,Map dictionaryMap)
{
if(content == null || content.equals("")
|| dictionaryMap==null || dictionaryMap.size()==0)
{
return content ;
}
StringBuffer sb = new StringBuffer(content.length()) ;
int prePos = 0 ; // "<IMG ..>"前的位置
int sufPos = 0 ; // "<IMG ..>"后的位置
while(true)
{
if(content.indexOf("<IMG") != -1)
{
prePos = content.indexOf("<IMG") ;
sufPos = content.indexOf(">" ,prePos) ;
sb.append(content.substring(0 ,prePos)) ;
// 取<IMG ..>间的内容
String imgContent = content.substring(prePos ,sufPos+1) ;
content = content.substring(sufPos+1 ,content.length()) ;
// 不是来自服务器的图像域,需进行替换
if(imgContent.indexOf("source=\"server\"") ==-1 )
{
int preSrc = imgContent.indexOf("src=\"file:///") ;
int sufSrc = imgContent.lastIndexOf("\">") ;
String imgName = imgContent.substring(preSrc+13 ,sufSrc) ;
// 根据此图像名取得替换的新名
String newName = (String)dictionaryMap.get(imgName) ;
sb.append("<IMG src=\"") ;
sb.append(newName) ;
sb.append("\" source=\"server\">") ;
}
else
{
// 需除去ip:host信息,只保存相对路径.
sb.append("<IMG src=\"") ;
sb.append(imgContent.substring(imgContent.indexOf("/agt/testman/images"))) ;
}
}
else
{
// 没有需要处理的IMG域了.
sb.append(content) ;
break ;
}
}
return sb.toString() ;
}
/**
* 删除imgPath目录下所有不出现在content中的图片文件.
* 被BulletinEditServlet.java和TBNoteServlet.java调用。
* @param content 表示内容的字符串.
* @param imgPath 存放图片文件的父级目录。
* @return 无.
*/
public static void delInvalidImgFile(String content ,File imgPath)
{
if(imgPath == null) return ;
try
{
if(imgPath.isDirectory())
{
File[] subfiles = imgPath.listFiles() ;
if(subfiles != null)
{
for(int i=0 ;i<subfiles.length ;i++)
{
File sfile = subfiles[i] ;
String filefullName = sfile.getAbsolutePath() ;
filefullName = filefullName.replace('\\' ,'/') ;
// 获取从图片相对路径起的字符串
String relName = filefullName.substring(filefullName.indexOf("/agt/testman/images")) ;
// 若包含该图片,则内容中应包含以下字符串
String imgfield = relName + "\" source=\"server\">" ;
// 如内容中不出现该图片域
if(content.indexOf(imgfield) == -1)
{
sfile.delete() ;
}
}
}
}
}
catch(Exception e)
{
}
}
/**
* 在输入字符串中"'"前加入符号"\".
* 被BulletinEdit.jsp和NoteTempBoxEdit.jsp调用。
* @return 转换后的字符串
**/
public static String convertStr(String str)
{
if (str != null && str.indexOf('\'')!=-1)
{
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(str.getBytes());
byte temp;
while( (temp = (byte)bis.read()) != -1 )
{
if(temp == '\'')
{
bos.write('\\');
}
bos.write(temp);
}
return bos.toString();
}
return str; //不含有需转义的字符,则直接将原字符串返回
}
/**
* 把符号<>'"&转换为相应的HTML字符.
* @return 转换后的字符串.
*/
public static String htmlConvert(String str)
{
if(str != null)
{
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(str.getBytes());
byte temp;
byte[] ba1 = {'&','l','t',';'};
byte[] ba2 = {'&','g','t',';'};
byte[] ba3 = {'&' ,'q' ,'u' ,'o' ,'t' ,';'} ;
byte[] ba4 = {'&','a','m','p',';'};
try
{
while( (temp = (byte)bis.read()) != -1 )
switch (temp)
{
case '<' : bos.write(ba1); break ;
case '>' : bos.write(ba2); break ;
case '"' : bos.write(ba3); break ;
case '&' : bos.write(ba4); break ;
default : bos.write(temp); break ;
}
return bos.toString();
}
catch(Exception e)
{
e.printStackTrace(System.err);
return null ;
}
}
else
{
return null ;
}
}
/**
* 把回车换行字符去掉
* @return 转换后的字符串.
*/
public static String strDelEnterChar(String str)
{
if(str != null)
{
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(str.getBytes());
byte temp;
try
{
while( (temp = (byte)bis.read()) != -1 )
{
if(temp != 13 && temp != 10)
{
bos.write(temp);
}
}
return bos.toString();
}
catch(Exception e)
{
e.printStackTrace(System.err);
return null ;
}
}
else
{
return null ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -