网络文件暂存为临时文件,读取时长
pom文件
1 <dependency> 2 <groupId>it.sauronsoftware</groupId> 3 <artifactId>jave</artifactId> 4 <version>1.0.2</version> 5 </dependency>
这个jar包不在maven私服上
jar包下载地址 :http://www.java2s.com/Code/Jar/j/Downloadjave102jar.htm
安装到本地maven仓库:
mvn install:install-file -DgroupId=it.sauronsoftware -DartifactId=jave -Dversion=1.0.2 -Dpackaging=jar -Dfile=C:UserspxwxDownloadsjave-1.0.2.jar
1 /** 2 * 获取网络文件,暂存为临时文件 3 * @param url 4 * @return 5 * @throws UnknownHostException 6 * @throws IOException 7 */ 8 public static File getFileByUrl(String url) throws UnknownHostException, IOException{ 9 File tmpFile = File.createTempFile("temp", ".tmp");//创建临时文件 10 Image2Binary.toBDFile(url, tmpFile.getCanonicalPath()); 11 return tmpFile; 12 } 13 14 /** 15 * 获取时长 16 * @return 17 * @throws IOException 18 * @throws EncoderException 19 */ 20 public static long getDuration(File file) throws EncoderException { 21 MultimediaInfo m = new Encoder().getInfo(file); 22 return m.getDuration(); 23 } 24 25 /** 26 * 获取http://mp4.res.hunantv.com/new_video/28/7A0B569858C4B58526273897A2321658_20170926_1_1_256.mp4 27 * 时长信息和大小信息 28 * @param args 29 */ 30 public static void main(String[] args) { 31 try {
//网络音频需要暂存为临时文件。如果是本地文件,File file = new File("C:\xx.mp3"); 32 File file = getFileByUrl("https://resource.puxinwangxiao.com/gaozhongbibeiwenyanwengushici_4.mp3"); 33 System.out.println("视频大小:" + file.length() / 1024 + "kb"); 34 System.out.println("视频时长:" + getDuration(file) / 1000 + "s"); 35 36 37 } catch (Exception e) { 38 // TODO: handle exception 39 } 40 }
package com.puxinwangxiao.mts.util; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Image2Binary { private static Logger log = LoggerFactory.getLogger(Image2Binary.class); public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out=new ByteArrayOutputStream(); byte[] buffer=new byte[1024*4]; int n=0; while ( (n=in.read(buffer)) !=-1) { out.write(buffer,0,n); } return out.toByteArray(); } /** * 网络文件转换为byte二进制 * @Title: toByteArray * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param url * @param @return * @param @throws IOException 设定文件 * @return byte[] 返回类型 * @throws */ public static byte[] toByteArray(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(conn.getInputStream()); ByteArrayOutputStream out=new ByteArrayOutputStream(); byte[] buffer=new byte[1024*4]; int n=0; while ( (n=in.read(buffer)) !=-1) { out.write(buffer,0,n); } return out.toByteArray(); } /** * @throws IOException * @throws MalformedURLException * 网络文件转换为本地文件 * @Title: toByteArray * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param url * @param @return * @param @throws IOException 设定文件 * @return byte[] 返回类型 * @throws */ public static void toBDFile(String urlStr, String bdUrl) throws IOException,UnknownHostException{ URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(conn.getInputStream()); byte[] data=toByteArray(in); in.close(); FileOutputStream out=new FileOutputStream(bdUrl); out.write(data); out.close(); } /** * 直接获取网络文件的md5值 * @param urlStr * @return */ public static String getMd5ByUrl(String urlStr){ String md5 = null; try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(conn.getInputStream()); md5 = DigestUtils.md5Hex(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return md5; } /** * 获取网络文件的输入流 * @param urlStr * @return */ public static InputStream getInputStreamByUrl(String urlStr){ DataInputStream in = null; try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); in = new DataInputStream(conn.getInputStream()); } catch (IOException e) { log.error("url转换输入流失败,错误信息{}",e.getMessage()); } return in; } public static void main(String[] args) { // try { // toBDFile("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496324940814&di=1d70e0de447be6547c372718b9b30ff6&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F204%2F22%2FYMG9CAUWUM15.jpg","E://a.jpg"); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } String a = getMd5ByUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496324940814&di=1d70e0de447be6547c372718b9b30ff6&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F204%2F22%2FYMG9CAUWUM15.jpg"); System.out.println(a); } }