@ApiOperation("上传图片")
    @ApiResponses({
            @ApiResponse(code = ResultCode.SYSTEM_ERROR, message = ResultCode.SYSTEM_ERROR_MSG),
            @ApiResponse(code = ResultCode.PARAM_ERROR, message = ResultCode.PARAM_ERROR_MSG)
    })
    @PostMapping(value = "/upload")
    public String fileUpload(@RequestHeader(value = "source", required = false) String source,
                                   HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestParam(value = "file",required = true) MultipartFile uploadFile){
        try {
            log.info("上传图片成功");
            return "success";
        } catch (Exception e) {
            log.error("上传图片发生异常",e);
            return "error";
        }
    }

复制流filter

@Component
@Order(value = -99999)
@WebFilter(urlPatterns = "/*")
@Slf4j
public class CopyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        ServletRequest requestWrapper = null;
        if (servletRequest instanceof HttpServletRequest) {
            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) servletRequest);
        }
        if (null == requestWrapper) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }
    }
}

复制request

@Slf4j
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private final byte[] body;

    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
        String sessionStream = getBodyString(request);
        body = sessionStream.getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 获取请求Body
     *
     * @param request
     * @return
     */
    public String getBodyString(final ServletRequest request) {
        StringBuilder sb = new StringBuilder();
        try (InputStream inputStream = cloneInputStream(request.getInputStream());
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            log.error("Failed to invoke getBodyString", e);
        }
        return sb.toString();
    }

    /**
     * Description: 复制输入流</br>
     *
     * @param inputStream
     * @return</br>
     */
    public InputStream cloneInputStream(ServletInputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buffer)) > -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            byteArrayOutputStream.flush();
        } catch (IOException e) {
            log.error("Failed to invoke cloneInputStream", e);
        }
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }

    @Override
    public BufferedReader getReader() {

        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() {

        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
        return new ServletInputStream() {


            @Override
            public int read() {

                return bais.read();
            }

            @Override
            public boolean isFinished() {

                return false;
            }

            @Override
            public boolean isReady() {

                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }
        };
    }
}

报错:

Required request part 'file' is not present

问题原因:

读取的流有乱码

----------------------------267101170674265160322840Content-Disposition: form-data; name="file"; filename="wx2ba363fc4454f27c.o6zAJs07nT9TRQEWUQB9Tm_qw8Bc.6a5wlcKcw00he5978fdca6a8c58336099df875c16a8c.jpg"Content-Type: image/jpeg����

解决方案:

暂无。

 

结束。