短UUID
java代码
public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
/**
* 9位随机数
*/
public static String generateShortUuid()
{
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++)
{
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
shortBuffer.append(chars[(int) (Math.random() * chars.length)]);
return shortBuffer.toString();
}
python代码
#uuid4,可以换成uuid1
from uuid import uuid4
uuidChars = ("a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z")
def short_uuid():
uuid = str(uuid4()).replace('-', '')
result = ''
for i in range(0,8):
sub = uuid[i * 4: i * 4 + 4]
x = int(sub,16)
result += uuidChars[x % 0x3E]
return result
print(short_uuid())
print(short_uuid())
print(short_uuid())
解压 压缩
/**
* 解压压缩
* @author
*
*/
public class UZipFileUtil {
private static final int BUFFER_SIZE = 2048;
/**
* 解压到指定目录
* @param zipPath 需要解压zip路径
* @param descDir 解压后放的路径
* @throws IOException
*/
public static void unZipFiles(String zipPath, String descDir) throws IOException
{
unZipFiles(new File(zipPath), descDir);
}
/**
* 解压到指定目录
* @param zipFile 需要解压zip文件
* @param descDir 解压后放的路径
* @throws IOException
*/
public static void unZipFiles(File zipFile, String descDir) throws IOException
{
// 判断源文件是否存在
if (!zipFile.exists()) {
throw new RuntimeException(zipFile.getPath() + "所指文件不存在");
}
File pathFile = new File(descDir);
if (!pathFile.exists())
{
pathFile.mkdirs();
}
ZipFile zip = null;
try {
// 解决zip文件中有中文目录或者中文文件
zip = new ZipFile(zipFile, Charset.forName("GBK"));
Enumeration<?> entries = zip.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = descDir + File.separator + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(descDir + File.separator + entry.getName());
// 保证这个文件的父文件夹必须要存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zip.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
}catch (Exception e){
e.printStackTrace();
}finally{
if(zipFile != null){
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩成ZIP 方法
* @param srcFiles 需要压缩的文件列表
* @param out 压缩文件输出流
* @throws
*/
public static void toZip(List<File> srcFiles , OutputStream out)throws Exception {
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(zos != null){
try {
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩文件
* @param sourceFolder 压缩文件夹
* @param zipFilePath 压缩文件
*/
public static void zip(String sourceFolder, String zipFilePath)
{
try (OutputStream os = new FileOutputStream(zipFilePath);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos, StandardCharsets.UTF_8))
{
File file = new File(sourceFolder);
String basePath = file.isDirectory() ? file.getPath() : file.getParent();
zipFile(file, basePath, zos);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 递归压缩文件
* @param parentFile
* @param basePath
* @param zos
* @throws Exception
*/
private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception
{
File[] files = new File[0];
if (parentFile.isDirectory())
{
files = parentFile.listFiles();
}
else
{
files = new File[1];
files[0] = parentFile;
}
if (files == null)
{
return;
}
for (File file : files)
{
if (file.isDirectory())
{
String pathName = file.getPath().substring(basePath.length() + 1) + File.separator;
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
continue;
}
String pathName = file.getPath().substring(basePath.length() + 1);
try (InputStream is = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(is))
{
zos.putNextEntry(new ZipEntry(pathName));
IOUtils.copy(bis, zos);
}
}
}
}
测试
public class ZipFileTest01 {
public static void main(String[] args) {
//解压
// try {
// UZipFileUtil.unZipFiles("D:\\wlr\\zips\\csdc.zip", "D:\\wlr\\zips");
//
// } catch (IOException e) {
// e.printStackTrace();
// }
//压缩
UZipFileUtil.zip("D:\\wlr\\zips\\wlr1", "D:\\wlr\\zips\\wlr2.zip");
}
}
导出 导入
导出
easypoi导出
poi导出
package com.wlr.poi.utils;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 导出
* @author
*
*/
public class ExcelExportUtils {
/**
* 导出excel
* @param sheetName sheet名称
* @param columns 参数
* @param data 数据
* @return
* @throws IOException
*/
public static byte[] exportExcel(String sheetName, String[] columns, List<Object[]> data) throws IOException
{
//创建Excel工作薄对象
Workbook workbook = new XSSFWorkbook();
//创建Excel工作表对象
Sheet sheet = workbook.createSheet(sheetName);
//创建行的单元格,从0开始
Row r0 = sheet.createRow(0);
//设置表头
for (int i = 0; i < columns.length; i++)
{
//创建单元格
Cell cell = r0.createCell(i);
cell.setCellValue(columns[i]);
}
int i = 1;
for (Object[] obj : data)
{
Row row = sheet.createRow(i);
for (int j = 0; j < obj.length; j++)
{
//创建单元格样式
CellStyle cellStyle = workbook.createCellStyle();
//创建单元格
Cell cell = row.createCell(j);
cell.setCellStyle(cellStyle);
cell.setCellValue(String.valueOf(obj[j]));
}
i++;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
return bos.toByteArray();
}
/**
* 根据byte数组,生成文件
* @param bfile byte字节
* @param filePath 路径
* @param fileName 文件名
*/
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 + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
bos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试
public class ExportTest01 {
public static void main(String[] args) {
List<SysUser> users = setUsers();
List<Object[]> objList = new ArrayList<>();
for (SysUser tScoreVO : users)
{
Object[] objs = new Object[3];
objs[0] = stringNull(tScoreVO.getName());
objs[1] = stringNull(tScoreVO.getSex());
objs[2] = stringNull(tScoreVO.getEge());
objList.add(objs);
}
try {
byte[] b = ExcelExportUtils.exportExcel("基础用户", new String[]{"姓名","性别","年龄"}, objList);
ExcelExportUtils.getFile(b,"D:\\wlr","wlr.xlsx");
} catch (IOException e) {
e.printStackTrace();
}
}
//判空
public static String stringNull(Object obj)
{
return obj == null ? "" : obj.toString();
}
//取值
public static List<SysUser> setUsers(){
List<SysUser> sysUsers = new ArrayList<SysUser>();
SysUser sysUser = null;
for (int i = 0; i < 10; i++) {
sysUser = new SysUser();
sysUser.setName("wlr"+i);
if (i%2 != 0) {
sysUser.setSex("男");
}else {
sysUser.setSex("女");
}
sysUser.setEge(i + 10);
sysUsers.add(sysUser);
}
return sysUsers;
}
}
导入
easypoi导入
poi导入
public class ImportExcelText01 {
public static void main(String[] args) {
try {
List<SysUser> sysUsers = importExcel();
sysUsers.forEach(f-> System.out.println(f.toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<SysUser> importExcel() throws Exception{
Workbook workbook = WorkbookFactory.create(new FileInputStream(new File("D:\\wlr\\wlr.xlsx")));
Sheet sheet = null;
List<SysUser> list = new ArrayList<SysUser>();
SysUser user = null;
for(int i = 0;i < workbook.getNumberOfSheets();i++){
//获取每个sheet
sheet = workbook.getSheetAt(i);
//getPhysicalNumberOfRows获取有记录的行数
for(int j = 0;j < sheet.getPhysicalNumberOfRows();j++){
Row row = sheet.getRow(j);
if(null!=row){
//getLastCellNum获取最后一列
user = new SysUser();
for(int k = 0;k < row.getLastCellNum();k++){
if(null!=row.getCell(k)){
if(k==0){
Cell cell = row.getCell(0);
//cell->string
user.setName(cell.getStringCellValue().toString());
}
if(k==1){
Cell cell = row.getCell(1);
//cell->string
user.setSex(cell.getStringCellValue().toString());
}
if(k==2){
Cell cell = row.getCell(2);
//cell->int
int eag = cell.getCellType();
user.setEge(eag);
}
}
}
list.add(user);
}
}
System.out.println("读取sheet表:"+ workbook.getSheetName(i) + "完成");
}
return list;
}
}