文件上传工具类2

	//获取文件输出流 
		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;
		}
	}

猜你喜欢

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