欢迎光临
感受代码之美

Spring Boot文件上传/下载示例

JAVA.jpg

前台

参考:漂亮的文件上传控件input file css样式整理

后台

代码如下:

package com.aitlp.dataArchives.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Spring Boot 文件上传下载
 * @author 四个空格-https://www.4spaces.org
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {
    /**
     * 多文件上传
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/bacthFileUpload")
    public Map bacthFileUpload(@RequestParam("file") MultipartFile[] file) {
        Map resultMap = new HashMap();
        resultMap.put("code", "0");
        resultMap.put("msg", "success");
        for (MultipartFile multipartFile : file) {
            singleFileUpload(multipartFile);
        }
        return resultMap;
    }

    /**
     * 单个文件上传
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/singleFileUpload")
    public Map singleFileUpload(MultipartFile file) {
        Map resultMap = new HashMap();
        resultMap.put("code", "200");
        resultMap.put("msg", "success");
        // 获取上传文件路径
        String originalFilename = file.getOriginalFilename();
        // 获取上传文件的后缀
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        // 上传路径
        String uploadPath = "F:\\upload\\";
        // 上传文件名
        String fileName = new Date().getTime() + new Random().nextInt(100) + "." + fileSuffix;
        File savefile = new File(uploadPath + fileName);
        if (!savefile.getParentFile().exists()) {
            savefile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(savefile);
        } catch (IOException e) {
            resultMap.put("code", "500");
            resultMap.put("msg", "error");
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 文件下载
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/downloadFile")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = request.getParameter("fileName");
        //设置文件路径,这里写死,下载F盘的1557803382139.jpg
        File file = new File("F://upload//" + fileName);
        //File file = new File(realPath , fileName);
        if (file.exists()) {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
            byte[] buffer = new byte[1024];
            FileInputStream fileInputStream = null;
            BufferedInputStream bufferedInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                bufferedInputStream = new BufferedInputStream(fileInputStream);
                OutputStream os = response.getOutputStream();
                int i = bufferedInputStream.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bufferedInputStream.read(buffer);
                }
                return "下载成功";
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return "下载失败";
    }

}

参考文章:

  1. SpringBoot文件上传下载
转载请注明来源:四个空格 » Spring Boot文件上传/下载示例

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址