โดยทั่วไปมีสองวิธีในการอัปโหลดรูปภาพ ไฟล์รูปภาพที่เขียนไปยังฐานข้อมูลจำเป็นต้องได้รับการแปลงเป็นรูปแบบสตรีมไบนารีซึ่งใช้พื้นที่ฐานข้อมูลและเหมาะสำหรับการจัดเก็บภาพจำนวนน้อย ว่าพวกเขาค่อนข้างปลอดภัยและไม่สามารถหลีกเลี่ยงได้อย่างง่ายดายโดยผู้ใช้
นำไปใช้ใน struts2 (การอัปโหลดรูปภาพเป็นตัวอย่าง)
1. รายการรหัส FileUpload.jsp มีดังนี้:
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <html> <html> Head> <title> fileuplaoddemo ใน struts2 </title> </head> <body> <s: form action = "fileupload" method = "post" enctype = "multipart/form-data" space = "/"> <s: file name = "myfile" label = "myfile"> </s: ไฟล์> <s: textfield name = "caption" label = "caption"> </s: textfield> <s: ส่ง label = "ส่ง ">/s: ส่ง> </s: ฟอร์ม> </body> </html>
2. รายการฟังก์ชั่นของ showupload.jsp มีดังนี้:
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <html> <html> Head> <title> showupload </title> </head> <body> <div style = "padding: 3px; border: solid 1px #cccccc; : property value = "imageFileName"/> "/> <br/> <s: ค่าคุณสมบัติ =" คำอธิบาย "/> </div>/body> </html>
3. รายการรหัสของ fileuploadaction.java มีดังนี้:
แพ็คเกจ com.chris; นำเข้า java.io.*; นำเข้า java.util.date; นำเข้า org.apache.struts2.servletactionContext; นำเข้า com.opensymphony.xwork2 572146812454L; ส่วนตัวคงที่ int buffer_size = 16 * 1024; // โปรดทราบว่าเมื่อไฟล์ถูกอัปโหลด, <s: ไฟล์ // ถูกผูกไว้กับ myfile, myfilecontenttype, myfilefilename.//so, myfilecontenttype, myfilefi Time Lename Method File Private File MyFile; SetMyFileContentType (String ContentType) {System.out .println ("ประเภทไฟล์:" + contentType); ;. filename = filename;} โมฆะสาธารณะ setMyFile (ไฟล์ myfile) {this .myfile = myFile;} สตริงสาธารณะ getImageFileName () {ret imageFileName;} สตริงสาธารณะ getCaption () {ret urn caption;} ) {สิ่งนี้ .Caption = คำอธิบายภาพ;} สำเนาโมฆะคงที่ส่วนตัว (ไฟล์ SRC, ไฟล์ dst) {ลอง {inputStream ใน = null; outputStream out = null; ลอง {in = new bufferedInputStream (ใหม่ FileInputStream (SRC) out = new bufferedOutputStream (ใหม่ fileOutputStream (dst), buffer_size); byte [] buffer = byte ใหม่ [buffer_size]; ในขณะที่ (in.read (buffer)> 0) {out.write null! = in) {in.close ();} ถ้า (null! = out) {out.close ();}}} catch (Exception e) {E.printstackTrace ();}} สตริงคงที่ส่วนตัว ชื่อไฟล์) {int pos = filename.lastindexof ("."); return filename.substring (pos);}@endridepublic string execute () {imagefilename = วันที่ใหม่ () getTime () + getExtention ไฟล์ใหม่ (servletActionContext.getServletContext (). getRealPath ("uploadimages") + "/" + imageFilename); Copy (myFile, ImageFile); return success;}}}หมายเหตุ: ในเวลานี้เพื่อความสะดวกในการดำเนินการดำเนินการการกระทำที่ได้รับการสืบทอดและวิธีการ overrider execute () คือ
pojo ใด ๆ ใน struts2 สามารถใช้เป็นการกระทำ
4.Struts.xml รายการมีดังนี้:
<? xml version = "1.0" encoding = "utf-8"?> <! doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.0 // en" "http: // strut s.apache.org /dtds/struts-2.0.dtd "> <struts> <package name =" ตัวอย่าง "namespace ="/"extred =" struts-default "> <action name =" fileupload "> <interceptor -ref name =" fileuploadstack " /> <result>/showupload.jsp </result> </action> </packagpact> </struts>
5. รายการ web.xml มีดังนี้:
<? xml version = "1.0" encoding = "utf-8"?> <web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = " http://www.w3.org/2001/xmlschema-instance"xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/ j2ee/web-app_2_4.xsd "> <filter> <filter-name> struts-cleanup </filter-name> <filter-lass> org.apache.struts2.dispatcher.actioncontextcleanup </filter-class> </filter> <silter-mapping> <filter-name> struts-cleanup </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <filter> lter -name> <filter-class> org.apache.struts2.dispatcher.filterDispatcher </filter-class> </filter> >/ *</url-pattern> </tilter-mapping> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </eb-app>
เนื้อหาข้างต้นเป็นเนื้อหาทั้งหมดของวิธีการอัปโหลดภาพใน Java Struts2 ที่แนะนำให้รู้จักกับคุณ