欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot文件上传与下载实现

程序员文章站 2022-06-02 14:11:41
...

文件上传

package com.neusoft.learn_sp.utils.file_tranfer;
import java.io.*;
import java.net.URLEncoder;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * 文件上传demo
 */
@RestController
public class FileUploadController {
    @RequestMapping(value = "/upload", method = RequestMethod.POST,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String fileUpload(@RequestParam("fileUpload") MultipartFile file) throws IOException {
        File convertFile = new File("F:/"+file.getOriginalFilename());
        convertFile.createNewFile();
        FileOutputStream fout = new FileOutputStream(convertFile);
        fout.write(file.getBytes());
        fout.close();
        return "File is upload successfully";
    }
}

postman接口设置
springboot文件上传与下载实现

文件下载

@GetMapping("/download")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

    File scFileDir = new File("=======> 文件path <=======");
    File TrxFiles[] = scFileDir.listFiles();
    String fileName = TrxFiles[0].getName(); // 获取指定目录下的第一个文件

    // 如果文件名不为空,则进行下载
    if (fileName != null) {
        //设置文件路径
        String realPath = "=======> 文件path <=======";
        File file = new File(realPath, fileName);
        // 如果文件名存在,则进行下载
        if (file.exists()) {
            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 实现文件下载
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                System.out.println("Download the song successfully!");
            }
            catch (Exception e) {
                System.out.println("Download the song failed!");
            }
            finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    return null;
}