java根据url下载文件

1.需要参数

remoteFilePath:url地址
fileName:文件名称

2.代码实现

@RequestMapping(value = "/uplaodFile",  method = RequestMethod.GET)
    public static void getFile(String remoteFilePath,String fileName, HttpServletResponse response) throws IOException {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        response.setHeader("Content-Disposition", "inline;filename="+fileName);
        try
        {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection)urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(response.getOutputStream());
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1)
            {
                bos.write(b, 0, len);
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            try
            {
                bis.close();
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 本身不能直接将文件下载到浏览器。通常情况下,Java Web 应用程序会将文件下载到服务器上,然后通过响应头部信息设置浏览器访问该文件URL,从而实现浏览器下载文件的功能。 以下是一个简单的示例代码,演示如何将文件下载到服务器并将文件 URL 发送给浏览器: ```java import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.net.URLDecoder; @WebServlet("/download") public class FileDownloader extends HttpServlet { private static final long serialVersionUID = 1L; public FileDownloader() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = URLDecoder.decode(request.getParameter("file"), "UTF-8"); // 获取文件路径 File file = new File(filePath); // 创建文件对象 String fileName = file.getName(); // 获取文件名 if (file.exists()) { response.setContentType("application/octet-stream"); // 设置响应内容类型 response.setHeader("Content-Disposition", "attachment;filename=" + fileName); // 设置响应头部信息,告诉浏览器打开“文件下载”对话框并设置文件名 InputStream inputStream = new FileInputStream(file); // 创建文件输入流 OutputStream outputStream = response.getOutputStream(); // 创建响应输出流 byte[] buffer = new byte[4096]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); // 将文件内容写入响应输出流 } inputStream.close(); outputStream.flush(); outputStream.close(); } else { response.setContentType("text/html;charset=UTF-8"); // 设置响应内容类型 response.getWriter().println("文件不存在!"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ``` 在上面的示例中,`doGet` 方法通过 URL 参数获取文件路径,创建一个文件对象,并设置响应头部信息。然后,通过文件输入流读取文件内容并将其写入响应输出流,最后刷新并关闭输出流,实现文件下载的功能。如果文件不存在,将返回一个错误信息。 在响应头部信息中,设置 `Content-Disposition` 为 `attachment`,表示将该文件作为附件下载,浏览器会弹出“文件下载”对话框。设置 `filename` 参数为文件名,告诉浏览器该文件的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值