文件上传工具类1

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();      
        }      

猜你喜欢

转载自javakeycode.iteye.com/blog/1099179