อัปโหลดการดาวน์โหลดและการจัดการเทคโนโลยีของไฟล์ที่ใช้งานได้: 1. ใช้ XML เป็นข้อมูลการจัดเก็บฐานข้อมูล (DOM4J, XPath)
2. อัปโหลดและดาวน์โหลดแบบฟอร์ม Java
3. การทำลายไดเรกทอรีไฟล์ (ไดเรกทอรีแฮชเป็นวิธีการเพิ่มประสิทธิภาพการจัดเก็บไฟล์ให้เหมาะสมที่สุด)
ต้องการแพ็คเกจ Jar:
Commons-Fileupload-1.2.2.jar, Commons-io-2.1.jar, Dom4j-1.6.1.jar และ Jaxen-1.1-beta-6.jar
-
เขียน index.jsp ก่อน
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en"> <html> <head> <head> href = "jsps/upload.jsp"> อัปโหลดอัลบั้ม </a> <a href = "servlets/showservlet"> อัลบั้มเรียกดู </a> </body> </html>
upload.jsp เป็นหน้าดาวน์โหลดที่วางไว้ในไดเรกทอรี JSPS
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <! doctype html public "-// w3c // dtd html 4.01 transitional // en"> <html> action = "<%= request.getContextPath ()%>/servlets/uploadservlet" method = "post" enctype = "multipart/form-data"> ไฟล์: <อินพุต type = "file" name = "file1"/> <br/> คำอธิบาย: </body> </html>
ใส่ photos.xml ในไดเร็กทอรี src
<? xml version = "1.0" encoding = "utf-8"?> <photos> </potos>
เขียนค่าวัตถุค่าโฟโตโมเดล
แพ็คเกจ cn.hncu.demain; public class photomodel {รหัสสตริงส่วนตัว; realname สตริงส่วนตัว; สตริงส่วนตัว ext; สตริงส่วนตัว Dir; สตริงส่วนตัว DateTime; สตริงส่วนตัว IP; สตริงส่วนตัว desc; สตริงสาธารณะ getId () {return id; } โมฆะสาธารณะ setId (รหัสสตริง) {this.id = id; } สตริงสาธารณะ getRealName () {return realName; } โมฆะสาธารณะ setRealName (String RealName) {this.realName = realName; } สตริงสาธารณะ getExt () {return ext; } โมฆะสาธารณะ setext (String ext) {this.ext = ext; } สตริงสาธารณะ getDir () {return dir; } โมฆะสาธารณะ setDir (สตริง dir) {this.dir = dir; } สตริงสาธารณะ getDateTime () {ส่งคืนวันที่; } โมฆะสาธารณะ setDateTime (String DateTime) {this.dateTime = DateTime; } สตริงสาธารณะ getip () {return ip; } โมฆะสาธารณะ setip (string ip) {this.ip = ip; } สตริงสาธารณะ getDesc () {return desc; } โมฆะสาธารณะ setDesc (String desc) {this.desc = desc; } @Override สตริงสาธารณะ toString () {return "photomodel [id =" + id + ", realName =" + realName + ", ext =" + ext + ", dir =" + dir + ", datetime =" + datetime + ", ip =" ip + ", desc =" + " - เครื่องมือการเขียนมีสองประเภท:
MyUtil (การจัดรูปแบบวันที่, รหัสทำลายไดเรกทอรี, รหัส ID สุ่ม)
แพ็คเกจ cn.hncu.utils; นำเข้า java.text.simpledateFormat; นำเข้า java.util.date; นำเข้า java.util.uuid; ชั้นเรียนสาธารณะ myutils {ส่วนตัว myutils () {} รูปแบบคงที่ สตริงคงที่สาธารณะ getCurrentDateTime () {return format.format (วันที่ใหม่ ()); } สตริงคงที่สาธารณะ getUuid () {uuid uuid = uuid.randomuuid (); string id = uuid.toString (). replaceall ("-", ""); return id; } สตริงคงที่สาธารณะ getDir (สตริง uuid) {string dir1 = integer.tohexstring (uuid.hashcode () & 0xf); String dir2 = integer.tohexstring ((uuid.hashCode () & 0xf0) >> 4); ส่งคืน dir1+"/"+dir2; -Dom4Jfactory (การดำเนินการที่เกี่ยวข้องของ DOM4J, รับวัตถุเอกสาร, บันทึกการดำเนินการ)
แพ็คเกจ cn.hncu.utils; นำเข้า java.io.file; นำเข้า java.io.fileinputstream; นำเข้า java.io.filenotfoundException; นำเข้า java.io.fileoutputstream; นำเข้า java.io org.dom4j.io.outputformat; นำเข้า org.dom4j.io.saxreader; นำเข้า org.dom4j.io.xmlwriter; คลาสสาธารณะ dom4jfactory {เอกสารคงที่ส่วนตัว Dom = null; เส้นทางสตริงคงที่ส่วนตัว; คงที่ {ลอง {saxReader sax = new SaxReader (); // เรียนรู้วิธีการโหลดเส้นทางทรัพยากรใต้เซิร์ฟเวอร์ (เนื่องจากทรัพยากรของเราได้รับการเผยแพร่จาก MyEclipse ไปยังเซิร์ฟเวอร์ Tomcat ดังนั้นจึงแตกต่างจากโครงการ Java บริสุทธิ์ดั้งเดิม) // ใช้คลาสปัจจุบันเพื่อค้นหาตัวโหลดคลาสแล้วใช้ตัวโหลดคลาส dom = sax.read (ใหม่ fileInputStream (พา ธ )); } catch (exception e) {โยน runtimeException ใหม่ (e.getMessage (), e); }} เอกสารสาธารณะคงที่ getDom () {return dom; } บูลีนแบบคงที่สาธารณะบันทึก () {ลอง {outputFormat format = new OutputFormat (); format.setEncoding ("UTF-8"); XMLWriter W = ใหม่ XMLWriter (ใหม่ fileOutputStream (ไฟล์ใหม่ (พา ธ )), รูปแบบ); W.Write (DOM); w.close (); กลับมาจริง; } catch (exception e) {return false; -เขียนโฟโต้
แพ็คเกจ cn.hncu.dao; นำเข้า java.util.arraylist; นำเข้า java.util.iterator; นำเข้า java.util.list; นำเข้า org.dom4j.document; นำเข้า org.dom4j.element; นำเข้า cn.hncu.demain.photomodel; บูลีนเพิ่ม (PhotoModel Photo) {document dom = dom4jfactory.getDom (); Element root = dom.getRootelement (); องค์ประกอบ ephoto = root.addelement ("photo"); ephoto.addattribute ("id", photo.getId ()); ephoto.addattribute ("realname", photo.getRealName ()); ephoto.addattribute ("dir", photo.getdir ()); ephoto.addattribute ("IP", photo.getip ()); ephoto.addattribute ("DateTime", photo.getDateTime ()); ephoto.addattribute ("ext", photo.getext ()); ephoto.addelement ("desc"). setText (photo.getDesc ()); บูลีนบู = dom4jfactory.save (); กลับ Boo; } // เรียกดูรายการสาธารณะ <Photomodel> getAll () {document dom = dom4jfactory.getDom (); รายการ <Photomodel> list = new ArrayList <HotoModel> (); Element root = dom.getRootelement (); Iterator <Element> it = root.elementIterator (); ในขณะที่ (it.hasnext ()) {PhotoModel photo = new photomodel (); องค์ประกอบ e = it.next (); photo.setId (e.attributeValue ("id")); photo.setDateTime (E.AttributeValue ("DateTime")); photo.setdir (e.attributeValue ("dir")); photo.setext (e.attributeValue ("ext")); photo.setip (E.AttributeValue ("IP")); photo.setRealName (e.attributeValue ("realname")); photo.setdesc (e.elementtext ("desc")); list.add (รูปภาพ); } return list; } public photoModel getSingleById (String id) {document dom = dom4jfactory.getDom (); รายการ <Photomodel> list = getAll (); สำหรับ (PhotoModel Photo: list) {if (photo.getId (). เท่ากับ (id)) {return photo; }} return null; } บูลีนสาธารณะ del (รหัสสตริง) {document dom = dom4jfactory.getdom (); องค์ประกอบ e = (องค์ประกอบ) dom.selectsingLenode ("// photo [@id = '"+id.trim ()+"']"); return e.getParent (). ลบ (e); - เขียนสี่ servlets ในตอนท้าย
UploadServlet อัปโหลดรหัส Servlet
แพ็คเกจ cn.hncu.servlets; นำเข้า java.io.file; นำเข้า java.io.ioexception; นำเข้า java.io.inputstream; นำเข้า java.io.printwriter; นำเข้า java.util.list; นำเข้า Javax.servlet.servletexception; javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.apache.commons.fileupload.fileitem; นำเข้า org.apache.Commons.fileupload org.apache.commons.fileupload.disk.diskfileitemfactory; นำเข้า org.apache.commons.fileupload.servlet.servletfileupload; นำเข้า org.apache.commons.io.fileutils; cn.hncu.utils.myutils; คลาสสาธารณะ uploadservlet ขยาย httpservlet {โมฆะสาธารณะ doget (httpservletrequest คำขอ, httpservletresponse การตอบสนอง) โยน servletexception, ioexception PrintWriter out = response.getWriter (); out.println ("<! doctype html สาธารณะ/"-// w3c // dtd html 4.01 transitional // en/">"); out.println ("<html>"); out.println ("<head> <title> a servlet </title> </head>"); out.println ("<body>"); out.println ("ไม่รองรับการอัปโหลด!"); out.println ("</body>"); out.println ("</html>"); out.flush (); out.close (); } โมฆะสาธารณะ dopost (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setContentType ("text/html; charset = utf-8"); PrintWriter out = response.getWriter (); String Path = request.getServletContext (). getRealPath ("/photos"); โรงงาน DiskFileItemFactory = ใหม่ diskfileitemfactory (); Factory.SetRepository (ไฟล์ใหม่ ("G:/A")); ServletFileUpload Upload = ใหม่ servletFilePilePload (โรงงาน); upload.setSizemax (1024*1024*10); // สูงสุด 10m upload.SetheAderEncoding ("UTF-8"); // ใช้เพื่อตั้งค่าการเข้ารหัสชื่อไฟล์เทียบเท่ากับ: request.Setcharacterencoding ("UTF-8"); fileItem fi = null; ลอง {list <fileItem> list = upload.parserequest (คำขอ); PhotoModel Photo = new Photomodel (); // การห่อหุ้มข้อมูล --- 7 แอตทริบิวต์จำเป็นต้องใช้บูลีน Boo = FALSE; inputStream ใน = null; สำหรับ (fileItem fi2: list) {fi = fi2; if (fi.isformfield ()) {string desc = fi.getString ("UTF-8"); photo.setDesc (desc); // desc} else {in = fi.getInputStream (); String realName = fi.getName (); if (realname == null || realname.trim (). เท่ากับ ("")) {out.print ("ไม่เลือกไฟล์!"); กลับ; } if (realname.indexof ("//")! =-1) {realName = realName.substring (realname.lastindexof ("//")+1); } photo.setRealName (realName); // สตริงชื่อไฟล์จริง ext = realName.SubString (realName.AltIstIndexof (".")); photo.setext (ext); // 3 photos.setDateTime (myutils.getCurrentDateTime ()); // 4 photo.setid (myutils.getuuid ()); // 5 photo.setdir (myutils.getdir (photo.getid ()); ฐานข้อมูล Photodao Dao = New PhotoDao (); boo = dao.add (รูปภาพ); // หากข้อมูลภาพด้านบนถูกบันทึกเรียบร้อยแล้วคุณจะเริ่มรับไฟล์รูปภาพและบันทึกลงในฮาร์ดดิสก์เซิร์ฟเวอร์ถ้า (boo) {system.out.println (dao); path = path+"/"+photo.getDir (); ไฟล์ dir = ไฟล์ใหม่ (พา ธ ); if (! dir.exists ()) {dir.mkdir (); } string filename = path+"/"+photo.getId ()+photo.getext (); fileutils.copyInputStreamTofile (ในไฟล์ใหม่ (ชื่อไฟล์)); string strpath = request.getContextPath ()+"/servlets/sweetervlet"; out.print ("อัปโหลดสำเร็จ! <a href = '"+strpath+"'> อัลบั้มเรียกดู </a>"); } else {out.print ("อัปโหลดล้มเหลว!"); }} catch (fileuploadexception e) {โยน runtimeException ใหม่ ("อัปโหลดล้มเหลว!"); ", e);} ในที่สุด {ถ้า (fi! = null) {fi.delete ();}} out.flush (); out.close ();}}Sweetervlet ด้าน servlet ของอัลบั้มการเรียกดู
แพ็คเกจ cn.hncu.servlets; นำเข้า java.io.ioexception; นำเข้า java.io.printwriter; นำเข้า java.util.list; นำเข้า javax.servlet.servletexception; นำเข้า javax.servlet.http.httpservlet; javax.servlet.http.httpservletResponse; นำเข้า cn.hncu.dao.photodao; นำเข้า cn.hncu.demain.photomodel; คลาสสาธารณะ showservlet ขยาย httpservlet {ioexception หากเกิดข้อผิดพลาด ServleTexception, iOexception {dopost (คำขอ, การตอบกลับ); } โมฆะสาธารณะ dopost (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setContentType ("text/html; charset = utf-8"); request.Setcharacterencoding ("UTF-8"); PrintWriter out = response.getWriter (); out.println ("<! doctype html สาธารณะ/"-// w3c // dtd html 4.01 transitional // en/">"); out.println ("<html>"); out.println ("<head> <title> a servlet </title> </head>"); out.println ("<body>"); table string = "<table border = '1' width = '100%'>"+ "<tr> <th> ชื่อไฟล์ </th> <th> อัปโหลด ip </th> <th> เวลาอัปโหลด </th> <th> ภาพ </th> <th> คำแนะนำ </th> <th> การดำเนินการ </th> </tr>"; out.print (ตาราง); Photodao Dao = New Photodao (); รายการ <Photomodel> list = dao.getall (); สำหรับ (photomodel p: list) {out.print ("<tr>"); out.println ("<td>"+p.getRealName ()+"</td>"); out.println ("<td>"+p.getip ()+"</td>"); out.println ("<td>"+p.getDateTime ()+"</td>"); // เอาท์พุทรูปภาพสตริงพา ธ = request.getContextPath ()+"/photos/"+p.getDir ()+"/"+p.getId ()+p.getext (); out.println ("<td> <a href = '"+path+"'> <img src = '"+path+"' width = '200' ความสูง = '200'> </img> </a> </td>"); String op = "<a href = '"+request.getContextPath ()+"/servlets/delservlet? id ="+p.getId ()+"'> ลบ </a>"; out.println ("<td>"+p.getdesc ()+"</td>"); op+= "<a href = '"+request.getContextPath ()+"/servlets/downservlet? id ="+p.getId ()+"'> ดาวน์โหลด </a>"; out.println ("<td>"+op+"</td>"); out.print ("</tr>"); } out.println ("</table>"); out.println ("</body>"); out.println ("</html>"); out.flush (); out.close (); -รหัสเซิร์ฟเวอร์ดาวน์โหลดโดย Downsertlet
แพ็คเกจ cn.hncu.servlets; นำเข้า java.io.fileinputstream; นำเข้า java.io.fileoutputstream; นำเข้า java.io.ioException; นำเข้า Java.io.InputStream; นำเข้า Java.io.OutputStream; นำเข้า Java.io.printwriter; javax.servlet.servletexception; นำเข้า javax.servlet.http.httpservlet; นำเข้า Javax.servlet.http.httpservletrequest นำเข้า javax.servlet.http.httpservletresponse; cn.hncu.demain.photomodel; คลาสสาธารณะ Downservlet ขยาย httpservlet {โมฆะสาธารณะ doGet (คำขอ httpservletrequest, httpservletresponse การตอบสนอง) โยน servletexception, ioexception {dopost (คำขอ, การตอบสนอง); } โมฆะสาธารณะ dopost (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {string id = request.getParameter ("id"); Response.SetContentType ("แอปพลิเคชัน/แรง-ลง"); Photomodel P = New PhotoDao (). GetingleById (ID); if (p! = null) {string realname = p.getRealName (); realName = สตริงใหม่ (realName.getBytes ("ISO8859-1"), "UTF-8"); Response.SetheAder ("เนื้อหา-การจัดสรร", "เอกสารแนบ; ชื่อไฟล์ =/" "+realName+"/""); สตริง relpath = getServletContext (). getRealPath ("/photos/"+p.getDir ()+"/"+p.getId ()+p.getext ()); inputStream ใน = ใหม่ fileInputStream (relpath); outputStream out = response.getOutputStream (); System.out.println (relpath); byte buf [] = ไบต์ใหม่ [1024]; int len = 0; ในขณะที่ ((len = in.read (buf))! =-1) {out.write (buf, 0, len); } out.close (); } else {response.setContentType ("text/html; charset = utf-8"); Response.getWriter (). println ("ไฟล์นี้ถูกลบ!"); -DelServlet Delete Server
แพ็คเกจ cn.hncu.servlets; นำเข้า java.io.file; นำเข้า java.io.fileinputstream; นำเข้า java.io.ioException; นำเข้า java.io.inputstream; นำเข้า Java.io.OutputStream; นำเข้า Java.io.printwriter; javax.servlet.http.httpservlet; นำเข้า javax.servlet.http.httpservletrequest นำเข้า Javax.servlet.http.httpservletresponse; นำเข้า cn.hncu.dao.photodao; {โมฆะสาธารณะ DOGET (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {dopost (คำขอ, การตอบสนอง); } โมฆะสาธารณะ dopost (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {request.Setcharacterencoding ("UTF-8"); Response.setContentType ("ข้อความ/html; charset = utf-8"); PrintWriter out = response.getWriter (); string id = request.getParameter ("id"); Photomodel P = New PhotoDao (). GetingleById (ID); ถ้า (p! = null) {ถ้า (! p.getip (). เท่ากับ (request.getRemoteaddr ())) {out.println ("คุณไม่ได้รับอนุญาตให้ลบออก!"); กลับ; } // ※※※※ส่วนต่อไปนี้จะถูกเพิ่มในคลาสต่อไปนี้ !!! // 1 ลบข้อมูลในฐานข้อมูล PhotoDao Dao = New PhotoDao (); บูลีนบู = dao.del (id); // 2Delete ไฟล์ในฮาร์ดดิสก์เซิร์ฟเวอร์ถ้า (boo) {สตริง filename = "Photos/"+P.GetDir ()+"/"+P.GetId ()+P.GetExt (); สตริง pathFileName = getServletContext (). getRealPath (ชื่อไฟล์); ไฟล์ f = ไฟล์ใหม่ (pathfilename); if (f.exists ()) {f.delete (); } string strpath = request.getContextPath ()+"/servlets/showservlet"; out.println ("รูปภาพลบสำเร็จ! <a href = '"+strpath+"'> อัลบั้มเรียกดู </a>"); } else {out.println ("การลบรูปภาพล้มเหลว!"); }} else {response.setContentType ("text/html; charset = utf-8"); Response.getWriter (). println ("ไฟล์นี้ไม่มีอยู่!"); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น