คลาสนี้สามารถบีบอัดไฟล์หลายไฟล์และส่งคืนสตรีม
การคัดลอกรหัสมีดังนี้:
-
* คำอธิบายวิธี: <b> คลาสทดสอบ </b> </br>
-
Public Class TestFilestream {
// ตำแหน่งที่เก็บไฟล์และแพ็คเกจบีบอัด
StringTempFilePath = "C:/temp/"
รายการ <String> fileList = NewarrayList <String> ();
fileList.Add (tempfilepath+"file1.txt");
fileList.add (tempfilepath+"file2.png");
fileList.Add (tempfilepath+"file3.xls");
// ชื่อแพ็คเกจบีบอัดที่สร้างขึ้น
stringzipname = "filedata";
// กลับไปที่สตรีม
ByTeArrayOutputStreamOutputStream = FileTozip (FileList, FileData, TempFilePath);
// หน้าอินพุตแพ็คเก็ตบีบอัดโฟลว์
Byte [] buffer = outputStream.tobyteArray ();
// การตอบสนองที่ชัดเจน
Response.reset ();
// ตั้งส่วนหัวการตอบกลับ
Response.addheader ("เนื้อหา-การจัดสรร"
"เอกสารแนบ; ชื่อไฟล์ ="+
NewString (("datafile.zip"). getBytes ("GB2312"), "iso8859-1"));
Response.addheader ("ความยาวเนื้อหา", ""+outputstream.size ());
toClient = newBufferedOutputStream (response.getOutputStream ());
Response.SetContentType ("แอปพลิเคชัน/octet-stream");
toClient.write (บัฟเฟอร์);
toClient.flush ();
-
-
*คำอธิบายวิธี: <b> บีบอัดหลายไฟล์ลงในแพ็คเกจซิป </b> </br>
-
PublicByTeArrayOutputStreamFileTozip (รายการ <String> FileList, StringZipName, StringTempFilePath) {
Byte [] buffer = newByte [1024];
ZipOutputStreamout = null;
พยายาม{
out = newzipoutputStream (newFileOutputStream (tempFilePath+zipname+". zip"));
รายการ <file> filedata = newarrayList <file> ();
สำหรับ (inti = 0, len = fileList.size (); i <len; i ++)
-
filedata.add (newfile (filelist.get (i)));
-
สำหรับ (intj = 0, len = filedata.size (); j <len; j ++)
-
FileInputStreamFis = newFileInputStream (filedata.get (j));
out.putnextentry (newzipentry (filedata.get (j) .getName ()));
Intdatalen;
// อ่านเนื้อหาของไฟล์ที่คุณต้องการดาวน์โหลดและจัดทำแพ็คเกจลงในไฟล์ zip
ในขณะที่ ((datalen = fis.read (บัฟเฟอร์))> 0) {
out.write (บัฟเฟอร์, 0, datalen);
-
out.closeentry ();
fis.close ();
-
out.close ();
-
จับ (ExceptionEx)
-
Ex.PrintStackTrace ();
-
// อ่านแพ็คเกจบีบอัด
fileFileZip = newFile (tempFilePath+zipname+". zip");
ByteArrayOutputStreamBaos = null;
พยายาม
-
BAOS = newByTeArrayOutputStream ();
FileInputStreamIntream = newFileInputStream (fileZip);
bufferedInputStreamBis = newBufferedInputStream (stream);
intc = bis.read ();
ในขณะที่ (c! =-1) {
baos.write (c);
c = bis.read ();
-
bis.close ();
Instream.close ();
-
จับ (ExceptionEx)
-
Ex.PrintStackTrace ();
-
returnbaos;
-