文件上传下载 纯JDK包实现(嘿嘿嘿)

一。文件上传

package com.test.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author lhc
 * @Date 2021/10/22 14:06
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @GetMapping("/upload")
    public File upload(@RequestParam(value = "file") MultipartFile multipartFile) {
        String fileDir = "D:\\substation\\upload\\";
        String fileName = multipartFile.getOriginalFilename();
        File newFile = new File(fileDir + fileName);

        //校验文件夹目录是否存在,不存在就创建一个目录
        if (!newFile.getParentFile().exists()) {
            boolean mkdirsResult = newFile.getParentFile().mkdirs();
            if (!mkdirsResult) {
                System.out.println("创建目录:" + newFile.getParentFile() + "失败");
                return null;
            }
        }
        try {
            //创建文件
            boolean newFile1 = newFile.createNewFile();
            //填充文件内容
            multipartFile.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFile;
    }
}

二。文件下载

选择地址下载参数要有HttpServletResponse response
默认直接下载参数不要有HttpServletResponse response

1。客户端选择地址下载
package com.test.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;


@RestController
@RequestMapping("/file")
public class FileController1 {

    /**
     * 文件下载(客户端选择下载地址)
     *
     * @param response response
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) {
        //服务器文件路径
        String filePath = "D:\\substation\\";
        //服务器文件名
        String fileName = "智能站出图0802.zip";
        //服务器文件
        File file = new File(filePath, fileName);
        //校验服务器文件是否存在
        if (!file.exists()) {
            System.out.println("找不到服务器指定文件");
            return;
        }
		response.setCharacterEncoding("utf-8");
		//防止文件中文名乱码
        downLoadFileName = URLEncoder.encode(downLoadFileName, "UTF-8").replaceAll("\\+", "%20");
       response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + downLoadFileName + ".xlsx");
        byte[] buffer = new byte[1024];
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            outputStream = response.getOutputStream();
            int i = bufferedInputStream.read(buffer);
            while (i != -1) {
                outputStream.write(buffer, 0, i);
                i = bufferedInputStream.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
2。直接下载
package com.test.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;


@RestController
@RequestMapping("/file")
public class FileController2 {
    /**
     * 文件下载(直接下载)
     */
    @GetMapping("/downloadDirect")
    public void downloadDirect() {
        //服务器文件路径
        String filePath = "D:\\substation\\";
        //服务器文件名
        String fileName = "智能站出图0802.zip";
        //服务器文件
        File file = new File(filePath, fileName);
        //本地文件路径
        String newFilePath = "C:\\substation\\";
        //本地文件名
        String newFileName = "智能站出图0802新.zip";
        //本地文件
        File newFile = new File(newFilePath, newFileName);
        //校验服务器文件是否存在
        if (!file.exists()) {
            System.out.println("找不到服务器指定文件");
            return;
        }
        // 校验本地文件夹目录是否存在,不存在就创建一个目录
        if (!newFile.getParentFile().exists()) {
            boolean mkdirsResult = newFile.getParentFile().mkdirs();
            if (!mkdirsResult) {
                System.out.println("创建目录:" + newFile.getParentFile() + "失败");
                return;
            }
        }

        byte[] buffer = new byte[1024];
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            outputStream = new FileOutputStream(newFile);
            int i = bufferedInputStream.read(buffer);
            while (i != -1) {
                outputStream.write(buffer, 0, i);
                i = bufferedInputStream.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飘然生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值