首页 > 代码库 > 打jar包
打jar包
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 chirs@zhoujin.com (Chirs Chou)
*/
public class ColorServlet extends HttpServlet{
public void init(ServletConfig config){
System.out.println("初始化");
}
/**
* 应用于文件下载接口
*
* @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);// 写文件
bw.flush();
bw.close();
//ZipUtil.addEntry(fzip,"index.html3243",f,new File(newZipPath));
ZipUtil.addEntries(fzip, entries, new File(newZipPath));
//开始下载新的zip包
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();
}
/**
* 通过响应输出流实现文件下载
*
* @param response 响应的请求
* @param fileLocal 文件的绝对路径 请用/斜杠表示路径
* @param downloadName 自定义的文件名 ( 不要后缀),如果此值为空则使用时间日期做为默认的文件名
* @param deleFile 下载完成后是否删除文件(true: 删除 , false:不删除)
*/
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){
}
}
}
}
/**
* 获得指定文件的byte数组
*/
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;
}
/**
* 根据byte数组,生成文件
*/
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()){//判断文件目录是否存在
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();
}
}
}
}
}
打jar包