XMLHttpRequest.send()发送Blob二进制数据,serverlet中如何接收?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XMLHttpRequest.send()发送Blob二进制数据,serverlet中如何接收?相关的知识,希望对你有一定的参考价值。

如题,serverlet中java代码如何写,如何去掉content_type等头数据?
得到的结果的是这样的:
------WebKitFormBoundarydUMAKAaxvcQpvKws
Content-Disposition: form-data; name="aa"; filename="blob"
Content-Type: audio/wav

RIFF ? WAVEfmt    D? ?   data ? 。。。。。。
一堆乱码
------WebKitFormBoundarydUMAKAaxvcQpvKws--

我怎样只要乱码里面的内容,不要头和尾的内容?请高手指点!

参考技术A xmlHttpRequest.send("num1="+1+"&num2="+2);

写法OK的。

bl bl bl bl bl

package com.dh.activiti;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProcessInterceptor implements HandlerInterceptor {
  
  
    @Override  
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
  
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");  
  
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");  
  
        httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
  
        httpServletResponse.setHeader("X-Powered-By","Jetty");  
  
  
        String method= httpServletRequest.getMethod();  
  
        if (method.equals("OPTIONS")){  
            httpServletResponse.setStatus(200);  
            return false;  
        }  
  
        System.out.println(method);  
  
        return true;  
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

  
    @Override  
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {  
  
    }  
}  

  

package com.dh.activiti;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

    @Bean   //把我们的拦截器注入为bean
    public HandlerInterceptor getMyInterceptor(){
        return new ProcessInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用于添加拦截规则, 这里假设拦截 /url 后面的全部链接
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

  

package com.dh.activiti;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

    @Bean   //把我们的拦截器注入为bean
    public HandlerInterceptor getMyInterceptor(){
        return new ProcessInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用于添加拦截规则, 这里假设拦截 /url 后面的全部链接
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

  

package com.dh.activiti;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/17.
 */
@RestController
public class ActivityConsumerServiceImpl  {


    @RequestMapping(value = "/startActivityDemo", method = RequestMethod.POST)
    public boolean startActivityDemo(@RequestBody Map<String,Object> map, HttpServletResponse response) {
//        response.setHeader("Access-Control-Allow-Origin","127.0.0.1:8020");
        System.out.println(JSON.toJSONString(map));
        TestEntity testEntity = JSON.parseObject(JSON.toJSONString(map),TestEntity.class);
        System.out.println(JSON.toJSONString(testEntity));
        return false;
    }


}

  

以上是关于XMLHttpRequest.send()发送Blob二进制数据,serverlet中如何接收?的主要内容,如果未能解决你的问题,请参考以下文章

XMLHttpRequest.send()发送Blob二进制数据,serverlet中如何接收?

什么是 XMLHttpRequest.send(file) 的 Fetch API 等价物?

服务工作者:获取请求时检索 xhr 正文

ajax如何请求python接口

FormData使用教程

Java实现文件导出过程中的提示功能