文件上传工具类

package com.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

public class UploadFileUtils {
    //限制图片文件类型
    private static String[] imgFileTypes = {"image/bmp", "image/png", "image/gif", "image/jpeg",
            "image/jpg", "image/pjpeg", "image/pjpg", "image/x-png"};
    //限制图片文件大小
    private static long imgFileMaxSize = 1 * 1024 * 1024L;
    private static long imgFileMinSize = 0L;

    //限制视频文件类型
    private static String[] videoFileTypes = {"video/mp4", "application/x-shockwave-flash", "video/mpeg", "video/avi", "video/3gpp", "application/octet-stream"};
    //限制视频文件大小
    private static long videoFileMaxSize = 50 * 1024 * 1024L;
    private static long videoFileMinSize = 0L;

    //文件存放的目录
    public static String saveFileURL;

    //ffmpeg工具存放路径
    public static String ffmpegPath;
    //截图要存放的路径
    public static String imagePath;

    //服务协议
    public static String serverProtocol;
    //文件服务器端口
    public static String serverPort;
    //文件服务器IP地址
    private static String serverIP;
    private static String downloadAction;

    /**
     * 根据上传文件获取其真实读取路径
     *
     * @param request
     * @param file
     * @param fileName
     * @param path
     * @return
     * @throws IOException
     */
    //public static String setUploadFile(HttpServletRequest request,File file,String fileName,String path) throws IOException{
    public static String setUploadFile(HttpServletRequest request, File file, String fileName, String path) throws IOException {
        final Lock lock = new ReentrantLock();
        String newName = null;
        lock.lock();
        try {
            //加锁为防止文件名重复
            newName = System.currentTimeMillis()
                    + fileName.substring(fileName.lastIndexOf("."),
                    fileName.length());
        } finally {
            lock.unlock();
        }
        //------------ 锁结束 -------------
		/*String savePath = request.getSession().getServletContext().getRealPath("/")+ path.replaceAll("/", "\\\\") + "\\" + newName;
        System.out.println("savePath:" + savePath);
        savePath = savePath.substring(0, savePath.lastIndexOf("FamilySpace\\"))+ path.replaceAll("/", "\\\\") + "\\" + newName;
        String createPath = savePath.substring(0, savePath.lastIndexOf("\\"+newName));
        System.out.println("createPath:" + createPath);*/

        String savePath = path.replaceAll("/", "\\\\") + "\\" + newName;
        System.out.println("savePath:" + savePath);
        String createPath = savePath.substring(0, savePath.lastIndexOf("\\" + newName));
        System.out.println("createPath:" + createPath);
        File dirPath = new File(createPath);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        //获取文件输出流
        FileOutputStream fos = new FileOutputStream(savePath);
		/*String newFileName = request.getScheme() + "://"
		        + request.getServerName() + ":" + request.getServerPort()
		        + "/"+path+"/" + newName;*/
        String fileId = newName.substring(0, newName.lastIndexOf("."));

        String accessPath = request.getContextPath();
        String newFileName = serverProtocol + "://" + serverIP + ":" + serverPort + "/" + accessPath + downloadAction + "?fileId=" + fileId + "&name=" + fileName;

        System.out.println("downLoadURL-------->" + newFileName);
        byte[] buffer = new byte[1024];
        //获取内存中当前文件输入流
        InputStream in = new FileInputStream(file);
        try {
            int num = 0;
            while ((num = in.read(buffer)) > 0) {
                fos.write(buffer, 0, num);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            in.close();
            fos.close();
        }
        return newFileName;
    }

    /**
     * 获取视频截图
     *
     * @param ffmpegPath ffmpeg工具存放路径
     * @param videoPath  视频的存放路径
     * @param imagePath  截图要存放的路径
     * @return 截图名字(包括路径)
     */
    public static String getVideoScreenShot(String ffmpegPath, String videoPath, String imagePath) {
        ffmpegPath = ffmpegPath.replaceAll("/", "\\\\");
        videoPath = videoPath.replaceAll("/", "\\\\");
        imagePath = imagePath.replaceAll("/", "\\\\");
        final Lock lock = new ReentrantLock();
        String imageName = null;
        lock.lock();
        try {
            //加锁为防止文件名重复
            imageName = imagePath + Long.toString(System.currentTimeMillis()) + ".jpg";
        } finally {
            lock.unlock();
        }
        System.out.println("ffmpegPath--->" + ffmpegPath);
        System.out.println("videoPath--->" + videoPath);
        System.out.println("imagePath--->" + imageName);
        //String cmd="cmd /c start "+"D:\\file\\ffmpeg\\ffmpeg.exe -i D:\\file\\ffmpeg\\aa.3gp -ss 10 -vframes 1 -r 1 -ac 1 -ab 2 -s 320x240 -f image2 D:\\file\\ffmpeg\\test.jpg";
        String cmd = "cmd /c start " + ffmpegPath + " -i " + videoPath + " -ss 10 -vframes 1 -r 1 -ac 1 -ab 2 -s 180x230 -f image2 " + imageName;

        try {
            Process pr = Runtime.getRuntime().exec(cmd);
            pr.getInputStream();
            Thread.sleep(15000);
            File f = new File(imageName);
            if (f.exists()) {
                // f.delete();
                System.out.println("file exists");
                return imageName;
            } else {
                // f.createNewFile();
                System.out.println("file not exists");
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 验证图片文件类型是否符合要求
     *
     * @param fileType
     * @return
     */
    public static boolean checkFileImg(String fileType) {
        boolean flag = false;
        for (String type : imgFileTypes) {
            if (type.equals(fileType))
                flag = true;
        }
        return flag;
    }

    /**
     * 判断图片文件是否过小
     *
     * @param file
     * @return
     */
    public static boolean checkFileImgMinSize(File file) {
        boolean flag = false;
        long size = file.length();
        if (size > imgFileMinSize)
            flag = true;
        return flag;
    }

    /**
     * 判断图片文件是否超长
     *
     * @param file
     * @return
     */
    public static boolean checkFileImgMaxSize(File file) {
        boolean flag = false;
        long size = file.length();
        if (size <= imgFileMaxSize)
            flag = true;
        return flag;
    }

    /**
     * 验证视频文件类型是否符合要求
     *
     * @param fileType
     * @return
     */
    public static boolean checkFileVideo(String fileType) {
        boolean flag = false;
        for (String type : videoFileTypes) {
            if (type.equals(fileType))
                flag = true;
        }
        return flag;
    }

    /**
     * 判断视频文件是否过小
     *
     * @param file
     * @return
     */
    public static boolean checkFileVideoMinSize(File file) {
        boolean flag = false;
        long size = file.length();
        if (size > videoFileMinSize)
            flag = true;
        return flag;
    }

    /**
     * 判断视频文件是否超长
     *
     * @param file
     * @return
     */
    public static boolean checkFileVideoMaxSize(File file) {
        boolean flag = false;
        long size = file.length();
        if (size <= videoFileMaxSize)
            flag = true;
        return flag;
    }

    /**
     * 读取file.properties配置文件中的属性
     *
     */
    static {
        try {
            Properties props = new Properties();
            //得到当前类的类加载器,以流的方式读取配置文件   
            props.load(UploadFileUtils.class.getClassLoader().getResourceAsStream("file.properties"));
            saveFileURL = props.getProperty("saveFileURL");
            ffmpegPath = props.getProperty("ffmpegPath");
            imagePath = props.getProperty("imagePath");
            serverProtocol = props.getProperty("serverProtocol");
            serverIP = props.getProperty("serverIP");
            serverPort = props.getProperty("serverPort");
            downloadAction = props.getProperty("downloadAction");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值