| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- package cn.com.goldenwater.dcproj.utils.impexcel;
- import cn.com.goldenwater.dcproj.model.GwComFile;
- import cn.com.goldenwater.id.util.UuidUtil;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- /**
- * Created by jinshui on 2019/11/1.
- */
- public class FileUtil {
- private FileUtil() {
- }
- private final static String PDF = "pdf";
- private final static String DOC = "doc";
- private final static String DOCX = "docX";
- private final static String HTML = "html";
- //压缩包列表
- private final static String[] CP = {"zip", "rar"};
- /**
- * 根据上传文件 得到文件列表
- *
- * @param file
- * @return
- */
- public static List<GwComFile> getFileList(MultipartFile file, String prefix, String fileDir) throws Exception {
- List<GwComFile> list = new ArrayList<>();
- //获取 文件名.格式
- String originalFilename = file.getOriginalFilename();
- String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
- if (StringUtils.isBlank(ext)) {
- return new ArrayList<>();
- }
- Calendar dat = Calendar.getInstance();
- String yearPath = dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH);
- fileDir = fileDir + yearPath;
- prefix = prefix + yearPath;
- int index = isZip(ext);
- //是否是压缩包
- if (index < 0) {
- //单个文件
- GwComFile gwComFile = getGwComFile(file.getName(), (double) file.getSize(), prefix);
- FileUtils.copyInputStreamToFile(file.getInputStream(), new File(fileDir, gwComFile.getId() + "." + gwComFile.getFileExt()));
- list.add(gwComFile);
- return list;
- }
- switch (index) {
- case 0:
- list.addAll(dealZipFile(file, prefix, fileDir));
- break;
- case 1:
- break;
- case 2:
- break;
- case 3:
- break;
- }
- return list;
- }
- private static List<GwComFile> dealZipFile(MultipartFile file, String prefix, String fileDir) throws IOException {
- List<GwComFile> list = new ArrayList<>();
- ZipInputStream zpIs = new ZipInputStream(file.getInputStream(), Charset.forName(getFilecharset(file)));
- ZipEntry ze = null;
- String path = fileDir;
- File pathFile = new File(path);
- if (!pathFile.exists()) {
- pathFile.mkdirs();
- }
- while ((ze = zpIs.getNextEntry()) != null) {
- if (ze.isDirectory() || ze.getName().endsWith("/")) {
- //暂时不处理压缩包里有文件夹这种形式
- continue;
- }
- GwComFile gwComFile = getGwComFile(ze.getName(), (double) ze.getSize(), prefix);
- ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
- byte[] byte_s=new byte[1024];
- int num=-1;
- while((num=zpIs.read(byte_s,0,byte_s.length))>-1) {//通过read方法来读取文件内容
- byteArrayOutputStream.write(byte_s, 0, num);
- }
- File outFile = new File(fileDir, gwComFile.getId() + "." + gwComFile.getFileExt());
- FileOutputStream fileOutputStream = new FileOutputStream(outFile);
- fileOutputStream.write(byteArrayOutputStream.toByteArray());
- list.add(gwComFile);
- byteArrayOutputStream.close();
- fileOutputStream.close();
- }
- zpIs.close();
- return list;
- }
- private static int isZip(String ext) {
- for (int i = 0; i < CP.length; i++) {
- if (CP[i].equalsIgnoreCase(ext)) {
- return i;
- }
- }
- return -1;
- }
- public static GwComFile getGwComFile(String fileName, double size, String prefix) throws IOException {
- GwComFile gwComFile = new GwComFile();
- gwComFile.setId(UuidUtil.uuid());
- gwComFile.setCreateDate(Calendar.getInstance().getTime());
- gwComFile.setFileTitle(fileName);
- gwComFile.setFileName(fileName);
- gwComFile.setFileSize(size);
- gwComFile.setFileExt(fileName.substring(1 + fileName.lastIndexOf(".")));
- String filePath = prefix + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt();
- gwComFile.setFilePath(filePath);
- return gwComFile;
- }
- //判断编码格式方法
- public static String getFilecharset(MultipartFile file) {
- String charset = "GBK";
- byte[] first3Bytes = new byte[3];
- try {
- boolean checked = false;
- BufferedInputStream bis = new BufferedInputStream(file.getInputStream());
- bis.mark(0);
- int read = bis.read(first3Bytes, 0, 3);
- if (read == -1) {
- return charset; //文件编码为 ANSI
- } else if (first3Bytes[0] == (byte) 0xFF
- && first3Bytes[1] == (byte) 0xFE) {
- charset = "UTF-16LE"; //文件编码为 Unicode
- checked = true;
- } else if (first3Bytes[0] == (byte) 0xFE
- && first3Bytes[1] == (byte) 0xFF) {
- charset = "UTF-16BE"; //文件编码为 Unicode big endian
- checked = true;
- } else if (first3Bytes[0] == (byte) 0xEF
- && first3Bytes[1] == (byte) 0xBB
- && first3Bytes[2] == (byte) 0xBF) {
- charset = "UTF-8"; //文件编码为 UTF-8
- checked = true;
- }
- bis.reset();
- if (!checked) {
- int loc = 0;
- while ((read = bis.read()) != -1) {
- loc++;
- if (read >= 0xF0) {
- break;
- }
- if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
- {
- break;
- }
- if (0xC0 <= read && read <= 0xDF) {
- read = bis.read();
- if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
- {
- continue;
- } else {
- break;
- }
- } else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
- read = bis.read();
- if (0x80 <= read && read <= 0xBF) {
- read = bis.read();
- if (0x80 <= read && read <= 0xBF) {
- charset = "UTF-8";
- break;
- } else {
- break;
- }
- } else {
- break;
- }
- }
- }
- }
- bis.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return charset;
- }
- }
|