Hit Jar bag

Source: Internet
Author: User

Package com.servlet;
Import Java.io.BufferedOutputStream;
Import Java.io.BufferedWriter;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.URLEncoder;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.zeroturnaround.zip.ByteSource;
Import Org.zeroturnaround.zip.FileSource;
Import Org.zeroturnaround.zip.ZipEntrySource;
Import Org.zeroturnaround.zip.ZipUtil;
/**
* @author [email protected] (Chirs Chou)
*/
public class Colorservlet extends httpservlet{
public void init (ServletConfig config) {
SYSTEM.OUT.PRINTLN ("Initialize");
}

/**
* Applied to the file download interface
*
* @param request
* @param response
*
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
protected void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception, ioexception{
String Rootpath=colorservlet.class.getclassloader (). GetResource ("/"). GetPath ();
Downloadzip (Request,response);
}
private void Downloadzip (HttpServletRequest request,httpservletresponse response) {
String rootpath=request.getsession (). Getservletcontext (). Getrealpath ("/web-inf");
System.out.print ("----" +rootpath);
String colorstr = request.getparameter ("Color")!=null?request.getparameter ("Color"): "";
File Findex = new file (rootpath+ "/data/index.html");
File F = new file (rootpath+ "/data/config.css");
File Fzip = new file (rootpath+ "/data/demo.zip");
String Newzippath = rootpath+ "/data/new.zip";
BufferedWriter bw;
try {
CreateFile (f);
zipentrysource[] entries = new zipentrysource[] {
New Filesource ("index.html", Findex),
New Bytesource ("Config.css", GetBytes (rootpath+ "/data/config.css"))
};

Java.io.FileOutputStream Writerstream = new Java.io.FileOutputStream (f);
BW = new BufferedWriter (new Java.io.OutputStreamWriter (Writerstream, "UTF-8"));
Bw.write (COLORSTR);//write file
Bw.flush ();
Bw.close ();
Ziputil.addentry (Fzip, "index.html3243", F,new File (Newzippath));
Ziputil.addentries (Fzip, entries, new File (Newzippath));

Start downloading a new zip package
DownLoadFile (Response,newzippath, "Nowcolor.zip", false);
} catch (Exception e) {
E.printstacktrace ();
}
}
protected void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception, ioexception{
System.out.print ("doPost------");
Downloadzip (Request,response);
}


public static void MakeDir (File dir) {
if (! Dir.getparentfile (). exists ()) {
MakeDir (Dir.getparentfile ());
}
Dir.mkdir ();
}

public static Boolean createFile (file file) throws IOException {
if (! file.exists ()) {
MakeDir (File.getparentfile ());
}
return File.createnewfile ();
}


/**
* File download via response output stream
*
* @param Request for response response
* @param the absolute path of the filelocal file, use the/slash to indicate the path
* @param downloadname a custom file name (no suffix), if this value is blank then use the time date as the default file name
* @param delefile Delete files after download is complete (true: Delete, false: Do not delete)
*/
public void DownLoadFile (HttpServletResponse response,string filelocal,string Downloadname,boolean delefile) {
InputStream In=null;
OutputStream Out=null;
try{
if (! "". Equals (Downloadname)) {
Downloadname=downloadname+filelocal.substring (Filelocal.lastindexof ("."));
}else{
Downloadname=filelocal.substring (Filelocal.lastindexof ("/") +1);
}
Response.setheader ("Content-disposition", "Attachment;filename=" +urlencoder.encode (DownloadName, "UTF-8"));
In=new FileInputStream (filelocal);
int len=0;
byte buffer[]=new byte[1024];
Out=response.getoutputstream ();
while ((Len=in.read (buffer)) >0) {
Out.write (Buffer,0,len);
}
}catch (Exception e) {
E.printstacktrace ();
}finally{
if (in!=null) {
try{
//
In.close ();
if (delefile) {
Thread.Sleep (1000l);
File File=new file (filelocal);
File.delete ();
}
}catch (Exception e) {
}
}
}
}

/**
* Gets the byte array of the specified file
*/
Private byte[] GetBytes (String filePath) {
byte[] buffer = NULL;
try {
File File = new file (FilePath);
FileInputStream fis = new FileInputStream (file);
Bytearrayoutputstream BOS = new Bytearrayoutputstream (1000);
Byte[] B = new byte[1000];
int n;
while ((n = fis.read (b))! =-1) {
Bos.write (b, 0, N);
}
Fis.close ();
Bos.close ();
Buffer = Bos.tobytearray ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return buffer;
}

/**
* Based on byte array, generate file
*/
public static void GetFile (byte[] bfile, String filepath,string fileName) {
Bufferedoutputstream BOS = NULL;
FileOutputStream fos = null;
File file = null;
try {
File dir = new file (FilePath);
if (!dir.exists () &&dir.isdirectory ()) {//Determine if the file directory exists
Dir.mkdirs ();
}
File = new file (filepath+ "\ \" +filename);
FOS = new FileOutputStream (file);
BOS = new Bufferedoutputstream (FOS);
Bos.write (bfile);
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (BOS! = NULL) {
try {
Bos.close ();
} catch (IOException E1) {
E1.printstacktrace ();
}
}
if (fos! = null) {
try {
Fos.close ();
} catch (IOException E1) {
E1.printstacktrace ();
}
}
}
}


}

Hit Jar bag

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.