1 to export to a ZIP file, you must first write the content to a file. Create the file and then write the content to the file. Writes content to a file through a stream.
such as: File File=new file (filename_);
FileOutputStream fileoutputstream=new fileoutputstream (file);
String neirong= "This is the content of the test";
Byte_s=neirong.getbytes ();
Fileoutputstream.write (byte_s,0,byte_s.length);//This is the writing of byte data to a file via a stream.
Fileoutputstream.close ();
2 Next you want to export to a zip file, you need to create a zip format file. Write the contents to the zip file at the same time.
such as: File Zipfile=new file ("complete" +new Date (). GetTime () + ". zip");
FileOutputStream zipout;
The above is a file that will create a zip format
ZipOut = new FileOutputStream (ZipFile);
Zipoutputstream zip=new Zipoutputstream (zipout);
Zip.close ();
Zipout.close ();
Writes content to a file in a zip format via zipoutputstream. If you want to add any files during the period. Add by ZipEntry.
A zipentry represents a file or a folder.
3 Zipoutputstream is a file that writes a stream to a zip format. ZipEntry is a sub-unit in a zip format file. can represent a file or folder.
If adding a file:
File Zipfile=new file ("complete" +new Date (). GetTime () + ". zip");
FileOutputStream zipout;
The above is a file that will create a zip format
ZipOut = new FileOutputStream (ZipFile);
Zipoutputstream zip=new Zipoutputstream (zipout);
If you are adding a folder:
File Zipfile=new file ("complete" +new Date (). GetTime () + ". zip");
FileOutputStream zipout;
The above is a file that will create a zip format
ZipOut = new FileOutputStream (ZipFile);
Zipoutputstream zip=new Zipoutputstream (zipout);
ZipEntry zipentry1=new ZipEntry (Name of folder + "/");//Name of folder Plus "/"
Zip.putnextentry (ZipEntry1);
byte [] byte_s= "test content AAA". GetBytes ();
Zip.write (byte_s,0,byte_s.length);
Zip.close ();
Zipout.close ();
ZipEntry and Zipoutputstream are inseparable.
4 Now if the ZIP format of the file has been created, then it is necessary to read the zip file. Gets the byte array. It's like reading a normal file.
such as: Bytearrayoutputstream byteout=new bytearrayoutputstream ();
FileInputStream fileInput = new FileInputStream (ZipFile);
byte [] byte_i=new byte[1024];
int num_i=0;
while ((Num_i= (Fileinput.read (byte_i))) >-1) {
Byteout.write (byte_i,0,num_i);
}
byte [] Byte_z=byteout.tobytearray ();
Fileinput.close ();
Byteout.close ();
5 A byte array is obtained after reading the file in zip format. Can be written by response. This is where you set some parameters.
such as: Resp.setcontenttype ("Application/zip");
Resp.setheader ("Content-disposition", "Attachment;filename=" +zipfile.getname () + "");
OutputStream FileOutputStream = Resp.getoutputstream ();
Fileoutputstream.write (byte_z);
Fileoutputstream.flush ();
Fileoutputstream.close ();
This gives you the ability to get a file in zip format. On the foreground screen. But remember. is directly called through the response of the foreground.
6 The method that calls the background directly in the foreground page. Written by JSP syntax. Like the export.jsp.
such as: <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
New Com.service.DemoService (). Exportservice (Request,response);
%>
In this case, you need to invoke the JSP file to be exported in a JSP in the JS method.
such as: var Infowindow = window.open ("export.jsp", "_blank", "width=500,height=400");
A complete example. As follows:
Front Desk JS Method:
function Export_onclick () {
var Infowindow = window.open ("export.jsp", "_blank", "width=500,height=400");
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.