一般に、画像をアップロードするには2つの方法があります。データベースに書き込まれた画像ファイルは、データベーススペースを占めるバイナリストリーム形式に変換する必要があります。たとえば、システム内のいくつかの小さなアイコンがデータベースに書き込む利点があります。それらは比較的安全であり、ユーザーには簡単に避けられません。
struts2に実装されています(例として画像のアップロードを取得)
1. fileupload.jspコードリストは次のとおりです。
<%@ page Language = "Java" Import = "Java.util。*" pageEncoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <html> < head> <title> struts2 </title> </head> <body> <s:form action = "fileupload" method = "post" enctype = "multipart/form-data" name space = "/"> <s:file name = "myfile" label = "myfile"> </s:file> <s:textfield name = "キャプション" label = "caption"> </s:textfield> <s:submit label = "submit ">/s:submit> </s:form> </body> </html>
2。showupload.jspの関数リストは次のとおりです。
<%@ page Language = "Java" Import = "Java.util。*" pageEncoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <html> < head> <title> showupload </title> </head> <body> <div style = "padding:3px; border:solid 1px #ccccc; text-align:center"> <img src = "uploadima ges/<s :Property Value = "ImageFileName"/> "/> <br/> <s:プロパティ値="キャプション "/> </div>/body> </html>
3. fileuploadaction.javaのコードリストは次のとおりです。
パッケージcom.chris; import java.io。*; import java.util.date; import org.apache.struts2.servletactionContext; Import com.opensymphony.xwork2.active onsupport; public class fileuploadactionはActionsupport {private static station serialsionuid = 572146812454l;プライベート静的最終int buffer_size = 16 * 1024; //ファイルがアップロードされたとき、<s:ファイル//はmyfile // myfilecontenttype、myfilefilename.////so、myfilecontenttype、myfilecontenttype、myfilecontenttypeにバインドされていることに注意してください。 Time Lenameのメソッドプライベートファイル//プライベート文字列コンテンツ。 setMyFileContentType(String contentType){System.out .println( "file type:" + contentType); ; this .fileName = filename;} public void setMyFile(file myfile){this .myfile = myfile;} public string getImageFileName(){ret yagefileName;} public string getCaption(){ret urn caption;} public setcaption(wubl ){this .caption = caption;} private static void copy(file src、file dst){try {inputstream in = null; outputstream out = null; try {in = new fileinputStream(src)、bu ffer_size); out = new BufferedOutputStream(new fileoutputStream(dst)、buffer_size); buffer = new byte [buffer_size]; null!= in){in.close();} if(null!= out){out.close();}}} catch(exception e){e.printstacktrace();}} private static string getextention(string(string filename){int pos = filename.lastindexof( "。"); return filename.substring(pos);}@overridepublic string execute(){imagefilename = new date()。新しいファイル(servletactionContext.getServletContext()。getRealPath( "uploadImages") + "/" + imageFileName); copy(myfile、imagefile); return success;}}}注:現時点では、アクションを実装する便利さのためにのみ、ActionSupportは継承され、Overrider execute()メソッドは
Struts2のPojoはアクションとして使用できます
4.Struts.xmlリストは次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <!doctype struts public " - // apacheソフトウェア財団// dtd struts構成2.0 // en" "http:// strut s.apache.org /dtds/struts-2.0.dtd "> <struts> <package name =" example "namespace ="/"extends =" struts-default "> <action name =" fileupload "> <interceptor -ref name =" fileuploadstack "" /> <sults>/showupload.jsp </result> </action> </package> </struts>
5。Web.xmlリストは次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <web-appバージョン= "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/ns/ j2ee/web-app_2_4.xsd "> <filter> <filter-name> struts-cleanup </filter-name> <filter-class> org.apache.struts2.dispatcher.actioncontextcleanup </filter-class> </filter> <フィルターマッピング> <filter-name> struts-cleanup </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <filter> <filter-name> struts2 </fi lter -Name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-na me> <url-pattern >/ *</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </web-app>
上記のコンテンツは、Java Struts2のアップロード方法のすべてのコンテンツです。