Java如何利用url下载MP3保存到本地?

Java如何利用url下载MP3保存到本地?,第1张

Java如何利用url下载MP3保存的方法:

1 /** ;

2      * TODO 下载文件到本地 ;

3      * @author nadim  ;

4      * @date Sep 11, 2015 11:45:31 AM ;

5      * @param fileUrl 远程地址 ;

6      * @param fileLocal 本地路径

7      * @throws Exception ;

8      */ ;

9     public void downloadFile(String fileUrl,String fileLocal) throws Exception {;

10         URL url = new URL(fileUrl)

11         HttpURLConnection urlCon = (HttpURLConnection) url.openConnection()

12    绝租亩     urlCon.setConnectTimeout(6000)

13         urlCon.setReadTimeout(6000)

14         int code = urlCon.getResponseCode()

15         if (code != HttpURLConnection.HTTP_OK) {

16    型衡         throw new Exception("文件读取失败")

17         }      

18         //读文件流;

19        DataInputStream in = new DataInputStream(urlCon.getInputStream())

20         DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal))

21         byte[] buffer = new byte[2048]

22         int count = 0

23         while ((count = in.read(buffer)) >0) {;

24             out.write(buffer, 0, count)

25         }

26         out.close()

27         in.close()

28     }。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,并森允许程序员以优雅的思维方式进行复杂的编程 。

1、web.xml文件中增加

<mime-mapping>

<extension>doc</extension>

<mime-type>application/vnd.ms-word</mime-type>

</mime-mapping>

2、程序如下:

<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%>

<%

//关于文件下载时采用文件流输出的方式处理:

//加上response.reset(),并且所有的%>后面不要换行,包迟衫括最后一个;

//因为Application Server在处理编译jsp时对于%>和<%之间的内容一般是原样输出,而且默认是PrintWriter,

//而你却要进行流输出:ServletOutputStream,这样做相当于试图在Servlet中使用两种输出机制,

//就会发生:getOutputStream() has already been called for this response的错误

//详细请见《More Java Pitfill》一书的第二部分 Web层Item 33:试图在Servlet中使用两种输出机制 270

//而且如果有换行,对于文本文件没有什么问题,但是对于其它格式,比如AutoCAD、Word、Excel等文件

//下载侍旦磨下来的文件中就会多出一些换行符0x0d和0x0a,这样可能导致某些格式的文件无法打开,有些也可以正常打开。

response.reset()//可以加也可以不加

response.setContentType("application/x-download")//设置为下载application/x-download

// /../老斗../退WEB-INF/classes两级到应用的根目录下去,注意Tomcat与WebLogic下面这一句得到的路径不同,WebLogic中路径最后没有/

System.out.println(this.getClass().getClassLoader().getResource("/").getPath())

String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../系统解决方案.doc"

String filenamedisplay = "系统解决方案.doc"//系统解决方案.txt

filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8")

response.addHeader("Content-Disposition","attachmentfilename=" + filenamedisplay)

OutputStream output = null

FileInputStream fis = null

try

{

output = response.getOutputStream()

fis = new FileInputStream(filenamedownload)

byte[] b = new byte[1024]

int i = 0

while((i = fis.read(b)) >0)

{

output.write(b, 0, i)

}

output.flush()

}

catch(Exception e)

{

System.out.println("Error!")

e.printStackTrace()

}

finally

{

if(fis != null)

{

fis.close()

}

简单写个小例子

import javax.media.ControllerEvent

import javax.media.ControllerListener

import javax.media.NoPlayerException

import javax.media.Player

import javax.media.Manager

import javax.media.MediaLocator

import javax.media.EndOfMediaEvent

import javax.media.PrefetchCompleteEvent

import javax.media.RealizeCompleteEvent

import java.io.*

import java.util.*

public class PlayerMusic implements ControllerListener {

//播放对象

private Player player

//是否循环播放

private boolean first,loop

//文件路径

private String path

//存放MP3文件

private List<String>mp3List

//当前MP3文件数量

private int mp3NO=0

PlayerMusic(List<String>mp3List)

{

this.mp3List=mp3List

}

//播放方法

public void start()

{

try {

player = Manager.createPlayer(new File(mp3List.get(mp3NO)).toURI().toURL())

} catch (NoPlayerException e) {

// TODO 自动迹衡生雹禅成 catch 块

e.printStackTrace()

System.out.println("不能播姿肆做放此文件!")

return

} catch (IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace()

return

}

if(player==null)

{

System.out.println("播放文件为空!")

return

}

player.addControllerListener(this)

//提取媒体内容

player.prefetch()

}

public void controllerUpdate(ControllerEvent e) {

//当媒体播放结束时,循环播放

if(e instanceof EndOfMediaEvent)

{

mp3NO++

System.out.println(mp3NO)

if(mp3NO<mp3List.size())

{

this.start()

}

return

}

//当提取媒体的内容结束

if (e instanceof PrefetchCompleteEvent) {

System.out.println("内容结束")

player.start()

return

}

//当实例化后

if (e instanceof RealizeCompleteEvent) {

System.out.println("实例化")

//pack()//执行pack() *** 作

return

}

}

public static void main(String[] args)

{

List<String>path=new ArrayList<String>()

path.add("F:\\歌曲\\天下.mp3")

path.add("F:\\歌曲\\画心.mp3")

path.add("F:\\歌曲\\只对你有感觉.mp3")

PlayerMusic play=new PlayerMusic(path)

play.start()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12414287.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存