javaweb压缩文件夹|如何使用java压缩文件夹成为zip包(最简单的

|

Ⅰ 把一个java的web项目做成exe文件

最最简单的方式:用rar等压缩工具把你的web项目目录压缩成一个可执行的压缩文件。web项目内的jre,数据库等都用相对路径写好,这样就可以了。

Ⅱ java web项目代码包含哪些文件夹

java zip 压缩文件以及整个文件夹目录 public static final String EXT = ".zip"; private static final String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符 private static final String PATH = "/"; private static final int BUFFER = 1024; /** * 压缩 * * @param srcPath 需要压缩的文件(目录) 的路径 * @throws Exception */ public static void compress(String srcPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile); } /** * 压缩 * * @param srcFile 需要压缩的文件(目录) 的路径流 * @throws Exception */ public static void compress(File srcFile) throws Exception { String name = srcFile.getName(); String basePath = srcFile.getParent(); String destPath = basePath +"/"+ name + EXT; compress(srcFile, destPath); } /** * 压缩 * * @param srcFile * 源路径 * @param destPath * 目标路径 * @throws Exception */ public static void compress(File srcFile, File destFile) throws Exception { // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); compress(srcFile, zos, BASE_DIR); zos.flush(); zos.close(); } /** * 压缩文件 * * @param srcFile * @param destPath * @throws Exception */ public static void compress(File srcFile, String destPath) throws Exception { compress(srcFile, new File(destPath)); } /** * 压缩 * * @param srcFile * 源路径 * @param zos * ZipOutputStream * @param basePath * 压缩包内相对路径 * @throws Exception */ private static void compress(File srcFile, ZipOutputStream zos, String basePath) throws Exception { if (srcFile.isDirectory()) { compressDir(srcFile, zos, basePath); } else { compressFile(srcFile, zos, basePath); } } /** * 文件压缩 * * @param srcPath * 源文件路径 * @param destPath * 目标文件路径 * */ public static void compress(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile, destPath); } /** * 压缩目录 * * @param dir * @param zos * @param basePath * @throws Exception */ private static void compressDir(File dir, ZipOutputStream zos, String basePath) throws Exception { File[] files = dir.listFiles(); // 构建空目录 if (files.length < 1) { ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH); zos.putNextEntry(entry); zos.closeEntry(); } for (File file : files) { // 递归压缩 compress(file, zos, basePath + dir.getName() + PATH); } } /** * 文件压缩 * * @param file * 待压缩文件 * @param zos * ZipOutputStream * @param dir * 压缩文件中的当前路径 * @throws Exception */ private static void compressFile(File file, ZipOutputStream zos, String dir) throws Exception { /** * 压缩包内文件名定义 * * <pre> * 如果有多级目录,那么这里就需要给出包含目录的文件名 * 如果用WinRAR打开压缩包,中文名将显示为乱码 * </pre> */ ZipEntry entry = new ZipEntry(dir + file.getName()); zos.putNextEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream( file)); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zos.write(data, 0, count); } bis.close(); zos.closeEntry(); } /** * 解压缩功能. * 将ZIP_FILENAME文件解压到ZIP_DIR目录下. * @throws Exception */ public static void upZipFile() throws Exception{ String ZIP_DIR="d:\\"; String ZIP_FILENAME="D:\\xxx.zip"; ZipFile zfile=new ZipFile(ZIP_FILENAME); Enumeration zList=zfile.entries(); ZipEntry ze=null; byte[] buf=new byte[1024]; while(zList.hasMoreElements()){ ze=(ZipEntry)zList.nextElement(); if(ze.isDirectory()){ File f=new File(ZIP_DIR+ze.getName()); f.mkdir(); continue; } OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(ZIP_DIR, ze.getName()))); InputStream is=new BufferedInputStream(zfile.getInputStream(ze)); int readLen=0; while ((readLen=is.read(buf, 0, 1024))!=-1) { os.write(buf, 0, readLen); } is.close(); os.close(); } zfile.close(); } /** * 给定根目录,返回一个相对路径所对应的实际文件名. * @param baseDir 指定根目录 * @param absFileName 相对路径名,来自于ZipEntry中的name * @return java.io.File 实际的文件 */ public static File getRealFileName(String baseDir, String absFileName){ String[] dirs=absFileName.split("/"); File ret=new File(baseDir); if(dirs.length>1){ for (int i = 0; i < dirs.length-1;i++) { ret=new File(ret, dirs[i]); } if(!ret.exists()) ret.mkdirs(); ret=new File(ret, dirs[dirs.length-1]); return ret; } return ret; }

Ⅲ 谁知道怎么把java web项目打包成exe可执行文件,要连数据库一起打包

先打包成jar,然后才能打包成exe的,不能直接打包成exe的

Ⅳ javaweb应用中,怎样压缩响应的html文件&n

Java Web总结(一)一、Servlet在应用中的作用①Servlet是Java Web中MVC模式中的C部分,即控制部分,下面展示一下其所处的位置和整个客户端(浏览器)和服务器端的交互过程。(如图1)Web总结(一)" TITLE="Java Web总结(一)" /> 图1②由图1可以看到浏览器通过请求将其数据提交到Servlet中,然后Servlet进行控制,即Servlet充当调度员的角色,其作用主要集中在三个方面:接收数据、调用业务逻辑、对客户端做出简单的响应。③然后Servlet将要继续处理的数据提交给JavaBean,然后JavaBean进行验证,然后如果涉及到数据库的操作,就会继续用到DAO(数据访问模型),然后调用数据库中间件JDBC进行数据库的相关操作,然后到数据库中访问数据。二、实现整个过程①为了实现整个过程,首先编写一个html(jsP)前台页面,如图2:Web总结(一)" TITLE="Java Web总结(一)" />图2代码如下:<html><head><title>系统登陆</title></head><body><p> </p><p> </p><p> </p><div align="center"><br><br><div style="margin:0 auto; width:230px;margin-top:70px;"><form id="login" action="/JavaWeb/loginServlet" method="post"><fieldset><legend>用户登陆</legend><br/><div>用户:<input name="username" id="username"/></div><br/><div>密码:<input name="password" id="pwd"/></div><br/></fieldset><div style="text-align: center;margin: 20px;"><input type="submit" value="提交"/><input type="reset" value="重置"/></div></form></div></div></body></html>②由上面的代码可以看到将<form>中的内容提交到/JavaWeb/loginServlet这个Servlet,这里涉及到了配置web.xml配置文件,只需要在配置文件中加入如下的部分:<servlet><servlet-name>loginServlet</servlet-name><servlet-class>cn.e.nwsuaf.cie.group4.servlet.LoginServlet</servlet-class><load-on-startup>0</load-on-startup>//在开启服务时,就调用init方法</servlet><servlet-mapping><servlet-name>loginServlet</servlet-name><url-pattern>/loginServlet</url-pattern></servlet-mapping>③Servlet中的代码如下所示package cn.e.nwsuaf.cie.group4.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.e.nwsuaf.cie.group4.service.LoginService;public class LoginServlet extends HttpServlet{private String userName;private String password;@Overridepublic void init() throws ServletException {// TODO Auto-generated method stubSystem.out.println("init….");}@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubuserName = req.getParameter("username");password = req.getParameter("password");LoginService loginService = new LoginService();boolean flag = loginService.validate(userName,password);if (flag){req.getRequestDispatcher("index.jsp").forward(req, resp);}else{req.getRequestDispatcher("login.jsp").forward(req, resp);}}@Overridepublic void destroy() {// TODO Auto-generated method stubSystem.out.println("destroy…");}}然后对上面的代码中的一部分进行解释。这个类继承了HttpServlet,然后并对其中的init()、destroy()、service()方法进行重写,其中Servlet是单例的(设计模式中的singleton model),因为所有的客户端自服务器开启并对servlet进行初始化以后,所有的客户端都是调用这里边的同一个对象,直到调用destroy方法销毁之。

Ⅳ javaweb如何实现上传整个文件夹,并存到数据库中.求方法…弄出压缩文件在上传的就不要发炎了…

不知道你数据多大,一般没人把文件夹存到数据库,如果真想这么干,明确告诉你必须压缩成文件,数据库存储的只能是一般的二进制数据,文件夹是受操作系统影响的,不同操作系统对文件夹的管理方式都不一样。

Ⅵ 如何使用java压缩文件夹成为zip包(最简单的

import java.io.File;

public class ZipCompressorByAnt {private File zipFile;/*** 压缩文件构造函数* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip*/public ZipCompressorByAnt(String finalFile) {zipFile = new File(finalFile);}/*** 执行压缩操作* @param srcPathName 需要被压缩的文件/文件夹*/public void compressExe(String srcPathName) {System.out.println("srcPathName="+srcPathName);File srcdir = new File(srcPathName);if (!srcdir.exists()){throw new RuntimeException(srcPathName + "不存在!");}Project prj = new Project();Zip zip = new Zip();zip.setProject(prj);zip.setDestFile(zipFile);FileSet fileSet = new FileSet();fileSet.setProject(prj);fileSet.setDir(srcdir);//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹 eg:zip.setIncludes("*.java");//fileSet.setExcludes(…); //排除哪些文件或文件夹zip.addFileset(fileSet);zip.execute();}

}

public class TestZip {public static void main(String[] args) {ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\test1.zip ");zca.compressExe("E:\test1");} }

/*如果 出现ant 的 52 51 50 等版本问题 可以去找对应的ant-1.8.2.jar 我开始用的ant-1.10.1.jar 就是这个包版本高了 一直报verson 52 版本问题*/

Ⅶ java怎样压缩文件夹

压缩文件夹代码:import java.io.File;import org.apache.tools.zip.ZipOutputStream; //这个包在ant.jar里,要到官方网下载import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.ZipInputStream;import java.util.zip.ZipEntry;public class CompressBook { public CompressBook() {} /* * inputFileName 输入一个文件夹 * zipFileName 输出一个压缩文件夹 */ public void zip(String inputFileName) throws Exception { String zipFileName = "c:\\test.zip"; //打包后文件名字 System.out.println(zipFileName); zip(zipFileName, new File(inputFileName)); } private void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); zip(out, inputFile, ""); System.out.println("zip done"); out.close(); } private void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { File[] fl = f.listFiles(); out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } }else { out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; System.out.println(base); while ( (b = in.read()) != -1) { out.write(b); } in.close(); } } public static void main(String [] temp){ CompressBook book = new CompressBook(); try { book.zip("c:\\c");//你要压缩的文件夹 }catch (Exception ex) { ex.printStackTrace(); } }}

Ⅷ java,web项目我想上传一个rar压缩文件,里面有个excel和图片。

apache有个org.apache.tools.zip包可以实现袭zip、rar文件压缩和解压,也支持加密。可以找找关于这个包的资料。另外j2se里的java.util.zip包可以实现zip文件的压缩和解压,但不支持rar文件,也不支持加密。

Ⅸ 怎么对一个javaWeb应用程序同时实施静态化和全站压缩呢

publicvoiddestroy(){}publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{//要想压缩页面就是当用户访问页面时在服务器把页面信息打给浏览器的时候进行压缩所以这时候就是对response进行包装MyResponsemyresponse=newMyResponse((HttpServletResponse)response);chain.doFilter(request,(ServletResponse)myresponse);//从包装的myresponse里获得写到容器流里的原始数据bsbyte[]bs=myresponse.getOut();System.out.println("压缩前>>>"+bs.length);//用原始的response响应用户,此response需要转换为HttpServletResponse类型HttpServletResponseresp=(HttpServletResponse)response;//response解决中文乱码问题resp.setContentType("text/html;charset=utf-8");//开始压缩ByteArrayOutputStreamout=newByteArrayOutputStream();GZIPOutputStreamzip=newGZIPOutputStream(out);zip.write(bs);zip.close();//获取压缩后的数据byte[]dest=out.toByteArray();System.out.println("压缩后>>>"+dest.length);//设置响应头resp.setDateHeader("content-length",dest.length);resp.setHeader("content-Encoding","gzip");resp.getOutputStream().write(dest);}publicvoidinit(Fi


赞 (0)