ファイルアップロード
multipart/form-dataに設定する必要があります<input type=“file”>フィールドを追加する必要があります。<s:form action = "testupload" enctype = "multipart/form-data"> <s:textfield name = "username [0]" label = "user-1"> </s:textfield> <s:file name = "photos" label ""> </s:file> <s:textfield name = "username [1] label = "photos"> </s:file> <s:textfield name = "username [2]" label = "user-3"> </s:textfield> <s:file name = "photos" label "label" label "label"> "photo"> </s:file> <s:submit value = "submit"> </s:submit> </s:form>
public class uploadactionはactionsupportを拡張します{ @setter @getterプライベートリスト<ファイル>写真; @setter@getter private list <string> photocontenttype; @setter@getterプライベートリスト<string> photosfilename; @setter@getterプライベートリスト<string> username; public string testupload()throws ioexception {system.out.println( "username:"+username); System.out.println( "写真:"+写真); System.out.println( "PhotosfileName:"+PhotosfileName); System.out.println( "PhotoscontentType:"+PhotoscontentType); //ファイルをサーバールートディレクトリのアップロードファイルに渡す// servletcontext servletcontext servletcontext = servletactionContext.getServletContext(); //リアルパス文字列realpath = servletcontext.getRealPath( "/upload")を取得します。 System.out.println(RealPath);ファイルuploadFile = new File(RealPath); //パスが存在するかどうかを判断します(!uploadFile.exists()){// uploadfile.mkdir();を作成します。 } for(int i = 0; i <photos.size(); i ++){uuid uuid = uuid.randomuuid(); fileutils.copyfile(photos.get(i)、new file(realpath + "/" + uuid + photosfilename.get(i))); }成功を返す; }}1.小さな問題をいくつ扱う必要がありますか?
1。ファイル名には重複した名前があります。一般に、UUIDはファイル名の前にプレフィックスとして生成できます。
2。単一のファイルのサイズを制限します
3.ファイルタイプを制限します
4。合計ファイルサイズを制限します
2. Fileuploadインターセプターは、これらの属性値を設定するためにStruts2で提供されます。
Fileuploadインターセプターには、設定できる3つのプロパティがあります。
注: org.apache.struts2の下のdefault.propertiesにアップロードされたファイルの合計サイズに制限があります。定数を使用して、この制限struts.multipart.maxsize = 2097152を変更できます
<constant name = "struts.devmode" value = "true"/> <! - ここで合計ファイルのサイズを変更します - > <constant name = "struts.multipart.maxsize" value = "2097152"/> <パッケージ名= "default" namespace = "/" extends = "struts-default" name = "defaultStack"> <! - 単一のファイルのサイズを変更すると、commons fileuploadコンポーネントはデフォルトでアップロードされたファイルを受け入れます。ファイルをアップロードするための拡張機能 - > <param name = "fileupload.allowedextensions"> jpg、gif </param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name = "myInterceptor"> </default-interceptor-ref> <extupload " name = "success">/web-inf/views/success.jsp </result> <result name = "input">/upload.jsp </result> </action> </package>
1.ファイルのアップロードに関連するエラーメッセージ?
1。ファイルアップロードに関連するエラーメッセージは、struts-messages.propertiesファイルで事前に定義されています。
2.ファイルのアクションに対応するリソースファイルをアップロードするか、i18n_zh_cn.properties International Resourceファイルのエラーメッセージを再定義できます
struts.messages.error.file.too.large =渡すファイルは大きすぎます。
ファイルのダウンロード
一部のアプリケーションでは、ファイルをユーザーのブラウザに動的に送信する必要がある場合があり、ファイルの名前とストレージの場所はプログラミング時に予測不可能です
サンプルコード
<a href = "testdownload">ダウンロード</a>
public class downloadlowdaction extends actionsupport {//通常、次のパラメーターはaction @setter @getter private string contentTypeで提供されます。 @setter@getter private long contentlength; @setter@getter private string contentdisposition; @setter@getter private inputstream inputstream; public String testDownLoad()SLOWS FILENOTFOUNDEXCEPTION、UNSUPPORTEDENCODINGEXCEPTION {// get servletContext servletcontext servletcontext = servletactionContext.getServletContext(); //ファイル文字列のパスを取得lealpath = servletcontext.getRealPath( "/web-inf/file/aot you.mp3"); // file inputstreamのストリームを取得= new fileinputStream(RealPath); //ファイルタイプContentType = servletcontext.getMimeType(RealPath); //ファイルの長さを取得contentlength = new file(realpath).length(); //ファイル名を設定しますstring filename = "a at you.mp3"; filename = new String(filename.getBytes( "gbk")、 "ISO8859-1"); contentDisposition = "attachment; filename ="+filename;成功を返す; }}<! - ファイルダウンロード - > <アクションname = "testdownload" method = "testdownload"> <result Type = "> <! - ファイルバッファーサイズ - > <param name =" buffersize "> 2048 </param> </result> </action>
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。