从服务器指定位置下载文件

下载文件转换成流,这里说两种流的方式:

1. 文件流
2. 字节流

一,字节流

String filePath=“/opt/peoject/file/123/pdf”; //这个是你服务上存放文件位置

方法体,代码如下:

public byte[] downLoadFile(String filePath) throws Exception {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(filePath);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

后端 controller层:

@PostMapping("/downLoadFile")
public byte[] downLoadFile(HttpServletRequest request,HttpServletResponse response){
    return  downLoadFile2(response,filePath);
}

前端接收处理、

参考地址:文件下载

二,文件流

public void downLoadFile2(HttpServletResponse response,String filePath) throws Exception {
        File file = new File(filePath);
        //获取文件名称 (例如:123.pdf)
        String fileName=filePath.substring(filePath.lastIndexOf("\\"));
        InputStream inputStream = new FileInputStream(file);
        response.setContentType("application/pdf;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();
        inputStream.close();
    }

后端controller

public void downLoadFile(HttpServletRequest request,HttpServletResponse response){
  downLoadFile2(response,filePath);
}

下载后,浏览器这里会有显示文件
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FTP是一种文件传输协议,可以用来在网络上进行文件传输。在Linux系统中,可以使用`ftpget`命令从FTP服务器下载文件到本地指定目录下。具体步骤如下: 1. 打开终端窗口,输入以下命令连接FTP服务器: ``` ftp ftp.example.com ``` 其中,`ftp.example.com`是FTP服务器的地址,根据实际情况替换。 2. 输入用户名和密码登录FTP服务器,如果登录成功,会看到类似如下提示信息: ``` Connected to ftp.example.com. 220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- 220-You are user number 1 of 50 allowed. 220-Local time is now 06:30. Server port: 21. 220-This is a private system - No anonymous login 220-IPv6 connections are also welcome on this server. 220 You will be disconnected after 15 minutes of inactivity. Name (ftp.example.com:user): your_username 331 User your_username OK. Password required Password: 230 OK. Current restricted directory is / ``` 其中,`your_username`是你在FTP服务器上的用户名,根据实际情况替换。 3. 进入要下载文件所在的目录,使用`cd`命令切换目录,例如: ``` cd /path/to/ftp/dir ``` 其中,`/path/to/ftp/dir`是要下载文件所在的目录,根据实际情况替换。 4. 使用`ftpget`命令下载文件到本地指定目录下,例如: ``` ftpget -v -u your_username -p your_password ftp://ftp.example.com/path/to/ftp/file /path/to/local/dir/file ``` 其中,`-v`表示显示详细的下载信息,`-u`和`-p`分别表示FTP服务器的用户名和密码,`ftp://ftp.example.com/path/to/ftp/file`表示要下载文件的FTP路径,`/path/to/local/dir/file`表示要下载到的本地目录和文件名,根据实际情况替换。 5. 下载完成后,可以使用`ls`命令查看本地目录是否有对应的文件,例如: ``` ls /path/to/local/dir ``` 其中,`/path/to/local/dir`是要查看的本地目录,根据实际情况替换。 6. 使用`exit`命令退出FTP服务器,例如: ``` exit ``` 注意:在使用`ftpget`命令下载文件时,需要确保本地目录已经存在并且有写权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值