其他分享
首页 > 其他分享> > # SpringBoot 文件上传与下载

# 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);

    }
 }
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("文件不存在");
    }
}

标签:AjaxResult,return,SpringBoot,fileName,file,import,上传,下载,String
来源: https://blog.csdn.net/love_life_forever/article/details/121659332