开发者社区> 问答> 正文

SpringMVC在上传文件的时候提示The current request is not a multipart request,如何破?

/* 保存订单 /
@RequestMapping("/insertOrder")
public @ResponseBody Object insertOrder(String userId,@RequestParam(required=false) MultipartFile voice) {}

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

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

展开
收起
a123456678 2016-03-19 10:24:17 15447 0
1 条回答
写回答
取消 提交回答
  • String uploadUrl = actionUrl;  
        String end = "\r\n";  
        String twoHyphens = "--";  
        String boundary = "******";  
        try  
        {  
          URL url = new URL(uploadUrl);  
          HttpURLConnection httpURLConnection = (HttpURLConnection) url  
              .openConnection();  
          httpURLConnection.setDoInput(true);  
          httpURLConnection.setDoOutput(true);  
          httpURLConnection.setUseCaches(false);  
          httpURLConnection.setRequestMethod("POST");  
          httpURLConnection.setRequestProperty("Connection", "Keep-Alive");  
          httpURLConnection.setRequestProperty("Charset", "UTF-8");  
          httpURLConnection.setRequestProperty("Content-Type",  
              "multipart/form-data;boundary=" + boundary);  
      
          DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());  
          dos.writeBytes(twoHyphens + boundary + end);  
          dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""  
                  + srcPath.substring(srcPath.lastIndexOf("/") + 1)  
                  + "\"" + end);  
          dos.writeBytes(end);  
    2019-07-17 19:07:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Well-That-Escalated-Quickly-How-Abusing-The-Docker-API-Led-To-Remote-Code-Execution-Same-Origin-Bypass-And-Persistence 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载