天天看點

java web 打包下載下傳_java web實作多檔案的打包下載下傳

// 建立檔案夾

private File createDir(String path) {

File dirFile = null;

try {

dirFile = new File(path);

if (!(dirFile.exists()) && !(dirFile.isDirectory())) {

dirFile.mkdirs();

}

} catch (Exception e) {

e.printStackTrace();

}

return dirFile;

}

// 建立檔案

private File createFile(String path) {

File file = new File(path);

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

return file;

}

// 删除檔案

public boolean delFile(String path) throws Exception {

boolean result = false;

File file=new File(path);

if(file.exists()&&file.isFile())

{

file.delete();

result = true;

}

return result;

}

// 删除檔案及檔案夾

private boolean delDir(File folder) {

boolean result = false;

try {

String childs[] = folder.list();

if (childs == null || childs.length <= 0) {

if (folder.delete()) {

result = true;

}

} else {

for (int i = 0; i < childs.length; i++) {

String childName = childs[i];

String childPath = folder.getPath() + File.separator

+ childName;

File filePath = new File(childPath);

if (filePath.exists() && filePath.isFile()) {

if (filePath.delete()) {

result = true;

} else {

result = false;

break;

}

} else if (filePath.exists() && filePath.isDirectory()) {

if (delDir(filePath)) {

result = true;

} else {

result = false;

break;

}

}

}

}

folder.delete();

} catch (Exception e) {

e.printStackTrace();

result = false;

}

return result;

}

public void zip(String zipFilePath, String inputFolderName)

throws Exception {

String zipFileName = zipFilePath; // 打包後檔案名字

File zipFile = new File(inputFolderName);

zip(zipFileName, zipFile);

}

private void zip(String zipFileName, File inputFolder) throws Exception {

FileOutputStream fileOut = new FileOutputStream(zipFileName);

ZipOutputStream out = new ZipOutputStream(fileOut);

zip(out, inputFolder, "");

out.close();

fileOut.close();

}

private void zip(ZipOutputStream out, File inputFolder, String base)

throws Exception {

if (inputFolder.isDirectory()) {

File[] fl = inputFolder.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(inputFolder);

int b;

//System.out.println(base);

while ((b = in.read()) != -1) {

out.write(b);

}

in.close();

}

}