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语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。

在jsp/servlet中断点/多线程下载文件

<%@ page import="java.io.File" %>

<%@ page import="java.io.IOException" %>

<%@ page import="java.io.OutputStream" %>

<%@ page import="java.io.RandomAccessFile" %>

<%!

    public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {

        RandomAccessFile raf = new RandomAccessFile(file, "r")

        java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD())

        response.setHeader("Server", "www.trydone.com")

        response.setHeader("Accept-Ranges", "bytes")

        long pos = 0

        long len

        len = raf.length()

        if (request.getHeader("Range") != null) {

            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT)

            pos = Long.parseLong(request.getHeader("Range")

                    .replaceAll("bytes=", "")

                    .replaceAll("-", "")

            )

        }

        response.setHeader("Content-Length", Long.toString(len - pos))

        if (pos != 0) {

            response.setHeader("Content-Range", new StringBuffer()

                    .append("bytes ")

                    .append(pos)

                    .append("-")

                    .append(Long.toString(len - 1))

                    .append("/")

                    .append(len)

                    .toString()

            )

        }

        response.setContentType("application/octet-stream")

        response.setHeader("Content-Disposition", new StringBuffer()

                .append("attachmentfilename=\"")

                .append(file.getName())

                .append("\"").toString())

        raf.seek(pos)

        byte[] b = new byte[2048]

        int i

        OutputStream outs = response.getOutputStream()

        while ((i = raf.read(b)) != -1) {

            outs.write(b, 0, i)

        }

        raf.close()

        fis.close()

    }

%>

<%

    String filePath = request.getParameter("file")

    filePath = application.getRealPath(filePath)

    File file = new File(filePath)

    downloadFile(request, response, file)

%>

ublic HttpServletResponse download(String path, HttpServletResponse response) {

try {

// path是指欲下载的文件的路径。

File file = new File(path)

// 取得文件名。

String filename = file.getName()

// 取得文件的后缀名。

String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase()

// 以流的形式下载文件。

InputStream fis = new BufferedInputStream(new FileInputStream(path))

byte[] buffer = new byte[fis.available()]

fis.read(buffer)

fis.close()

// 清空response

response.reset()

// 设置response的Header

response.addHeader("Content-Disposition", "attachmentfilename=" + new String(filename.getBytes()))

response.addHeader("Content-Length", "" + file.length())

OutputStream toClient = new BufferedOutputStream(response.getOutputStream())

response.setContentType("application/octet-stream")

toClient.write(buffer)

toClient.flush()

toClient.close()

} catch (IOException ex) {

ex.printStackTrace()

}

return response

}


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

原文地址: https://outofmemory.cn/tougao/8137825.html

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

发表评论

登录后才能评论

评论列表(0条)

保存