com.eryansky.modules.disk.utils.DiskUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.eryansky.modules.disk.utils.DiskUtils.java

Source

/**
 *  Copyright (c) 2012-2014 http://www.eryansky.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.eryansky.modules.disk.utils;

import com.eryansky.common.spring.SpringContextHolder;
import com.eryansky.common.utils.StringUtils;
import com.eryansky.common.utils.collections.Collections3;
import com.eryansky.common.utils.io.FileUtils;
import com.eryansky.common.web.utils.WebUtils;
import com.eryansky.core.security.SecurityUtils;
import com.eryansky.modules.disk.extend.IFileManager;
import com.google.common.collect.Maps;
import com.eryansky.core.security.SessionInfo;
import com.eryansky.core.web.upload.FileUploadUtils;
import com.eryansky.core.web.upload.exception.FileNameLengthLimitExceededException;
import com.eryansky.core.web.upload.exception.InvalidExtensionException;
import com.eryansky.modules.disk.entity.File;
import com.eryansky.modules.disk.entity.Folder;
import com.eryansky.modules.disk.entity._enum.FolderAuthorize;
import com.eryansky.modules.disk.service.DiskManager;
import com.eryansky.modules.sys.service.UserManager;
import com.eryansky.utils.AppConstants;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.io.*;

/**
 * ? ?
 *
 * @author &Eryan eryanwcp@gmail.com
 * @date 2014-12-10
 */
public class DiskUtils {

    protected static Logger logger = LoggerFactory.getLogger(DiskUtils.class);

    private static UserManager userManager = SpringContextHolder.getBean(UserManager.class);
    private static DiskManager diskManager = SpringContextHolder.getBean(DiskManager.class);
    private static IFileManager iFileManager = SpringContextHolder.getBean("iFileManager");
    /**
     *  
     */
    public static String FOLDER_NOTICE = "notice";
    /**
     * kindeditor
     */
    public static String FOLDER_KINDEDITOR = "kindeditor";
    /**
     * ?
     */
    public static String FOLDER_USER_PHOTO = "userphoto";
    /**
     * ???
     */
    public static final String UPLOAD_FAIL_MSG = "?";
    /**
     *  ?
     */
    public static final String FILE_VIRTUAL_PATH = "disk/file/";

    /**
     * KindEditor
     * @param userId
     * @return
     */
    public static String getKindEditorRelativePath(String userId) {
        Folder folder = new Folder();
        folder.setFolderAuthorize(FolderAuthorize.SysTem.getValue());
        folder.setCode(FOLDER_KINDEDITOR);
        return getDISKStoreDir(folder, userId);
    }

    /**
     * ?
     * @param userId ID
     * @return
     */
    public static String getUserPhotoRelativePath(String userId) {
        Folder folder = new Folder();
        folder.setFolderAuthorize(FolderAuthorize.SysTem.getValue());
        folder.setCode(FOLDER_USER_PHOTO);
        return getDISKStoreDir(folder, userId);
    }

    /**
     * 
     * @param folderCode ?
     * @param userId ID
     * @return
     */
    public static String getRelativePath(String folderCode, String userId) {
        Folder folder = new Folder();
        folder.setFolderAuthorize(FolderAuthorize.SysTem.getValue());
        folder.setCode(folderCode);
        return getDISKStoreDir(folder, userId);
    }

    /**
     *  ???
     *
     * @param folder
     *            
     * @return
     */
    public static String getRelativePath(Folder folder, String userId) {
        Date now = Calendar.getInstance().getTime();
        StringBuffer path = new StringBuffer();
        path.append(DateFormatUtils.format(now, "yyyy")).append(java.io.File.separator);
        String folderAuthorize = FolderAuthorize.getFolderAuthorize(folder.getFolderAuthorize()).toString()
                .toLowerCase();
        path.append(userId).append(java.io.File.separator).append(folderAuthorize).append(java.io.File.separator);
        if (FolderAuthorize.User.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getId());
        } else if (FolderAuthorize.Organ.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getOrganId()).append(java.io.File.separator).append(folder.getId());
        } else if (FolderAuthorize.Role.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getRoleId()).append(java.io.File.separator).append(folder.getId());
        } else if (FolderAuthorize.Public.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getId());
        } else if (FolderAuthorize.SysTem.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getCode());
        }
        return path.toString();
    }

    /**
     * ?
     * @param folder
     * @param userId
     * @return
     */
    public static String getDISKStoreDir(Folder folder, String userId) {
        String path = getRelativePath(folder, userId);
        com.eryansky.common.utils.io.FileUtils.checkSaveDir(path);
        return path;
    }

    /**
     * FTP
     * @param folder
     * @param userId
     * @return
     */
    public static String getFTPStoreDir(Folder folder, String userId) {
        Date now = Calendar.getInstance().getTime();
        String _S = "/";
        StringBuffer path = new StringBuffer();
        path.append(DateFormatUtils.format(now, "yyyy")).append(_S);
        String folderAuthorize = FolderAuthorize.getFolderAuthorize(folder.getFolderAuthorize()).toString()
                .toLowerCase();
        path.append(userId).append(_S).append(folderAuthorize).append(_S);
        if (FolderAuthorize.User.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getId());
        } else if (FolderAuthorize.Organ.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getOrganId()).append(_S).append(folder.getId());
        } else if (FolderAuthorize.Role.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getRoleId()).append(_S).append(folder.getId());
        } else if (FolderAuthorize.Public.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getId());
        } else if (FolderAuthorize.SysTem.getValue().equals(folder.getFolderAuthorize())) {
            path.append(folder.getCode());
        }
        return path.toString();
    }

    /**
     * ??? ? <br/>
     * ?
     *
     * @param code
     *            ?
     * @return
     */
    public static Folder getSystemFolderByCode(String code) {
        return diskManager.checkSystemFolderByCode(code);
    }

    public static Folder getSystemFolderByCode(String code, String userId) {
        return diskManager.checkSystemFolderByCode(code, userId);
    }

    /**
     * ?
     * @param userId
     * @return
     */
    public static Folder getUserNoticeFolder(String userId) {
        return diskManager.checkSystemFolderByCode(FOLDER_NOTICE, userId);
    }

    /**
     * ??
     * @param userId
     * @return
     */
    public static Folder getUserPhotoFolder(String userId) {
        return diskManager.checkSystemFolderByCode(FOLDER_USER_PHOTO, userId);
    }

    /**
     * ?KindEditor
     * @param userId
     * @return
     */
    public static Folder getUserKindEditorFolder(String userId) {
        return diskManager.checkSystemFolderByCode(FOLDER_KINDEDITOR, userId);
    }

    /**
     * ?
     *
     * @param folderCode
     *            ?
     * @param sessionInfo
     *            session? ??null
     * @param multipartFile
     *             SpringMVC
     * @return
     * @throws InvalidExtensionException
     * @throws FileUploadBase.FileSizeLimitExceededException
     * @throws FileNameLengthLimitExceededException
     * @throws IOException
     */
    public static File saveSystemFile(String folderCode, SessionInfo sessionInfo, MultipartFile multipartFile)
            throws InvalidExtensionException, FileUploadBase.FileSizeLimitExceededException,
            FileNameLengthLimitExceededException, IOException {
        String userId = null;
        if (sessionInfo != null && sessionInfo.getUserId() != null) {
            userId = sessionInfo.getUserId();
        }

        String code = FileUploadUtils.encodingFilenamePrefix(userId + "", multipartFile.getOriginalFilename());
        Folder folder = getSystemFolderByCode(folderCode, userId);
        String storeFilePath = iFileManager.getStorePath(folder, userId, multipartFile.getOriginalFilename());
        File file = new File();
        file.setFolder(folder);
        file.setCode(code);
        file.setUserId(userId);
        file.setName(multipartFile.getOriginalFilename());
        file.setFilePath(storeFilePath);
        file.setFileSize(multipartFile.getSize());
        file.setFileSuffix(FilenameUtils.getExtension(multipartFile.getOriginalFilename()));
        iFileManager.saveFile(file.getFilePath(), multipartFile.getInputStream(), true);
        diskManager.saveFile(file);
        return file;
    }

    /**
     * ?
     *
     * @param folderCode
     *            ?
     * @param sessionInfo
     *            session? ??null
     *
     * @param workbook
     * @param fileName
     *
     * @return
     * @throws InvalidExtensionException
     * @throws FileUploadBase.FileSizeLimitExceededException
     * @throws FileNameLengthLimitExceededException
     * @throws IOException
     */
    public static File saveExcelFile(String folderCode, SessionInfo sessionInfo, HSSFWorkbook workbook,
            String fileName) throws InvalidExtensionException, FileUploadBase.FileSizeLimitExceededException,
            FileNameLengthLimitExceededException, IOException {
        String userId = null;
        if (sessionInfo != null && sessionInfo.getUserId() != null) {
            userId = sessionInfo.getUserId();
        }

        String code = FileUploadUtils.encodingFilenamePrefix(userId + "", fileName);
        Folder folder = getSystemFolderByCode(folderCode, userId);
        String storeFilePath = iFileManager.getStorePath(folder, userId, fileName);
        File file = new File();
        file.setFolder(folder);
        file.setCode(code);
        file.setUserId(userId);
        file.setName(fileName);
        file.setFilePath(storeFilePath);
        file.setFileSize(Long.valueOf(workbook.getBytes().length));
        file.setFileSuffix(FilenameUtils.getExtension(fileName));
        FileOutputStream fileOut = new FileOutputStream(storeFilePath);
        workbook.write(fileOut);
        fileOut.close();
        //        iFileManager.saveFile(file.getFilePath(),multipartFile.getInputStream(), true);
        diskManager.saveFile(file);
        return file;
    }

    /**
     * ?
     *
     * @param sessionInfo
     * @param multipartFile
     * @return
     * @throws InvalidExtensionException
     * @throws FileUploadBase.FileSizeLimitExceededException
     * @throws FileNameLengthLimitExceededException
     * @throws IOException
     */
    public static File saveNoticeFile(SessionInfo sessionInfo, MultipartFile multipartFile)
            throws InvalidExtensionException, FileUploadBase.FileSizeLimitExceededException,
            FileNameLengthLimitExceededException, IOException {
        return saveSystemFile(DiskUtils.FOLDER_NOTICE, sessionInfo, multipartFile);
    }

    /**
     * 
     * @param file 
     * @return
     */
    public static void updateFile(File file) {
        diskManager.updateFile(file);
    }

    /**
     * 
     * @param fileId ID
     * @return
     */
    public static void deleteFile(String fileId) {
        Validate.notNull(fileId, "?[fileId]?null.");
        diskManager.deleteFile(fileId);
    }

    /**
     * 
     * @param file
     * @return
     */
    public static void deleteFile(File file) {
        Validate.notNull(file, "?[file]?null.");
        diskManager.deleteFile(file);
    }

    /**
     * ?
     * @param file
     * @return
     */
    public static String getVirtualFilePath(File file) {
        //        return AppConstants.getAdminPath() + "/" + FILE_VIRTUAL_PATH + file.getId();
        return AppConstants.getAdminPath() + "/" + FILE_VIRTUAL_PATH + file.getId() + "." + file.getFileSuffix();
    }

    public static java.io.File getDiskFile(String fileId) {
        if (fileId == null) {
            return null;
        }
        File file = diskManager.getFileById(fileId);
        return getDiskFile(file);
    }

    public static java.io.File getDiskFile(File file) {
        if (file == null || file.getId() == null) {
            return null;
        }
        //        String tempPath = AppConstants.getDiskTempDir() + java.io.File.separator + file.getCode();
        String tempPath = AppConstants.getDiskTempDir() + java.io.File.separator + file.getCode();
        java.io.File tempFile = new java.io.File(tempPath);
        try {
            if (file != null) {
                if (!tempFile.exists()) {
                    iFileManager.loadFile(file.getFilePath(), tempPath);
                }
                return tempFile;
            }
        } catch (IOException e) {
            logger.warn(String.format("%s?", file.getId()), e.getMessage());
        }
        return null;
    }

    //    

    public static final String ENCODING_DEFAULT = "UTF-8";

    public static final int BUFFER_SIZE_DIFAULT = 1024 * 8;

    public static void makeZip(List<File> inFiles, String zipPath) throws Exception {
        makeZip(inFiles, zipPath, ENCODING_DEFAULT);
    }

    public static void makeZip(List<File> inFiles, String zipPath, String encoding) throws Exception {
        ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));
        zipOut.setEncoding(encoding);
        doZipFile(zipOut, inFiles);
        zipOut.flush();
        zipOut.close();
    }

    /**
     * ????
     *
     * @param inFiles
     *            ?
     * @return
     * @throws Exception
     */
    private static void doZipFile(ZipOutputStream zipOut, List<File> inFiles) throws Exception {
        if (Collections3.isNotEmpty(inFiles)) {
            Map<String, Integer> countMap = Maps.newHashMap();

            for (File file : inFiles) {
                String name = file.getName();
                Integer mapVal = countMap.get(name);
                String newName = name;
                if (mapVal == null) {
                    mapVal = 0;
                } else {
                    mapVal++;
                    int index = name.lastIndexOf(".");
                    if (index > -1) {
                        newName = (new StringBuffer(name).insert(index, "(" + mapVal + ")")).toString();
                    } else {
                        newName = (new StringBuffer(name).append("(" + mapVal + ")")).toString();
                    }
                }
                if (file.getDiskFile().isFile()) {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file.getDiskFile()));
                    ZipEntry entry = new ZipEntry(newName);
                    zipOut.putNextEntry(entry);
                    byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
                    int size;
                    while ((size = bis.read(buff, 0, buff.length)) != -1) {
                        zipOut.write(buff, 0, size);
                    }
                    zipOut.closeEntry();
                    bis.close();
                }
                countMap.put(name, mapVal);

            }
        }
    }

    /**
     * 
     */
    public static void clearTempDir() {
        String tempDir = AppConstants.getDiskTempDir();
        java.io.File file = new java.io.File(tempDir);
        FileUtils.deleteFile(file.listFiles());

    }

    /**
     * ? ? + ? + ?
     * @param userId ID null,?ID
     * @return
     */
    public static boolean isDiskAdmin(String userId) {
        String _userId = userId;
        if (_userId == null) {
            SessionInfo sessionInfo = SecurityUtils.getCurrentSessionInfo();
            _userId = sessionInfo.getUserId();
        }

        boolean isAdmin = false;
        if (userManager.isSuperUser(_userId) || SecurityUtils.isPermittedRole(AppConstants.ROLE_SYSTEM_MANAGER)
                || SecurityUtils.isPermittedRole(AppConstants.ROLE_DISK_MANAGER)) {//? + ?
            isAdmin = true;
        }
        return isAdmin;
    }

    /**
     * 
     * @param request
     * @param response
     * @param inputStream ?
     * @param displayName ??
     * @throws IOException
     */
    public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream,
            String displayName) throws IOException {
        response.reset();
        WebUtils.setNoCacheHeader(response);
        String contentType = "application/x-download";
        if (StringUtils.isNotBlank(displayName)) {
            if (displayName.endsWith(".doc")) {
                contentType = "application/msword";
            } else if (displayName.endsWith(".docx")) {
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            } else if (displayName.endsWith(".xls")) {
                contentType = "application/vnd.ms-excel";
            } else if (displayName.endsWith(".xlsx")) {
                contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            } else if (displayName.endsWith(".ppt")) {
                contentType = "application/vnd.ms-powerpoint";
            } else if (displayName.endsWith(".pptx")) {
                contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
            } else if (displayName.endsWith(".pdf")) {
                contentType = "application/pdf";
            } else if (displayName.endsWith(".jpg") || displayName.endsWith(".jpeg")) {
                contentType = "image/jpeg";
            } else if (displayName.endsWith(".gif")) {
                contentType = "image/gif";
            } else if (displayName.endsWith(".bmp")) {
                contentType = "image/bmp";
            }
        }

        response.setContentType(contentType);
        response.setContentLength((int) inputStream.available());

        //        String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
        //        displayFilename = displayFilename.replace(" ", "_");
        WebUtils.setDownloadableHeader(request, response, displayName);
        BufferedInputStream is = null;
        OutputStream os = null;
        try {

            os = response.getOutputStream();
            is = new BufferedInputStream(inputStream);
            IOUtils.copy(is, os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}