开发者社区> 问答> 正文

SpringMVC在上传文件的时候提示The current request i?400报错

SpringMVC在上传文件的时候提示The current request is not a multipart request,如何破?? 400 报错 /** 保存订单 */
@RequestMapping("/insertOrder")

public @ResponseBody Object insertOrder(String userId,@RequestParam(required=false) MultipartFile voice) {}

比如是这样一个Controller。 对方是移动端发送POST请求来执行这个方法。 当它发布的请求没有voice这个参数的时候就会报错。。。

以前在用form提交的时候知道如何解决,但是单纯的POST请求不知道如何解决了。

展开
收起
爱吃鱼的程序员 2020-05-30 23:02:31 487 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    参照这行 httpURLConnection.setRequestProperty("Content-Type",  

    •           "multipart/form-data;boundary=" + boundary);  

    设置消息头

    ######
    1.  String uploadUrl = actionUrl;  
    2.     String end = "\r\n";  
    3.     String twoHyphens = "--";  
    4.     String boundary = "******";  
    5.     try  
    6.     {  
    7.       URL url = new URL(uploadUrl);  
    8.       HttpURLConnection httpURLConnection = (HttpURLConnection) url  
    9.           .openConnection();  
    10.       httpURLConnection.setDoInput(true);  
    11.       httpURLConnection.setDoOutput(true);  
    12.       httpURLConnection.setUseCaches(false);  
    13.       httpURLConnection.setRequestMethod("POST");  
    14.       httpURLConnection.setRequestProperty("Connection""Keep-Alive");  
    15.       httpURLConnection.setRequestProperty("Charset""UTF-8");  
    16.       httpURLConnection.setRequestProperty("Content-Type",  
    17.           "multipart/form-data;boundary=" + boundary);  
    18.   
    19.       DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());  
    20.       dos.writeBytes(twoHyphens + boundary + end);  
    21.       dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""  
    22.               + srcPath.substring(srcPath.lastIndexOf("/") + 1)  
    23.               + "\"" + end);  
    24.       dos.writeBytes(end);  
    ######

    form的enctype 设置一下

    <form action="/api/v1.0/tests/testfile" httpMethod="post" enctype="multipart/form-data">
      <input type="file" name="testfile"/>
      <button type="submit">提交</button>
    </form>



    ######回复 @Dreampie : 好的,非常感谢~~######回复 @Leaybc : post提交也可以设置的 可以参考我的 Resty 框架 client设计 里面有文件上传和下载 http://dreampie.gitbooks.io/resty-chs/content/client.html######囧,这个我知道,但是他们不是用form提交的,直接用post提交的######

    移动端通过httpclient发送文件,具体百度。

    服务端

    @RequestMapping("/insertOrder")

    @ResponseBody

    public  Object insertOrder(String userId,HttpServletRequest req) {

    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;             // 获得文件:         MultipartFile file = multipartRequest.getFileMap()

    }

    ######那是不是客户端发送方式不正确######关键是他们加了voice这个参数就行,不加就会报错,哪怕这个参数的内容是null都是正常的。######http://www.oschina.net/code/snippet_1863482_43648
    2020-05-30 23:02:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载