登录后查才能浏览下载更多咨询,有问题联系QQ:3283999
您需要 登录 才可以下载或查看,没有账号?入住遨海湾
×
package zengxiaomeng;
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.swing.ImageIcon; import com.jspsmart.upload.*; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.*; import com.sun.image.codec.jpeg.*; public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; private final static long FILE_MAX_SIZE=4000000;//设置允许上传文件的大小限制 private final static String FILE_EXTEND_NAME="jpg,JPG,gif,GIF,png,PNG";//设置允许上传文件的扩展名 private final static String UPLOAD_DIRECTORY="/upload/";//设置上传的路径 private String path; //上传的绝对路径 private ServletConfig config; final public void init(ServletConfig config) throws ServletException { this.config = config; path=config.getServletContext().getRealPath(UPLOAD_DIRECTORY)+"/"; } final public ServletConfig getServletConfig() { return config; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //PrintWriter out = response.getWriter(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GBK"); //PrintWriter out = response.getWriter(); String filename=upload(config,request,response); watermark(filename);//生成水印 reduce(filename);//生成缩略图 response.sendRedirect("/Test.htm"); } public void destroy () { } //********************************附加的一些自定义方法********************************// //上传文件 private String upload(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { //初始化上传组件 SmartUpload mySmartUpload=new SmartUpload(); mySmartUpload.initialize(config,request,response); mySmartUpload.setAllowedFilesList(FILE_EXTEND_NAME); mySmartUpload.setMaxFileSize(FILE_MAX_SIZE); //上传文件 try{ mySmartUpload.upload(); //mySmartUpload.save(path);//注释掉此行将用原始文件名来保存 //以下三行代码利用Calendar对象生成一个随机毫秒数来作为新的文件名保存 com.jspsmart.upload.File myFile=mySmartUpload.getFiles().getFile(0);//返回刚刚上传的文件对象 String filename=String.valueOf(Calendar.getInstance().getTimeInMillis())+"."+myFile.getFileExt();//构造文件名 myFile.saveAs(path+filename,SmartUpload.SAVE_PHYSICAL);//保存文件为新的文件名 return filename; }catch(Exception e){} return "0"; } //生成缩略图 private void reduce(String filename){ //设置新文件的宽和高 int tagImageWidth=102; int tagImageHeight=120; ImageIcon srcFile=new ImageIcon(path+filename);//读入源文件 Image srcImage =srcFile.getImage();//构造Image对象 BufferedImage tagImage = new BufferedImage(tagImageWidth,tagImageHeight,BufferedImage.TYPE_INT_RGB);//构造图像缓存对象 tagImage.getGraphics().drawImage(srcImage,0,0,tagImageWidth,tagImageHeight,null);//绘制缩小后的图 paint(tagImage,"s_"+filename); } //生成水印 private void watermark(String filename){ Color color=Color.RED;//前景色 Color backgroundColor=Color.WHITE;//背景色 String word="zengxiaomeng";//水印文字 String wartmarkfilename="fjsenlogo.jpg";//水印图片地址 int watermarkLocationX=100; int watermarkLocationY=100; int wordLocationX=100; int wordLocationY=100; ImageIcon srcFile=new ImageIcon(path+filename);//读入源文件 Image srcImage =srcFile.getImage();//构造Image对象 ImageIcon watermarkFile=new ImageIcon(path+wartmarkfilename);//读入水印文件 Image watermarkImage =watermarkFile.getImage();//构造水印图片对象 BufferedImage tagImage=new BufferedImage(srcImage.getWidth(null),srcImage.getHeight(null), BufferedImage.TYPE_INT_RGB);//构造图像缓存对象 Graphics2D g=tagImage.createGraphics( ); g.setColor(color);//设置绘图笔的前景色 g.setBackground(backgroundColor);//设置绘图笔的背景色 g.drawImage(srcImage, 0, 0, null );//先绘制源图 g.drawImage(watermarkImage, watermarkLocationX,watermarkLocationY, null ); //添加水印图片 g.drawString(word,wordLocationX,wordLocationY); //添加文字 g.dispose(); paint(tagImage,filename); } private void paint(BufferedImage tagImage,String tagFilename){ try{ FileOutputStream tagFile=new FileOutputStream(path+tagFilename); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(tagFile); JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(tagImage); param.setQuality(50f, true); encoder.encode(tagImage, param); tagFile.close(); }catch(Exception e){} } } |