期末项目 网上书店 需要用到 图片上传功能,研究了下,发现很多知识点和细节,下面一一列举
Jsp 代码:
1 2 3 4 5 6 7 8 9 10 11 12
| <form action="<%=basePath%>admin/book/add" method="post" enctype="multipart/form-data" > <input type="text" name="book_auth" placeholder="作者" /> <br /> <input type="text" name="book_publish" placeholder="出版社" /> <br /> <input type="text" name="book_ISBN" placeholder="ISBN" /> <br /> <input type="text" name="book_price" placeholder="价格" /><br /> <input type="file" name="uploadify" /> <br /> <input type="submit" value="添加书籍" /> </form>
|
注:这里 ENCTYPE=”multipart/form-data” 是设置数据以 2 进制的编码传输,这样任何文件都可以传了,不过这样存在个问题。(看后文)
Serlet 核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| String book_ISBN = ""; String book_name = ""; String book_auth = ""; String book_publish = ""; String book_price = ""; String booktype_id = ""; String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "bookface\\"; String firstFileName = ""; String fileRealPath = ""; File file = new File(savePath); if (!file.isDirectory()) { file.mkdirs(); } try { DiskFileItemFactory fac = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(fac); upload.setHeaderEncoding("UTF-8"); List fileList = upload.parseRequest(request); Iterator it = fileList.iterator(); while (it.hasNext()) { Object obit = it.next(); if (obit instanceof DiskFileItem) { DiskFileItem item = (DiskFileItem) obit; String temp = item.getFieldName(); if ("book_ISBN".equals(temp)) { book_ISBN = new String(item.getString("utf-8")); } if ("book_name".equals(temp)) { book_name = new String(item.getString("utf-8")); } if ("book_auth".equals(temp)) { book_auth = new String(item.getString("utf-8")); } if ("book_publish".equals(temp)) { book_publish = new String(item.getString("utf-8")); } if ("book_price".equals(temp)) { book_price = new String(item.getString("utf-8")); } if ("booktype_id".equals(temp)) { booktype_id = new String(item.getString("utf-8")); } String fileName = item.getName(); if (fileName != null) { firstFileName = item.getName().substring(item.getName().lastIndexOf("\\") + 1); String formatName = firstFileName.substring(firstFileName.lastIndexOf(".")); fileRealPath = savePath + book_ISBN + formatName; BufferedInputStream in = new BufferedInputStream(item.getInputStream()); BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath))); Streams.copy(in, outStream, true); if (new File(fileRealPath).exists()) { System.out.print("写入数据库"); } } } } } catch (org.apache.commons.fileupload.FileUploadException ex) { ex.printStackTrace(); System.out.println("没有上传文件"); return; }
|
- 1.这段代码就是上面所说的问题,form 里面的 input 的值以 2 进制的方式传过去,所以 request 就得不到值了,request.getParameter(“”); 得到的值永远是 null!就这个问题我调了一上午的时间,诶!
- 2.这段代码是假如当前 item 是文件,则就保存…java 的 IO 操作没什么说的
代码中用到的 DiskFileItemFactory 和 ServletFileUpload 这两个类是 commons-fileupload 这个包下面的,
这个包还需要 commons-io 这个包的支持 网上很容易找到。