java ajaxsubmit_java上传文件功能,通过ajaxSubmit获取返回值

java ajaxsubmit_java上传文件功能,通过ajaxSubmit获取返回值1importorg.slf4j.Logger;2importorg.slf4j.LoggerFactory;34importjava.io.*;5importjava.nio.charset.Charset;6importjava.util.Enumeration;7importjava.util.List;8importjava.util.zip.ZipEntry;9impo…

大家好,欢迎来到IT知识分享网。java ajaxsubmit_java上传文件功能,通过ajaxSubmit获取返回值

1 importorg.slf4j.Logger;2 importorg.slf4j.LoggerFactory;3

4 import java.io.*;5 importjava.nio.charset.Charset;6 importjava.util.Enumeration;7 importjava.util.List;8 importjava.util.zip.ZipEntry;9 importjava.util.zip.ZipFile;10 importjava.util.zip.ZipOutputStream;11

12 public classZipUtils {13 private static final int BUFFER_SIZE = 2 * 1024;14 private static final Logger LOGGER = LoggerFactory.getLogger(ZipUtils.class);15 /**

16 * zip解压17 *@paramsrcFile zip源文件18 *@paramdestDirPath 解压后的目标文件夹19 *@throwsRuntimeException 解压失败会抛出运行时异常20 */

21 public static void unZip(File srcFile, String destDirPath) throwsRuntimeException {22 long start =System.currentTimeMillis();23 //判断源文件是否存在

24 if (!srcFile.exists()) {25 throw new RuntimeException(srcFile.getPath() + “所指文件不存在”);26 }27 //开始解压

28 ZipFile zipFile = null;29 try{30 zipFile = new ZipFile(srcFile, Charset.forName(“gbk”));31 Enumeration> entries =zipFile.entries();32 while(entries.hasMoreElements()) {33 ZipEntry entry =(ZipEntry) entries.nextElement();34 System.out.println(“解压” +entry.getName());35 //如果是文件夹,就创建个文件夹

36 if(entry.isDirectory()) {37 String dirPath = destDirPath + “/” +entry.getName();38 File dir = newFile(dirPath);39 dir.mkdirs();40 } else{41 //如果是文件,就先创建一个文件,然后用io流把内容copy过去

42 File targetFile = new File(destDirPath + “/” +entry.getName());43 //保证这个文件的父文件夹必须要存在

44 if(!targetFile.getParentFile().exists()){45 targetFile.getParentFile().mkdirs();46 }47 targetFile.createNewFile();48 //将压缩文件内容写入到这个文件中

49 InputStream is =zipFile.getInputStream(entry);50 FileOutputStream fos = newFileOutputStream(targetFile);51 intlen;52 byte[] buf = new byte[BUFFER_SIZE];53 while ((len = is.read(buf)) != -1) {54 fos.write(buf, 0, len);55 }56 //关流顺序,先打开的后关闭

57 fos.close();58 is.close();59 }60 }61 long end =System.currentTimeMillis();62 LOGGER.info(“解压完成,耗时:” + (end – start) +” ms”);63 } catch(Exception e) {64 LOGGER.error(e.getMessage());65 } finally{66 if(zipFile != null){67 try{68 zipFile.close();69 } catch(IOException e) {70 e.printStackTrace();71 }72 }73 }74 }75

76 /**

77 * 压缩成ZIP 方法78 *@paramsrcFiles 需要压缩的文件列表79 *@paramout 压缩文件输出流80 *@throwsRuntimeException 压缩失败会抛出运行时异常81 */

82 public static void toZip(List srcFiles , OutputStream out)throwsException {83 long start =System.currentTimeMillis();84 ZipOutputStream zos = null;85 try{86 zos = newZipOutputStream(out);87 for(File srcFile : srcFiles) {88 byte[] buf = new byte[BUFFER_SIZE];89 zos.putNextEntry(newZipEntry(srcFile.getName()));90 intlen;91 FileInputStream in = newFileInputStream(srcFile);92 while ((len = in.read(buf)) != -1){93 zos.write(buf, 0, len);94 }95 zos.closeEntry();96 in.close();97 }98 long end =System.currentTimeMillis();99 LOGGER.info(“解压完成,耗时:” + (end – start) +” ms”);100 } catch(Exception e) {101 LOGGER.error(e.getMessage());102 }finally{103 if(zos != null){104 try{105 zos.close();106 } catch(Exception e) {107 e.printStackTrace();108 }109 }110 }111 }112 }

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22364.html

(0)
上一篇 2024-01-05 20:15
下一篇 2024-01-06 10:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信