イテレータモードとコンビネーションモードを使用して、ワンクリックエクスポートとダウンロードをブラウザにzip圧縮パッケージファイルにダウンロードする
プロジェクトのニーズのために、私は以前に見たデザインパターンを思い出したので、ワンクリックエクスポートのアイデアがありました。
アイデアはシンプルで明確です。段階的に見てください。
1.複合オブジェクトを作成します
public abstract class filecomponent { / ** *説明:フォルダーを再帰的に作成するか、file * / public void mkfile(){supl unsupportedoperationexception(); } / ** *説明:ファイル入力パスを取得 * / public string getInPath(){supportedoperationexception(); } / ** *説明:ファイル出力パスを取得 * / public String getOutPath(){surportedoperationexception(); } / ***説明:フォルダーの場合、他のフォルダーまたはファイルを追加できます* / public void add(filecomponent filecomponent){show new unsupportedoperationexception(); }}この組み合わせオブジェクトは、フォルダーオブジェクトまたは特定のファイルオブジェクトにすることができます。後続の呼び出しでは、それがフォルダーかファイルか(つまり、コンビネーションモードの透明度)かを知る必要はありません。
2。複合オブジェクトの抽象クラスの実装
上記の抽象クラスの実装は次のとおりです。
パブリッククラスzipfileitemは、filecomponentを拡張します{//入力ファイル文字列のパスinpath; //出力ファイル文字列Outpathのパス。 //サブノードファイル情報リスト<FileComponent> filecomponents = new ArrayList <FileComponent>(); // inpathはnull public zipfileitem(string outpath){this.outpath = outpath; } //圧縮ファイルのソースディレクトリパスと圧縮ターゲット位置public zipfileitem(string inpath、string outpath){this.inpath = inpath; this.outpath = outpath; } public void add(filecomponent filecomponent){filecomponents.add(filecomponent); } public void remove(filecomponent filecomponent){filecomponents.remove(filecomponent); } @Override public String getInpath(){return inpath; } @Override public String getoutPath(){return outpath; } @Override public void mkfile(){fileutils.createfile(inpath、outpath); iterator <filecomponent> iterator = filecomponents.iterator(); //フォルダーの場合は、ファイルとオブジェクト内の特定のファイルオブジェクトを繰り返すこともできます。 filecomponent.mkfile(); }}}3。ファイルツールクラス
パブリッククラスConferenceFileutils { /** *説明:ファイルの絶対パスに基づいて、絶対出力パスにファイルを作成します * @param inpath入力パス。既存のファイルに基づいて作成する場合は、 * @param Outpath出力パスを渡す必要があります。ディレクトリの場合、 */ public static void createfile(string inpath、string outpath){file filein = new file(inpath)を使用しません。 file fileout = new file(outpath); //ターゲットファイルが既に存在する場合は、ファイルが存在しない場合は無視してください。その後、if(!fileout.exists()){int lastseparator = outpath.lastindexof(file.separator); string lastpart = outpath.substring(lastseparator); //フォルダーでない場合は、if(lastPart.lastIndexof( "。")!= -1){loggerutil.info( "------------ concretefile --------------"+outpath)を作成します。 fileInputStream in = null; fileoutputStream out = null; file directory = null; try {directory = new file(outpath.substring(0、lastseparator+1)); directory.mkdirs(); out = new fileoutputStream(fileout); //ソースファイルが存在する場合(filein.exists()){in = new fileinputStream(filein); int len; byte [] buf = new byte [10240]; while((len = in.read(buf))> 0){out.write(buf、0、len); } out.close(); in.close(); in = null; }} catch(ioException e){system.err.println( "ファイルの作成失敗!"、e); }} //フォルダーの場合、フォルダーを作成します。親クラスフォルダーが存在しない場合は、else {を作成します{ System.err.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- boolean success = deletedir(dir、children [i]); fileinputs inull } catch(ioexception {throw ioexception(e);4。コアエクスポートロジックコード
Public Class ExportMaterialToziptemplate {@Resource Private RedichFileLevelsService RedichFileLevelsService; //ルートディレクトリフォルダー名またはダウンロードブラウザファイル名プライベート文字列downloadzipname; //ルートディレクトリアドレスプライベート文字列savepath = "d:// tempfile"; //ルートディレクトリパスプライベートストリングSuperRootPath; //ルートディレクトリオブジェクトプライベートファイルコンポーネントスーパールート。 //ビジネスパラメーターdto private exportallthematerialdto paramdto; //応答private private httpservletResponse応答。 public exportmaterialtoziptemplate(exportallthematerialdto paramdto、richfilelelesservice richfilelevelesservice、httpservletresponse応答){this.downloadzipname = paramdto.getdownoloadzipname(); this.paramdto = paramdto; this.response = response; this.enrichfilevelsservice = renichfilelelesservice; this.superrootpath = savepath+file.separator+downloadzipname; this.superroot = new zipfileitem(superrootpath); } // 1。ルートディレクトリのプライベートボイドrichfileLevels(){enthfilelelesservice.enrichfileleveles(superroot、superrootpath、paramdto)をカプセル化する; } // 2。ファイルディレクトリレベルを生成します。つまり、すべてのファイル(フォルダーを含む)を作成しますprivate void createallthefiles(){if(null!= superroot){superroot.mkfile(); }} // 3。ファイルレベルを生成した後、それを圧縮してブラウザプライベートvoid compressanddownload(){file srcfile = new file(fortifyutil.filterfilename(superrootpath)); string targetfilepath = savepath+file.separator+srcfile.getName()+"。zip";ファイルターゲットファイル= new File(Fortifyutil.FilterFileName(TargetFilePath)); zipfileutil.zipfiles(srcfile、targetfile); try {//ファイルを圧縮する一時パス文字列downfileName = downloadzipname+"。zip"; Response.reset(); //出力タイプResponse.setContentType( "Application/Octet-Stream"); Response.setheader( "content-disposition"、 "attachment; filename =" + new String(downfileName.getBytes( "gbk")、 "ISO-8859-1") + "; size =" +ターゲットファイル()length(); outputfileutil.outputfile(targetfile、response); //一時的に保存されたフォルダーを削除するif(srcfile.exists()){conferenctfileutils.deletedir(srcfile); } //一時的な圧縮パッケージを削除するif(targetfile.exists()){targetfile.delete(); }} catch(ioexception e){devlog.error(e.getmessage()); }} //ワンクリックエクスポート、外観モードpublic void export(){retrichfilelevels(); createallthefiles(); CompressandDownLoad(); }}5。ファイルレベルのインターフェイスを濃縮します
パブリックインターフェイスretrichfilelevelsservice {public void richfilelelevels(filecomponent superroot、string superrootpath、exportallthematerialdto paramdto);}さまざまなビジネスシナリオで、このインターフェイスを実装するだけで、retrichFileLevels()メソッドを実装し、このインターフェイスをExportMaterialToziptemplateクラスのコンストラクターメソッドに実装するクラスインスタンスを渡し、ExportMaterialToziptemplateクラスインスタンスのExport()メソッドを呼び出します。今すぐ
新しいexportmaterialtoziptemplate(dtoparams、
retrichfileLevelsService、response).export();
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。