文件上傳工具類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;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章