Posted on 2006-09-29 11:14
毛里求斯的化石 阅读(1224)
评论(0) 编辑 收藏 所属分类:
java&js相关
我是结合在jspsmart.upload下使用的,这个是imgbean的代码
package com.dcampus;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import org.w3c.dom.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.*;
public class ImgBean_i{
public void ImgBean_i(){
}
public void ImgYin(String Imagename_biao,String ImgName){
try{
File _file = new File(ImgName);
Image src = ImageIO.read(_file);
int wideth=src.getWidth(null);
int height=src.getHeight(null);
BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.createGraphics();
g.drawImage(src,0,0,wideth,height,null);
//水印文件
File _filebiao = new File(Imagename_biao);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao=src_biao.getWidth(null);
int height_biao=src_biao.getHeight(null);
g.drawImage(src_biao,wideth-110,height-110,wideth_biao,height_biao,null);
//水印文件在原图片文件的位置,原图片文件的右下角为wideth-0,height-0
g.dispose();
FileOutputStream out=new FileOutputStream(ImgName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
以下是修改过的smartupload代码
<%@ page language="java" import="com.jspsmart.upload.*" import="java.util.*,java.text.*"%>
<%@ page contentType="text/html;charset=GBK" %>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<HTML><META http-equiv=Content-Type content="text/html; charset=gb2312">
<LINK href="images/css.css" rel=stylesheet>
<BODY BGCOLOR="white" leftmargin="0" topmargin="0">
<%
int count=0;
String path=request.getRealPath("");
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
connectToDatabase();
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
int Size=myFile.getSize() ;
String FileName=myFile.getFileName();
String FileExt=myFile.getFileExt();
String uploadtime=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( java.util.Calendar.getInstance().getTime() );
if(Size>=1000000) {
out.println("文件超过1000K!"); //限制大小
}else{
if((FileExt.equals("jpg")) || (FileExt.equals("JPG")) || (FileExt.equals("gif")) || (FileExt.equals("GIF"))) {
java.util.Date date = new java.util.Date();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
String name1=FileName.substring(0,FileName.indexOf("."));
String name2=FileName.substring(FileName.indexOf("."),FileName.length());
String newname=name1+formatter1.format(date)+name2;
//session.putValue("path1",newname);
myFile.saveAs("/photo/upfile/" + newname);
if(Integer.parseInt(sy)==1) {
com.pic.ImgBean_i bbb= new com.pic.ImgBean_i();
bbb.ImgYin(path+"/photo/images/shuiyin.gif",path+"/photo/upfile/" + newname);//这里是水印文件的来源地址,可修改成取得下拉菜单值的形式,这样就可以实现打文字水印或者图片水印了
}
stmt.executeUpdate("insert into img_photo (name,http,userid,classid,title,content,imgsize,format,num,primflag,posttime) values ()");
out.println(("<br><br><center>图片上传成功!<br>").toString());
}else{
out.println("文件格式不对!");
}
}
count ++;
}
else
{
out.println("未找到上传文件!\n");
}
}
disConnectToDatabase();
%>
</BODY>
</HTML>