Springboot文件上传代码笔记

1.在src下创建filter包,包内Class名UploadFilter

package com.gd.filter;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebFilter(urlPatterns = "/upload/*", filterName = "uploadFilter")
public class UploadFilter implements Filter {
    //定义文件路径常量
    public static final File UPLOAD_FOLDER = new File("F:\\nginx-1.14.2\\html\\upload");//修改上传路径
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化uploadFilter");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //转换ServletRequest request类型
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        int index = uri.lastIndexOf("/");
        String fileName = uri.substring(index + 1);
        File file = new File(UploadFilter.UPLOAD_FOLDER, fileName);
        //输入流
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bs = new byte[(int) file.length()];
        fileInputStream.read(bs);
        //关闭流
        fileInputStream.close();
        //转换ServletResponse response类型
        HttpServletResponse resp = (HttpServletResponse) response;
        resp.getOutputStream().write(bs);
        System.out.println("---请求进入--");
    }

    @Override
    public void destroy() {

    }
}

2.在控制层(controller)编写交互接口

package com.gd.controller;

import com.gd.filter.UploadFilter;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
public class UploadController {

    /**
     * 文件上传
     *
     * @param multipartFile
     * @param request
     * @return
     */
    @RequestMapping("/uploadFile")
    public String uploadFile(@RequestParam("multipartFile") MultipartFile multipartFile,HttpServletRequest request) {
        System.out.println(multipartFile.getOriginalFilename());
        System.out.println("上传的文件夹路径:" + UploadFilter.UPLOAD_FOLDER);
        if (UploadFilter.UPLOAD_FOLDER.exists() == false) {
            UploadFilter.UPLOAD_FOLDER.mkdirs();
        }
        //准备用来接收数据的文件
        File uploadFile = new File(UploadFilter.UPLOAD_FOLDER, multipartFile.getOriginalFilename());
        try {
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
}

3.前端页面上传入口静态页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/uploadFile" method="post" enctype="multipart/form-data">
    <input type="file" name="multipartFile">
    <input type="submit" value="提交">
</form>

</body>
</html>

 

posted @ 2019-03-06 13:54  StanleyBlogs  阅读(651)  评论(0编辑  收藏  举报