Stringpath=list.get(i).toString();
if(path.indexOf(".")!
=-1){
array.add(path);
continue;
}
Filefile=newFile(path);
File[]f=file.listFiles();
if(f==null){
array.add(path);
continue;
}
if(f.length==0){
array.add(file.getPath());
}
for(Filev:
f){
bool=true;
array.add(v.getPath());
}
}
if(bool)
{
returngetRemove(array);
}
returnarray;
}
3dom4J生成一个XML文件
Documentdocument=DocumentHelper.createDocument();
document.setXMLEncoding("UTF-8");
Elementroot=document.addElement("root");//创建根节点
Elementdata=root.addElement("data");
//产品任务单信息
ElementcatalogID=data.addElement("catalogID");
catalogID.addText(outPutProduct.getFdCatalogid()==null?
"":
outPutProduct.getFdCatalogid());//台风
BufferedWriterbw=null;
try{
Stringstr1=document.asXML();
Filefile=newFile(path+"\\"+guid+"-meta"+".xml");
bw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file)));
bw.write(str1);
outPutProductXml.doWebService();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
bw.flush();
bw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
4图片复制效果
FilefileImgPath=newFile(outJpgPath);
FiletoFile=newFile(xmlpath);
if(!
toFile.exists())toFile.mkdirs();
FileChannelsrcChannel=newFileInputStream(fileImgPath.getPath()).getChannel();
FileChanneldstChannel=newFileOutputStream(toFile.getPath()+"\\"+fileImgPath.getName()).getChannel();dstChannel.transferFrom(srcChannel,0,srcChannel.size());
srcChannel.close();
dstChannel.close();
5图片叠加效果
/**
*图片叠加,但不改变大小
*@param大图片
*@param小图片
*/
publicvoidImageOverlying(StringimagePathA,StringimagePathB,Stringoutpath){
try{
FilefileImageA=newFile(imagePathA);
ImageimageA=ImageIO.read(fileImageA);
//图片A大小
intwidthA=imageA.getWidth(null);
intheightA=imageA.getHeight(null);
BufferedImagebufImage=newBufferedImage(widthA,heightA,BufferedImage.TYPE_INT_RGB);
Graphicsg=bufImage.createGraphics();
g.drawImage(imageA,0,0,widthA,heightA,null);
FilefileImageB=newFile(imagePathB);
ImageimageB=ImageIO.read(fileImageB);
//图片B大小
intwidthB=imageB.getWidth(null);
intheightB=imageB.getHeight(null);
/*
*图片B在图片A的位置
*左下角:
0,heightA-heightB
*左上角:
0,0
*右上:
widthA-widthB,0
*右下:
widthA-widthB,heightA-heightB
*/
g.drawImage(imageB,0,heightA-heightB,widthB,heightB,null);
g.dispose();
FileOutputStreamfos=newFileOutputStream(outpath);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(bufImage);
//图片质量设置
param.setQuality(1.0F,true);
encoder.encode(bufImage,param);
fos.flush();
fos.close();
fos=null;
}catch(Exceptione){
e.printStackTrace();
}
}
6图片添加边框
/**
*功能说明:
给图片加边框
*@paramargs
*/
publicvoidImgBox(StringimgPath,intnewwidth,intnewheight){
BufferedImageimage=newBufferedImage(newwidth,newheight,
BufferedImage.TYPE_INT_RGB);
Graphicsgraphics=image.getGraphics();
ImageIconim=newImageIcon(imgPath);
Imageimg=im.getImage();
graphics.drawImage(img,0,0,newwidth,newheight,null);//绘制缩小后的图
//画边框,在drawImage后面,下面代码给图片加上两个像素的白边
graphics.setColor(Color.LIGHT_GRAY);
graphics.drawRect(0,0,newwidth-1,newheight-1);
graphics.drawRect(1,1,newwidth-1,newheight-1);
graphics.drawRect(0,0,newwidth-2,newheight-2);
FileOutputStreamout;
try{
out=newFileOutputStream(imgPath);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);//JPEG编码
out.close();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}//输出到文件流
}
7程序运行时报错内存溢出解决方案
报错:
java.lang.OutOfMemoryError:
PermGenspace
解决方案:
-Xms800m-Xmx800m-XX:
PermSize=128M-XX:
MaxNewSize=256m-XX:
MaxPermSize=256m
8JAVA迭代器的使用
for(Iteratorites=elementBody.iterator();ites.hasNext();){
Elementelementeses=(Element)ites.next();
}
9编码格式问题
//正确的格式
OutputStreamWriteroutput=newOutputStreamWriter(newFileOutputStream(file),"gbk");
output.write(str1);
output.flush();
output.close();
//错误的格式
bw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file)));
bw.write(str1);
10获取服务器端路径
req.getScheme()//协议名称http
req.getServerPort()//所使用的访问端口8080
request.getContextPath()//获取项目名称
req.getScheme()+":
//"+req.getServerName()+":
"+req.getServerPort()+req.getServletPath()
11在一个集合里面过滤已知的文件类型
String[]stres=newString[]{".dbf",".prj",".sbn",".sbx",".shp.xml",".shx",".aux",".rrd",".tif.xml"};
intnum;
for(num=0;numfor(inti=0;iif(list.get(num).toString().indexOf(stres[i])!
=-1){
list.remove(list.get(num));
num=0;
}
}
}
12java正则表达式嵌入写法
importjava.util.regex.*;
publicStringgetLine(Stringline){
//表达式对象
Patternp=Ppile("^.+//$");
//创建Matcher对象
Matcherm=p.matcher(line);
returnm.find()?
m.group():
null;//当找到以//结尾的行返回这一行否则返回空
}
13File类的FileList方法
Stringvalue=”C:
\\SSS\\BBB\CCC”;
Filefile=newFile(value);
StringaskName=File.getName();
askName=ccc;
14直接访问HTTPSERVLET的集成类
在web里面应该做如下的配置即可直接访问http//localhost:
8080/RLRS/SERVLET
SwfFileUploadServlet
com.hjkj.action.permissions.SwfFileUploadServlet
SwfFileUploadServlet
/swfupload
15MD5的加密方式代码
publicStringgetMD5(StringstrValue){
try{
Strings;
charhexDigits[]={
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
byte[]b=strValue.getBytes();
java.security.MessageDigestmd=java.security.MessageDigest.getInstance("MD5");
md.update(b);
bytetmp[]=md.digest();
charstr[]=newchar[16*2];
intk=0;
for(inti=0;i<16;i++){
bytebyte0=tmp[i];
str[k++]=hexDigits[byte0>>>4&0xf];
str[k++]=hexDigits[byte0&0xf];
}
s=newString(str);
returns;
}catch(NoSuchAlgorithmExceptione){
e.printStackTrace();
}
returnstrValue;
}
16Fileter过滤类用法
publicclassMyCASFilterextendsFileter{
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfc)throwsIOException,ServletException{
StringrequestURI=((HttpServletRequest)request).getRequestURI();
StringcontextPath=((HttpServletRequest)request).getContextPath();
///System.out.println(requestURI.toString());
if(requestURI.indexOf("reportServlet")!
=-1){
fc.doFilter(request,response);
return;
//过滤掉台风模块前台的访问需要过滤的问题
}elseif(requestURI.indexOf("TfInfoAction")!
=-1){
fc.doFilter(request,response);
return;
}elseif(requestURI.indexOf("InterpoServlet")!
=-1){
fc.doFilter(request,response);
return;
}
try{
super.doFilter(request,response,fc);
}catch(ServletExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
17jdk配置信息
Path C:
\ProgramFiles\Java\jdk1.6.0_11\bin;
class_Path C:
\ProgramFiles\Java\jdk1.6.0_11\lib
java_Home C:
\ProgramFiles\Java\jdk1.6.0_11
18【ServeltContext】在servlet中获得此对象的方法
HttpSessionsession=req.getSession();
ServletContextsc=session.getServletContext();
HttpServletRequestrequest=ServletActionContext.getRequest();
19打开一个网页的方法
StringstrURL="http:
//localhost:
8080/geoserver/wms?
service=WMS&version=1.1.0&request=GetMap&layers=topp:
tasmania_water_bodies&styles=&bbox=145.97161899999998,-43.031944,147.219696,-41.775558&width=508&height=512&srs=EPSG:
4326&format=application/openlayers";
Runtime.getRuntime().exec("cmd/cstart"+strURL);
20读取一个网页的内容
resp.setContentType("text/html;charset=GB2312");
java.io.PrintWriterout=resp.getWriter();
StringstrURL="http:
//localhost:
8080/geoserver/wms?
service=WMS&version=1.1.0&request=GetMap&layers=topp:
tasmania_water_bodies&styles=&bbox=145.97161899999998,-43.031944,147.219696,-41.775558&width=508&height=512&srs=EPSG:
4326&format=application/openlayers";
URLurl=newURL(strURL);
InputStreamReaderisr=newInputStreamReader(url.openStream());
BufferedReaderbr=newBufferedReader(isr);
Stringstr;
while((str=br.readLine())!
=null)
{
out.println(str);
}
br.close();
isr.close();
21重定向和转发及创建session【onextendsHttpServlet】
ActionContextctx=ActionContext.getContext();
HttpServletRequestrequest=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponseresponse=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
resp.sendRedirect("http:
//localhost:
8080/geoserver/view.html");//重定向
req.getRequestDispatcher("http:
//localhost:
8080/geoserver/view.jsp?
name="+namezh).forward(req,resp);
22File类
fileObj.Exists();//判断一个文件是否存在存在true不存在false
fileObj.createNewFile();