在weblogic8.1下运行通过
//mkYueLi.jsp
<%@ page language="java" import="javax.imageio.*,java.io.FileInputStream,java.io.FileOutputStream,java.io.File,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*" pageEncoding="GBK"%><%@ page import="javax.swing.text.StyledEditorKit.FontFamilyAction" %><%
System.out.print("/nhost:"+request.getRemoteHost()+"/naddr:"+request.getRemoteAddr()+"/nuser:"+request.getRemoteUser());
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String url=(String)request.getParameter("url");
String year=(String)request.getParameter("year");
String month=(String)request.getParameter("month");
String cellSpacing=(String)request.getParameter("cellSpacing");
String rowSpacing=(String)request.getParameter("rowSpacing");
String fontSize=(String)request.getParameter("fontSize");
String fontFamily=(String)request.getParameter("fontFamily");
String fontStyle=(String)request.getParameter("fontStyle");
String right=(String)request.getParameter("right");
String bottom=(String)request.getParameter("bottom");
String lunar=(String)request.getParameter("lunar");
if(url==null||url.equals("")){url="http://img04.21cn.com/2004/11/15/2912082.jpg";}
if(fontFamily==null||fontFamily.equals("")){fontFamily="Courier New";}
fontFamily=new String(fontFamily.getBytes("ISO-8859-1"),"GBK");
if(year==null) year="2006";
if(month==null) month="6";
if(fontSize==null) fontSize="15";
if(cellSpacing==null) cellSpacing="20";
if(rowSpacing==null) rowSpacing="0";
if(right==null) right="0";
if(bottom==null) bottom="0";
if(fontStyle==null) fontStyle="0";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>
Wallpaper Maker
</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style>
body{font-size:13px;}
input{border-style:none;border-bottom-style:solid;border-bottom-width:1px;}
</style>
</head>
<body>
<form action="" method="post">
<table border="1" style="border-style:solid;border-collapse:collapse"><tr><td>
year:<input type="text" value="<%=year%>" size="5" name="year">
month:<select name="month">
<%
for(int i=1;i<=12;i++){
out.println("<option value=/""+i+"/""+((month.equals(i+""))?" selected":"")+">"+i+"</option>");
}
%>
</select>
<input type="checkbox" name="lunar" id="lunar" value="lunar" <%=lunar!=null?"checked":""%> style="border:none"/><label for="lunar">show lunarCalendar</label>
</td></tr>
<tr><td>
right:<input type="text" name="right" value="<%=right%>" size="3">px
bottom:<input type="text" name="bottom" value="<%=bottom%>" size="3">px
cell-spacing:<input type="text" name="cellSpacing" size="3" value="<%=cellSpacing%>">px
row-spacing:<input type="text" name="rowSpacing" size="3" value="<%=rowSpacing%>">px
</td></tr>
<tr><td>
font-family:<SELECT name="fontFamily">
<%
GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = eq.getAvailableFontFamilyNames();
for(int i=0;i<fontNames.length;i++){
out.println("<option value=/""+fontNames[i]+"/""+(fontFamily.equals(fontNames[i])?" selected":"")+">"+fontNames[i]+"</option>");
}
%>
</SELECT>
font-size:<input name="fontSize" value="<%=fontSize%>" size="3">
font-style:<select name="fontStyle">
<option value="0" <%=fontStyle.equals("0")?"selected":""%>>Plain</option>
<option value="1" <%=fontStyle.equals("1")?"selected":""%>>Bold</option>
<option value="2" <%=fontStyle.equals("2")?"selected":""%>>Italic</option>
<option value="3" <%=fontStyle.equals("3")?"selected":""%>>Bold+Italic</option>
</select>
</td></tr>
<tr><td>
picture's URL:<input type="text" name="url" size="50" value="<%=url%>">
<input type="submit" value="Build" style="width:100;border-style:solid;border-width:1px;">
</td></tr>
</table>
</form>
<IMG src="makePic.jsp?url=<%=url%>&year=<%=year%>&month=<%=month%>&cellSpacing=<%=cellSpacing%>&rowSpacing=<%=rowSpacing%>&fontFamily=<%=fontFamily%>&fontSize=<%=fontSize%>&right=<%=right%>&bottom=<%=bottom%>&fontStyle=<%=fontStyle%>"/>
</body>
</html>
//mkPic.jsp
<%@page autoFlush="true" contentType="image/jpeg" pageEncoding="GBK" import="YL.lunarCalendar,java.io.InputStream,java.io.FileOutputStream,java.io.File,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%><%@ page import="javax.servlet.http.HttpServletResponse" %><%!
int[] getMaxDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//cal.setTime(new Date());
cal.set(year, month - 1, 1);
cal.setFirstDayOfWeek(Calendar.SUNDAY);
int len = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//System.out.println(len+","+year+","+month);
int[] arr = new int[42];
//System.out.println(cal.get(Calendar.DAY_OF_WEEK));
for (int i = cal.get(Calendar.DAY_OF_WEEK) - 1, j = 1; j <= len; i++, j++) {
arr[i] = j;
}
return arr;
}
void drawErrorPic(int fontSize,String fontFamily,int fontStyle,String errStr,HttpServletResponse response){
BufferedImage image = new BufferedImage(fontSize*20,fontSize*2,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(new Font(fontFamily,fontStyle,fontSize));
g.drawString(errStr,0,fontSize);
g.dispose();
try{
javax.imageio.ImageIO.write(image, "JPEG", response.getOutputStream());
}catch(Exception e){}
}
String toChinese(int[] d){
String chn[]={"十","一","二","三","四","五","六","七","八","九"};
String chnMonth[]={"一","二","三","四","五","六","七","八","九","十","十一","十二"};
String resultStr="";
if(d[1]==1){
if(d[0]>12)
resultStr="闰"+chnMonth[d[0]-14]+"月";
else if(d[0]==1){
resultStr="正月";
}else{
resultStr=chnMonth[d[0]-1]+"月";
}
}else{
resultStr=(d[1]<=10?"初":(d[1]/10==1?"十":(d[1]/10==2&&d[1]%10!=0?"廿":chn[d[1]/10])))+chn[d[1]%10];
}
return resultStr;
}
%><%
response.addHeader("pragma", "NO-cache");
response.addHeader("Cache-Control", "no-cache");
response.addDateHeader("Expries", 0);
InputStream sFile=null;
BufferedImage src=null;
String picURL=(String)request.getParameter("url");
int year=Integer.parseInt(request.getParameter("year")+"");
int month=Integer.parseInt(request.getParameter("month")+"");
int cellSpacing=Integer.parseInt(request.getParameter("cellSpacing")+"");
int rowSpacing=Integer.parseInt(request.getParameter("rowSpacing")+"");
String fontFamily=request.getParameter("fontFamily")+"";
String strFontSize=request.getParameter("fontSize")+"";
int fontStyle=Integer.parseInt(request.getParameter("fontStyle")+"");
int bottom=Integer.parseInt((String)request.getParameter("bottom"));
int right=Integer.parseInt((String)request.getParameter("right"));
if(strFontSize==null) strFontSize="15";
int fontSize=Integer.parseInt(strFontSize);
if(picURL==null||picURL.equals("")) picURL="http://img04.21cn.com/2004/11/15/2912082.jpg";
if(fontFamily==null||fontFamily.equals("")) fontFamily="Courier New";
fontFamily=new String(fontFamily.getBytes("ISO-8859-1"),"GBK");
java.net.HttpURLConnection conn=null;
try{
java.net.URL url = new java.net.URL(picURL);
conn = (java.net.HttpURLConnection) url.openConnection();
conn.connect();
sFile = conn.getInputStream();
}catch(java.io.FileNotFoundException e){
drawErrorPic(fontSize,fontFamily,fontStyle,"File is not found in the path!",response);
sFile.close();
return;
}catch(java.net.UnknownHostException e){
drawErrorPic(fontSize,fontFamily,fontStyle,"Unknown host name!",response);
return;
}catch(java.net.MalformedURLException e){
drawErrorPic(fontSize,fontFamily,fontStyle,"Illegal protocol!",response);
return;
}
//设定源图片路径
//String FileName = request.getRealPath("/")+"//aa.jpg";
//设定目标图片路径
//String OutFileName = request.getRealPath("/")+"//aa_new.jpg";
//创建一个FileInputStream对象从源图片获取数据流
//FileInputStream sFile = new FileInputStream(FileName);
//创建一个Image对象并以源图片数据流填充
//Image
src = javax.imageio.ImageIO.read(sFile);
conn.disconnect();
//关闭输入文件流
sFile.close();
if(src==null){
drawErrorPic(fontSize,fontFamily,fontStyle,"The URL is not a path for picture!",response);
return;
}
//得到源图宽
int width = src.getWidth(null);
//得到源图长
int height = src.getHeight(null);
//创建一个BufferedImage来作为图像操作容器
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//创建一个绘图环境来进行绘制图像
Graphics g = image.getGraphics();
//将原图像数据流载入这个BufferedImage
g.drawImage(src,0,0,width,height,null);
//星期数组
String[] dayArr={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
//设定文本大小
int fontPX=fontSize;
int xBase=width-(fontPX+cellSpacing)*7-right;
//设定文本字体
g.setFont(new Font(fontFamily,fontStyle,fontPX));
//设定文本颜色
g.setColor(Color.WHITE);
//写入星期表头
for(int i=0;i<7;i++){
int x=xBase+(fontPX+cellSpacing)*(i%7);
int y=height-bottom-(fontPX+rowSpacing)*13;
//向BufferedImage写入文本字符
g.drawString(dayArr[i],x,y);
}
//获取指定年月日期数组
int[][] dateOfMonth=lunarCalendar.lunarMonth(year,month);//getMaxDayOfMonth(year,month);
int len=dateOfMonth[0].length;
for(int i=0;i<len;i++){
if(dateOfMonth[0][i]==0) continue;
int x=xBase+(fontPX+cellSpacing)*(i%7);
int y=height-bottom-(fontPX+rowSpacing)*2*(6-i/7);
int lunarY=height-bottom-(fontPX+rowSpacing)*(6-i/7)*2+fontPX*2/3;
g.setColor(Color.WHITE);
g.setFont(new Font(fontFamily,fontStyle,fontPX));
//向BufferedImage写入文本字符
g.drawString((dateOfMonth[0][i]<10?" ":"")+dateOfMonth[0][i],x,y);
g.setColor(Color.BLUE);
g.setFont(new Font(fontFamily,fontStyle,fontPX*2/3));
String drawStr=toChinese(new int[]{dateOfMonth[1][i],dateOfMonth[2][i]});
g.drawString(drawStr,x,lunarY);
}
//使更改生效
g.dispose();
// 输出图象到页面
javax.imageio.ImageIO.write(image, "JPEG", response.getOutputStream());
%>