jsp 上传图片并生成缩位图或者加水印--zt

来源:互联网 发布:防噪音 知乎 编辑:程序博客网 时间:2024/05/16 02:26
有些网站  动网, 上传图片后加给加上自己的字(是在图片上加的) 请问在JSP里如何实现??//添加水印,filePath 源图片路径, watermark 水印图片路径public static boolean createMark(String filePath,String watermark) {ImageIcon imgIcon=new ImageIcon(filePath);Image theImg =imgIcon.getImage();ImageIcon waterIcon=new ImageIcon(watermark);Image waterImg =waterIcon.getImage();int width=theImg.getWidth(null);int height= theImg.getHeight(null);BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);Graphics2D g=bimage.creatGraphics( );g.setColor(Color.red);g.setBackground(Color.white);g.drawImage(theImg, 0, 0, null );g.drawImage(waterImg, 100, 100, null );g.drawString("12233",10,10); //添加文字g.dispose();try{FileOutputStream out=new FileOutputStream(filePath);JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);param.setQuality(50f, true);encoder.encode(bimage, param);out.close();}catch(Exception e){ return false; }return true;}/////////////////范例////////////////////package package;import java.io.*;import javax.servlet.ServletException;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;public class upload{private static String newline = "/n";private String uploadDirectory;private String ContentType;private String CharacterEncoding;public upload(){uploadDirectory = ".";ContentType = "";CharacterEncoding = "";}private String getFileName(String s){int i = s.lastIndexOf("//");if(i < 0 || i >= s.length() - 1){i = s.lastIndexOf("/");if(i < 0 || i >= s.length() - 1)return s;}return s.substring(i + 1);}public void setUploadDirectory(String s){uploadDirectory = s;}public void setContentType(String s){ContentType = s;int i;if((i = ContentType.indexOf("boundary=")) != -1){ContentType = ContentType.substring(i + 9);ContentType = "--" + ContentType;}}public void setCharacterEncoding(String s){CharacterEncoding = s;}public String uploadFile(HttpServletRequest httpservletrequest)throws ServletException, IOException{String s = null;setCharacterEncoding(httpservletrequest.getCharacterEncoding());setContentType(httpservletrequest.getContentType());s = uploadFile(httpservletrequest.getInputStream());return s;}public String uploadFile(ServletInputStream servletinputstream)throws ServletException, IOException{String s = null;String s1 = null;byte abyte0[] = new byte[4096];byte abyte1[] = new byte[4096];int ai[] = new int[1];int ai1[] = new int[1];String s2;while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null){int i = s2.indexOf("filename=");if(i >= 0){s2 = s2.substring(i + 10);if((i = s2.indexOf("/"")) > 0)s2 = s2.substring(0, i);break;}}s1 = s2;if(s1 != null && !s1.equals("/"")){s1 = getFileName(s1);String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);if(s3.indexOf("Content-Type") >= 0)readLine(abyte0, ai, servletinputstream, CharacterEncoding);File file = new File(uploadDirectory, s1);FileOutputStream fileoutputstream = new FileOutputStream(file);while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null){if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)break;if(s != null){fileoutputstream.write(abyte1, 0, ai1[0]);fileoutputstream.flush();}s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)break;fileoutputstream.write(abyte0, 0, ai[0]);fileoutputstream.flush();}byte byte0;if(newline.length() == 1)byte0 = 2;elsebyte0 = 1;if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);fileoutputstream.close();}return s1;}private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s){ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);if(ai[0] == -1)return null;break MISSING_BLOCK_LABEL_27;Object obj;obj;return null;if(s == null)return new String(abyte0, 0, ai[0]);return new String(abyte0, 0, ai[0], s);obj;return null;}}JSP页:<%@page contentType="text/html;charset=gb2312" import="package.upload"%><%String Dir = "c:/dir/upload";String fn="";upload upload = new upload();upload.setUploadDirectory(Dir);fn=upload.uploadFile(request);%>随机图片名称<%mySmartUpload.initialize(pageContext);mySmartUpload.service(request,response);mySmartUpload.upload();String fn=mySmartUpload.getFiles().getFile(0).getFileName();mySmartUpload.save("upload/"); //文件保存的目录为uploadout.println("已经成功上传了文件,请查看<a href=upload/"+fn+">这里</a>");%>上面的程序可以上传图片,不过只能上传gif或者JPG图片。而且保存图片在upload文件夹下面,要想GIF或Jpg图片的名称变为年+月+日+随机数.gif或年+月+日+随机数.jpg只允许上传jpg或gif图片,在客户端用javaScript控制要好些。变图片名称可用如下代码:自己看看就明白了。://得到实际路径String realPath = this.masRequest.getRequest().getRealPath("/");String userPhotoPath = realPath + "images//UserPhoto//";userPhotoPath = MasString.replace(userPhotoPath,"//","////");if (!file.getFileName().trim().equals("")){//根据系统时间生成文件名Date nowTime = new Date();emp_Photo = userPhotoPath + String.valueOf(nowTime.getTime()) +"."+ file.getFileExt();file.saveAs(emp_Photo);System.out.println("file.saveAs() = " + "OK!!!");
原创粉丝点击