直接进入主题
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public void exportPropertiesFile(HttpServletResponse response){
// tempfile 是读取配置文件CDN上配置的路径
String docName = getNumUUID(6);
StringBuffer sb = new StringBuffer();
try {
Map<String,Object> params = new HashMap<>();
// 比如两层循环查询 先获取外层数据
List<Map> map = new ArrayList<>(); // 获取数据
//循环匹配将数据封装成JSON然后生成properties文件
for(Map lb : map){
sb.delete(0, sb.length()); // 使用时删除索引从begin开始(包含begin)到end(不包含end)的所有字符;
List<Map> map2 = new ArrayList<>(); // 另外的数据
for(Map lc : map2){
sb.append("#");
sb.append("顺便写");
sb.append("\n");
sb.append(convert("处英文以为的语言都要转unicode编码"));
sb.append("=");
// if(){
sb.append(convert("英文以外的语言需要转"));
// }else{
// sb.append("English does not turn");
// }
sb.append("\n");
}
String fileName = "lang_名称"+".properties";
//生成properties文件
createFile(tempfile, docName,fileName, sb.toString().getBytes());
}
String srcFilePath = tempfile+File.separator+docName;
String destFilePath = tempfile+File.separator+docName+".zip";
//将文件夹里面的文件打包压缩
compress(srcFilePath, destFilePath,response);
//输出文件流
// outputFile(response, tempfile,docName+".zip");
//删除文件夹和压缩文件
File srcFile = new File(srcFilePath);
// File destFile = new File(destFilePath);
delFile(srcFile);
// delFile(destFile);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 将字符串转成unicode
* @param str 待转字符串
* @return unicode字符串
*/
public static String convert(String str)
{
str = (str == null ? "" : str);
String tmp;
StringBuffer sb = new StringBuffer();
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++)
{
c = str.charAt(i);
sb.append("\\u");
j = (c >>>8); //取出高8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF);//取出低8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
}
return (new String(sb));
}
/**
* 生成文件
* @param filePath
* @param docName
* @param fileName
* @param content
*/
private void createFile(String filePath,String docName,String fileName,byte[] content){
FileOutputStream outStream = null;
File file = null;
try {
file = new File(filePath+File.separator+docName+File.separator+fileName);
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
outStream = new FileOutputStream(file,true);
outStream.write(content);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
outStream.close();
// if(file.exists()){
// file.delete();
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String getNumUUID(int n){
String temp="";
Random random = new Random();
for(int i=0;i<n;i++){
int num = random.nextInt(10);
temp+=num;
}
return temp;
}
/**
* 压缩文件
* @param srcFilePath 压缩源路径
* @param destFilePath 压缩目的路径
*/
public static void compress(String srcFilePath, String destFilePath, HttpServletResponse response) {
//
File src = new File(srcFilePath);
if (!src.exists()) {
throw new RuntimeException(srcFilePath + "不存在");
}
File zipFile = new File(destFilePath);
try {
// FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
String baseDir = "";
compressbyType(src, zos, baseDir);
zos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 删除文件
* @param file
*/
private boolean delFile(File file) {
if (!file.exists()) {
return false;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
delFile(f);
}
}
return file.delete();
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
* @param src
* @param zos
* @param baseDir
*/
private static void compressbyType(File src, ZipOutputStream zos,String baseDir) {
if (!src.exists())
return;
// System.out.println("压缩路径" + baseDir + src.getName());
//判断文件是否是文件,如果是文件调用compressFile方法,如果是路径,则调用compressDir方法;
if (src.isFile()) {
//src是文件,调用此方法
compressFile(src, zos, baseDir);
} else if (src.isDirectory()) {
//src是文件夹,调用此方法
compressDir(src, zos, baseDir);
}
}
/**
* 压缩文件
*/
private static void compressFile(File file, ZipOutputStream zos,String baseDir) {
if (!file.exists())
return;
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zos.putNextEntry(entry);
int count;
byte[] buf = new byte[1024];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 压缩文件夹
*/
private static void compressDir(File dir, ZipOutputStream zos,String baseDir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
if(files.length == 0){
try {
zos.putNextEntry(new ZipEntry(baseDir + dir.getName()+File.separator));
} catch (IOException e) {
e.printStackTrace();
}
}
for (File file : files) {
compressbyType(file, zos, baseDir + dir.getName() + File.separator);
}
}