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

  1. //添加水印,filePath 源图片路径, watermark 水印图片路径 
  2. public static boolean createMark(String filePath,String watermark) { 
  3. ImageIcon imgIcon=new ImageIcon(filePath); 
  4. Image theImg =imgIcon.getImage(); 
  5. ImageIcon waterIcon=new ImageIcon(watermark); 
  6. Image waterImg =waterIcon.getImage(); 
  7. int width=theImg.getWidth(null); 
  8. int height= theImg.getHeight(null); 
  9. BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); 
  10. Graphics2D g=bimage.createGraphics( ); 
  11. g.setColor(Color.red); 
  12. g.setBackground(Color.white); 
  13. g.drawImage(theImg, 00null ); 
  14. g.drawImage(waterImg, 100100null ); 
  15. g.drawString("12233",10,10); //添加文字 
  16. g.dispose(); 
  17. try
  18. FileOutputStream out=new FileOutputStream(filePath); 
  19. JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); 
  20. JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); 
  21. param.setQuality(50f, true); 
  22. encoder.encode(bimage, param); 
  23. out.close(); 
  24. }catch(Exception e){ return false; } 
  25. return true

 

  1. /范例 
  2. package package
  3. import java.io.*; 
  4. import javax.servlet.ServletException; 
  5. import javax.servlet.ServletInputStream; 
  6. import javax.servlet.http.HttpServletRequest; 
  7. public class upload 
  8. private static String newline = "/n"
  9. private String uploadDirectory; 
  10. private String ContentType; 
  11. private String CharacterEncoding; 
  12. public upload() 
  13. uploadDirectory = "."
  14. ContentType = ""
  15. CharacterEncoding = ""
  16. private String getFileName(String s) 
  17. int i = s.lastIndexOf("//"); 
  18. if(i < 0 || i >= s.length() - 1
  19. i = s.lastIndexOf("/"); 
  20. if(i < 0 || i >= s.length() - 1
  21. return s; 
  22. return s.substring(i + 1); 
  23. public void setUploadDirectory(String s) 
  24. uploadDirectory = s; 
  25. public void setContentType(String s) 
  26. ContentType = s; 
  27. int i; 
  28. if((i = ContentType.indexOf("boundary=")) != -1
  29. ContentType = ContentType.substring(i + 9); 
  30. ContentType = "--" + ContentType; 
  31. public void setCharacterEncoding(String s) 
  32. CharacterEncoding = s; 
  33. public String uploadFile(HttpServletRequest httpservletrequest) 
  34. throws ServletException, IOException 
  35. String s = null
  36. setCharacterEncoding(httpservletrequest.getCharacterEncoding()); 
  37. setContentType(httpservletrequest.getContentType()); 
  38. s = uploadFile(httpservletrequest.getInputStream()); 
  39. return s; 
  40. public String uploadFile(ServletInputStream servletinputstream) 
  41. throws ServletException, IOException 
  42. String s = null
  43. String s1 = null
  44. byte abyte0[] = new byte[4096]; 
  45. byte abyte1[] = new byte[4096]; 
  46. int ai[] = new int[1]; 
  47. int ai1[] = new int[1]; 
  48. String s2; 
  49. while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null
  50. int i = s2.indexOf("filename="); 
  51. if(i >= 0
  52. s2 = s2.substring(i + 10); 
  53. if((i = s2.indexOf("/"")) > 0
  54. s2 = s2.substring(0, i); 
  55. break
  56. s1 = s2; 
  57. if(s1 != null && !s1.equals("/"")) 
  58. s1 = getFileName(s1); 
  59. String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding); 
  60. if(s3.indexOf("Content-Type") >= 0
  61. readLine(abyte0, ai, servletinputstream, CharacterEncoding); 
  62. File file = new File(uploadDirectory, s1); 
  63. FileOutputStream fileoutputstream = new FileOutputStream(file); 
  64. while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null
  65. if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45
  66. break
  67. if(s != null
  68. fileoutputstream.write(abyte1, 0, ai1[0]); 
  69. fileoutputstream.flush(); 
  70. s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding); 
  71. if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45
  72. break
  73. fileoutputstream.write(abyte0, 0, ai[0]); 
  74. fileoutputstream.flush(); 
  75. byte byte0; 
  76. if(newline.length() == 1
  77. byte0 = 2
  78. else 
  79. byte0 = 1
  80. if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0) 
  81. fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0); 
  82. if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0) 
  83. fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0); 
  84. fileoutputstream.close(); 
  85. return s1; 
  86. private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s) 
  87. ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length); 
  88. if(ai[0] == -1
  89. return null
  90. break MISSING_BLOCK_LABEL_27; 
  91. Object obj; 
  92. obj; 
  93. return null
  94. if(s == null
  95. return new String(abyte0, 0, ai[0]); 
  96. return new String(abyte0, 0, ai[0], s); 
  97. obj; 
  98. return null

JSP页:

  1. <%@page contentType="text/html;charset=gb2312" import="package.upload"%> 
  2. <
  3. String Dir = "c:/dir/upload"
  4. String fn=""
  5. upload upload = new upload(); 
  6. upload.setUploadDirectory(Dir); 
  7. fn=upload.uploadFile(request); 
  8. %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值