Apache commons-fileupload 是一个很好的文件上传工具, 最近使用 commons-fileupload 实现了图 片的上传及显示,可将图片保存在指定的文件夹中,也可以将图片存放在数据库,并支持四种
常用的图片 格式:jpg,png,gif,bmp。 首先,跟上传一般文件一样,需要写一个 servlet 来处理上传的文件,你可以修改保存路径或选择将 图片保存在数据库中,只需要做简单的修改就行了,servlet 代码如下: FileUploadServlet.java java 代码 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. package com.ek.servlet; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apachemons.fileupload.FileItem; import org.apachemons.fileupload.FileUploadException; import org.apachemons.fileupload.disk.DiskFileItemFactory; import org.apachemons.fileupload.servlet.ServletFileUpload; import com.ek.image.ImageUtil; public class FileUploadServlet extends HttpServlet { /** * */
24. 25. 26. 27.
private static final long serialVersionUID = 1L; private static String filePath = ""; /** * Destruction of the servlet.
28. 29. 30. 31. 32. 33. 34.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doPost method of the servlet.
35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51.
* * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server
* @param response * the response send by the server to the client
* @throws ServletException * if an error occurred
* @throws IOException * */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html; charset=UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory(); if an error occurred
52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81.
// maximum size that will be stored in memory factory.setSizeThreshold(4096); // the location for saving data that is larger than getSizeThreshold() factory.setRepository(new File(filePath)); ServletFileUpload upload = new ServletFileUpload(factory); // maximum size before a FileUploadException will be thrown upload.setSizeMax(1000000); try { List fileItems = upload.parseRequest(req); Iterator iter = fileItems.iterator(); // Get the file name String regExp = ".+\\\\(.+\\.?())$"; Pattern fileNamePattern = Patternpile(regExp); while (iter.hasNext()) { File
Item item = (FileItem) iter.next(); if (!item.isFormField()) { String name = item.getName(); long size = item.getSize(); if ((name == null || name.equals("")) &;&; size == 0) continue; Matcher m = fileNamePattern.matcher(name); boolean result = m.find(); if (result) { try { // String type = // m.group(1).substring(m.group(1).lastIndexOf('.')+1); InputStream stream = item.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutp