ใช้โหมดตัววนซ้ำและโหมดการรวม
เนื่องจากความต้องการของโครงการฉันจึงจำรูปแบบการออกแบบที่ฉันเคยเห็นมาก่อนดังนั้นฉันจึงมีความคิดในการส่งออกคลิกเดียว
ความคิดนั้นง่ายและชัดเจน เพียงแค่ดูทีละขั้นตอน
1. สร้างวัตถุคอมโพสิต
บทคัดย่อคลาสสาธารณะ FileComponent { / ** * คำอธิบาย: สร้างโฟลเดอร์ซ้ำหรือไฟล์ * / โมฆะสาธารณะ mkfile () {โยน unsupportedoperationException ใหม่ (); } / ** * คำอธิบาย: รับพา ธ อินพุตไฟล์ * / สตริงสาธารณะ getInpath () {โยน unsupportedOperationException ใหม่ (); } / ** * คำอธิบาย: รับพาพุทไฟล์ * / สตริงสาธารณะ getOutPath () {โยน unsupportedOperationException ใหม่ (); } / *** คำอธิบาย: สำหรับโฟลเดอร์คุณสามารถเพิ่มโฟลเดอร์หรือไฟล์อื่น ๆ* / โมฆะสาธารณะเพิ่ม (fileComponent fileComponent) {โยน unsupportedOperationException ใหม่ (); -วัตถุชุดค่าผสมนี้สามารถเป็นวัตถุโฟลเดอร์หรือวัตถุไฟล์เฉพาะ ในการโทรที่ตามมาไม่จำเป็นต้องรู้ว่าเป็นโฟลเดอร์หรือไฟล์ (นั่นคือความโปร่งใสของโหมดรวม)
2. การใช้งานนามธรรมของวัตถุคอมโพสิต
การดำเนินการของคลาสนามธรรมข้างต้นมีดังนี้:
คลาสสาธารณะ zipfileitem ขยาย fileComponent {// พา ธ ของสตริงไฟล์อินพุต inpath; // พา ธ ของสตริงไฟล์เอาต์พุต outpath; // รายการข้อมูลไฟล์ subnode <SiveComponent> fileComponents = new ArrayList <SiveComponent> (); // inpath สามารถเป็น null public zipfileitem (string outpath) {this.outpath = outpath; } // เส้นทางไดเรกทอรีต้นฉบับของไฟล์ที่บีบอัดและตำแหน่งเป้าหมายที่บีบอัด zipfileItem (สตริง inpath, สตริง outpath) {this.inpath = inpath; this.outpath = outpath; } โมฆะสาธารณะเพิ่ม (fileComponent fileComponent) {fileComponents.add (fileComponent); } โมฆะสาธารณะลบ (fileComponent fileComponent) {fileComponents.remove (fileComponent); } @Override สตริงสาธารณะ getInpath () {return inpath; } @Override สตริงสาธารณะ getOutPath () {return outpath; } @Override โมฆะสาธารณะ mkfile () {fileutils.createfile (inpath, outpath); Iterator <FileComponent> iterator = fileComponents.iterator (); // ถ้าเป็นโฟลเดอร์คุณยังสามารถวนซ้ำผ่านไฟล์และวัตถุไฟล์เฉพาะในวัตถุในขณะที่ (iterator.hasnext ()) {fileComponent fileComponent = iterator.next (); fileComponent.mkfile (); -3. คลาสเครื่องมือไฟล์
Public Class ConferenceFileUtils { /** * คำอธิบาย: สร้างไฟล์ในเส้นทางเอาต์พุตแบบสัมบูรณ์ตามเส้นทางที่สมบูรณ์ของไฟล์ * @param inpath Path หากคุณต้องการสร้างมันขึ้นอยู่กับไฟล์ที่มีอยู่คุณต้องผ่านเส้นทางเอาต์พุตออก * @param outpath หากเป็นไดเรกทอรีคุณจะไม่ใช้ */ โมฆะคงที่สาธารณะ createFile (String inpath, string outpath) {filein = ไฟล์ใหม่ (inpath); ไฟล์ไฟล์ = ไฟล์ใหม่ (outpath); // หากไฟล์เป้าหมายมีอยู่แล้วให้ละเว้นหากไม่มีไฟล์ จากนั้นสร้าง if (! fileout.exists ()) {int lastSeparator = outpath.AlastIndexof (file.Separator); String lastPart = outpath.substring (LastSeparator); // ถ้าไม่ใช่โฟลเดอร์ให้สร้างไฟล์ถ้า (lastpart.lastindexof (".")! =-1) {loggerutil.info ("------------ การทำคอนกรีตไฟล์ --------------"+outpath); FileInputStream ใน = null; fileOutputStream out = null; ไดเรกทอรีไฟล์ = null; ลอง {directory = ไฟล์ใหม่ (outpath.substring (0, lastseparator+1)); directory.mkdirs (); out = new fileOutputStream (fileout); // ถ้าไฟล์ต้นฉบับมีอยู่ถ้า (filein.exists ()) {in = new fileInputStream (filein); int len; ไบต์ [] buf = ไบต์ใหม่ [10240]; ในขณะที่ ((len = in.read (buf))> 0) {out.write (buf, 0, len); } out.close (); in.close (); ใน = null; }} catch (ioexception e) {system.err.println ("การสร้างไฟล์ล้มเหลว!", e); }} // ถ้าเป็นโฟลเดอร์ให้สร้างโฟลเดอร์ หากโฟลเดอร์คลาสแม่ไม่มีอยู่ให้สร้าง else { System.err.println ("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - ความสำเร็จของบูลีน = deletedir (ไฟล์ใหม่ (dir, children [i]); FileInputStream ใน = null; } catch (ioexception e) {โยน iOexception ใหม่ (e);4. รหัสลอจิกส่งออกหลัก
คลาสสาธารณะ ExportMaterialToziptemplate {@Resource Private EnrichfilelevelsService EnrichfilelevelsService; // ชื่อโฟลเดอร์รูทไดเรกทอรีหรือดาวน์โหลดชื่อไฟล์เบราว์เซอร์ส่วนตัวสตริงดาวน์โหลด ZipName; // ที่อยู่ไดเรกทอรีรูทสตริงส่วนตัวบันทึก = "d: // tempfile"; // เส้นทางรูทไดเรกทอรีสตริงส่วนตัว Superrootpath; // Root Directory Object Private FileComponent Superroot; // พารามิเตอร์ธุรกิจ DTO ExportAllTheMaterialdto Paramdto; // การตอบสนองส่วนตัว httpservletresponse; Public ExportMaterialtoziptemplate (ExportAllTheMaterialdto Paramdto, EnrichfilelevelsService EnrichfilelevelsService, การตอบสนอง httpservletResponse) {this.downloadzipname = paramdto.getDownloadzipName (); this.paramdto = paramdto; this.response = การตอบสนอง; this.enrichfilelevelsService = enrichFileLevelsService; this.SuperRootPath = savepath+file.Sparator+downloadzipname; this.superroot = zipfileitem ใหม่ (superrootpath); } // 1 ห่อหุ้มรูทไดเรกทอรีโมฆะส่วนตัว enrichfilelevels () {enrichfilelevelsservice.enrichfilelevels (superroot, superrootpath, paramdto); } // 2 สร้างระดับไดเรกทอรีไฟล์นั่นคือสร้างไฟล์ทั้งหมด (รวมถึงโฟลเดอร์) โมฆะส่วนตัว createAllTheFiles () {ถ้า (null! = superroot) {superroot.mkfile (); }} // 3 หลังจากสร้างระดับไฟล์แล้วบีบอัดและดาวน์โหลดไปยังเบราว์เซอร์ส่วนตัว Void CompressandDownload () {ไฟล์ srcfile = ไฟล์ใหม่ (fortifyutil.filterfilename (superrootpath)); String TargetFilePath = savePath+file.Saperator+srcfile.getName ()+". zip"; File TargetFile = ไฟล์ใหม่ (FortifyUtil.FilterFileName (TargetFilePath)); zipfileutil.zipfiles (srcfile, targetFile); ลอง {// บีบอัดไฟล์เส้นทางชั่วคราวสตริง downfileName = downloadZipName+". zip"; Response.reset (); // กำหนดประเภทการตอบสนองของเอาต์พุต SetContentType ("แอปพลิเคชัน/octet-stream"); Response.SetheAder ("เนื้อหา-การจัดสรร", "เอกสารแนบ; filename =" + สตริงใหม่ (downfilename.getBytes ("GBK"), "ISO-8859-1") + "; size =" + targetFile.length ()); OutputFileUtil.OutputFile (TargetFile, การตอบสนอง); // ลบโฟลเดอร์ที่เก็บไว้ชั่วคราวถ้า (srcfile.exists ()) {ConferenceFileUtils.deletedir (srcfile); } // ลบแพ็คเกจบีบอัดชั่วคราวถ้า (targetFile.exists ()) {targetFile.delete (); }} catch (ioexception e) {devlog.error (e.getMessage ()); }} // การส่งออกแบบคลิกเดียวโหมดปรากฏตัวโมฆะสาธารณะส่งออก () {enrichfilelevels (); CreateAllTheFiles (); compressanddownload (); -5. อินเทอร์เฟซระดับไฟล์ Enrich
อินเทอร์เฟซสาธารณะ EnrichFilelevelsService {Public Void Enrichfilelevels (Superroot Superroot, Superrootpath String, ExportAllTheMaterialdto paramdto);} ในสถานการณ์ทางธุรกิจที่แตกต่างกันเพียงแค่ใช้อินเทอร์เฟซนี้ใช้เมธอด EnrichFileLevels () ผ่านอินสแตนซ์คลาสที่ใช้อินเทอร์เฟซนี้กับวิธีการสร้างของคลาส ExportMaterialToziptemplate จากนั้นเรียกใช้วิธีการส่งออก () ตอนนี้
ใหม่ ExportMaterialtoziptemplate (dtoparams,
EnrichFileLevelsService, Response) .Export ();
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น