# SpringBoot 文件上传与下载

文件上传

  • 配置文件本地映射路径
@Configuration
@EnableWebMvc
public class WebConfigure implements WebMvcConfigurer {
    private static final  String LOCATION_IMAGE_PATH = "file:D:\\ZyProject\\data-v\\images\\";
    private static final  String NET_IMAGE_PATH = "/img/**";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
        /** 本地文件路径 **/
        registry.addResourceHandler(NET_IMAGE_PATH).addResourceLocations(LOCATION_IMAGE_PATH);

    }
 }
  • 配置文件上传限制

    spring:
      servlet:
          multipart:
              #是否开启文件上传
              enabled: true 
              #写入磁盘的速度
              file-size-threshold: 2MB
              #单文件最大限制
              max-file-size: 20MB
              #单次传输最大限制
              max-request-size: 200MB
    
  • 创建接口

package com.data.v.sys.controller;
import com.data.v.util.http.AjaxResult;
import com.data.v.util.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author it_dog_zhang
 * @version 1.0
 * @date 2021/12/1 15:45
 * @desc 系统相关
 */
@RestController
public class SysController {
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return AjaxResult.error("上传文件为空");
            }
            // 获取文件名
            String fileName = file.getOriginalFilename();
            String filePath = "D:\\ZyProject\\data-v\\images\\";
            String path = filePath + fileName;
            File dest = new File(path);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();// 新建文件夹
            }
            file.transferTo(dest);// 文件写入
            return AjaxResult.success(path, HttpStatus.SUCCESS);
        } catch (IllegalStateException e) {
            AjaxResult.error(e.getMessage());
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error("上传失败");
    }
    /**
     * 文件下载
     *
     * @return
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public AjaxResult downLoadFile(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String fileName = request.getParameter("file");
        String path = "D:\\ZyProject\\data-v\\images\\";
        File file = new File(path, fileName);

        if (file.exists()) {
            // 设置强制下载打开
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8"));

            // 读取文件
            BufferedInputStream bi = null;
            try {
                byte[] buffer = new byte[1024];
                bi = new BufferedInputStream(new FileInputStream(file));
                ServletOutputStream outputStream = response.getOutputStream();
                int i = -1;
                while (-1 != (i = bi.read(buffer))) {
                    outputStream.write(buffer, 0, i);
                }
                return AjaxResult.success("下载成功");
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            } finally {
                if (bi != null) {
                    try {
                        bi.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return AjaxResult.error("文件不存在");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

有志青年(top)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值