1。Strutsファイルをアップロードするためのアイデア
また、Fileuploadコンポーネントについても話しました。強力な機能がありますが、操作は複雑で複雑です。今回は、ファイルのアップロードもStrustでサポートされており、Fileuploadはパッケージ化されています。これははるかに便利です。
ここにタグとクラスがあります:
<html:file property = "actionformのプロパティ名に対応">
このタグを使用してファイルをアップロードしますが、コンテンツを受信するには、org.apache.struts.upload.formfileに依存する必要があります
インターフェイスが完了しました。
2。実現
sh.jsp:
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "UTF-8"%> <%@ taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "bean"%> <%@ taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html"%> <%@ taglib uri = "http://jakarta.org/struts/tags-logic" prefix = "logic" html 4.01トランジショナル// en "> <html> <head> <title> sh.jsp </title> <meta http-equiv =" content-type "content =" text/html enctype = "multipart/form-data"> <html:file property = "photo"> </html:file> <html:submit value = "upload"> </html:submit> </html:form> </body> </html>
uploadform.java:
パッケージcom.zyy.struts.form; Import org.apache.struts.action.actionform; Import org.apache.struts.upload.formfile; public class uploadform extends actionform {private formfile写真; public formfile getphoto(){return photo; } public void setphoto(formfile photo){this.photo = photo; }} iptimestamp.java:
パッケージcom.zyy.util; import java.text.simpledateformat; import java.util.date; import java.util.random; public class iptimestamp {private simpledateformat sdf = null;プライベート文字列IP = null; public iptimestamp(){} public iptimestamp(string ip){this.ip = ip; } public string getiptimerand(){stringbuffer buf = new StringBuffer(); if(this.ip!= null){string s [] = this.ip.split( "//。"); for(int i = 0; i <s.length; i ++){buf.append(this.addzero(s [i]、3)); }} buf.append(this.getTimestamp());ランダムr = new Random(); for(int i = 0; i <3; i ++){buf.append(r.nextint(10)); } buf.toString();を返します。 } public String getDate(){this.sdf = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss.sss"); this.sdf.format(new date()); } public string getTimestamp(){this.sdf = new SimpledateFormat( "yyyymmdhhmmsssssssss"); this.sdf.format(new date()); } private string addzero(string str、int len){stringbuffer s = new StringBuffer(); S.Append(str); while(s.length()<len){s.insert(0、 "0"); } s.toString()を返します。 } public static void main(string args []){system.out.println(new iptimestamp( "192.168.1.1")。getiptimerand()); }}uploadaction.java:
パッケージcom.zyy.struts.action; Import java.io.file; import java.io.fileoutputStream; Import java.io.inputStream; Import java.outputStream; Import javax.servlet.http.httpservletrequest; Import javax.http.httpseponse; org.apache.struts.action.crut.struts.actionform; import org.apache.struct.action.actionforward; import org.pache.struct.action.actionmapping; import com.zyy.struct.form.uploadform; import com.zyy.utimestamp; public class -uploageds maktime exciants ma ActionForm Form、httpservletrequestリクエスト、httpservletResponse応答)スロー例外{uploadform uploadform =(uploadform)form; iptimestamp ips = new iptimestamp(request.getRemoteaddr()); //ファイル名文字列filename = ips.getiptimerand() + "。" + uploadform.getPhoto()。getFileName()。split( "//。")[uploadform.getphoto()。getFileName()。split( "//。")。 //出力パスファイルoutfile = new file(super.getServlet()。getServletContext().getRealPath( "/") + "upload" + file.separator + filename); //画像が保存されているフォルダーfile file = new file(super.getServlet()。getServletContext().getRealPath( "/") + "upload"); if(!file.exists()){file.mkdir(); } inputstream input = uploadform.getPhoto()。getInputStream(); outputStream output = new FileOutputStream(Outfile);バイトデータ[] = new byte [1024]; int temp = 0; while((temp = input.read(data、0、1024))!= -1){output.write(data); } output.close(); input.close(); nullを返します。 }}struts-config.xml:
<?xml version = "1.0" encoding = "iso-8859-1"?> < <form-beans> <form-bean name = "uploadform" type = "com.zyy.struts.form.uploadform"> </form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <アクションパス= " name = "uploadform" scope = "request" type = "com.zyy.struts.action.uploadaction"> </action> </action-mappings> <message-resources parameter = "resource.messageresources"/> </struts-config>
私が保存しているのはsuper.getServlet()。getServletContext()。getRealPath( "/")+ "upload"
これは、アップロードフォルダーの下の仮想ディレクトリの実際のパスです。
Strutsのファイルアップロードの原理はFileuploadと同じであることがわかりますが、Strutsはパッケージ化されているため、Fileuploadコンポーネントのみを使用するよりも使用する方がはるかに便利です。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。