SpringMVC在上传文件的时候提示The current request is not a multipart request错误

@RequestMapping("/insertOrder")

@ResponseBody

public  Object insertOrder(String userId,HttpServletRequest req) {

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

String path = req.getSession().getServletContext().getRealPath("/");
        System.out.println(path);
        String filepath = "business/water/images/";
        String picturepath = "";
        if(file!=null){
            picturepath =filepath+getTimeName()+file.getOriginalFilename();
        }
        FileUtil.SaveFileFromInputStream(file.getInputStream(), path+picturepath);
        dto.setPhotos(picturepath);
        resp.setContentType("text/html");

}

********************************************************************************************

jsp中的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>
 
 
***********************************************************************************
  首先要说的就是 ajax 是无法实现上传文件的,可以想一下ajax与后台通信都是通过传递字符串,怎么能传递文件呢?其实出于安全考虑js是不能操作文件的,所以就不要再说用ajax来实现文件的上传了,这是不可能的。

    而本文实现的文件上传也是无页面刷新的,可以说是一种"类似AJAX"方法。

    开始之前先说两句无关的,其实在ajax出现之前,web应用也可以是无刷新的,那时大多通过IFrame来做到这一点。当然Ajax出现之后,人们一窝蜂地投奔Ajax 的阵营了,iFrame 就乏人问津了。但是用iFrame来实现无刷新上传文件确实一个很好的选择。ps:Ajax技术基本上可以说是由google公司带起来的,但少Gmail中上传文件用的还是 IFrame,所以说使用IFrame来上传文件是最好的选择。

    我在这里这里用的技术是jsp,其实asp,php等也是一样可以这么实现的

    一共两个文件就可实现:index.html 和 upload.jsp,在这里讲解一下

index.html

<html>   

<body>   

<form action="upload.jsp" id="form1" name="form1" encType="multipart/form-data"  method="post"       target="hidden_frame" >   

    <input type="file" id="file" name="file" style="450">   

    <INPUT type="submit" value="上传文件"><span id="msg"></span>   

    <br>   

    <font color="red">支持JPG,JPEG,GIF,BMP,SWF,RMVB,RM,AVI文件的上传</font>                 

    <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>   

</form>   

  

</body>   

</html>   

  

<script type="text/javascript">   

function callback(msg)   

{   

    document.getElementByIdx_x_x_x_x_x("file").outerHTML = document.getElementByIdx_x_x_x_x_x("file").outerHTML;   

    document.getElementByIdx_x_x_x_x_x("msg").innerHTML = "<font color=red>"+msg+"</font>";   

}   

</script>  

index.html 中主要要做的就是写一个 form 和 iframe ,并把 form 的 target 设为 iframe 的名字,注意要把 iframe 设为不可见,其他的都是正常的文件上传的写法,这样刷新的页面就是这个隐藏的 Iframe ,而在 index.html 中是不会有页面刷新的,js的 callback 方法是回调方法。用于清空文件上传框和显示后台信息,注意清空文件上传框的方法,和普通方法有点不一样。

--upload.jsp

<%@ page language="java" contentType="text/html; charset=gb2312" %>   

<%@ page import="com.jspsmart.upload.SmartUpload"%>   

  

<%   

    //新建一个SmartUpload对象   

    SmartUpload su = new SmartUpload();   

  

    //上传初始化   

    su.initialize(pageContext);   

  

    // 设定上传限制   

    //1.限制每个上传文件的最大长度。   

    su.setMaxFileSize(10000000);   

  

    //2.限制总上传数据的长度。   

    su.setTotalMaxFileSize(20000000);   

  

    //3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。   

    su.setAllowedFilesList("doc,txt,jpg,rar,mid,waw,mp3,gif");   

       

    boolean sign = true;   

       

    //4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。   

    try {   

        su.setDeniedFilesList("exe,bat,jsp,htm,html");   

  

        //上传文件   

        su.upload();   

        //将上传文件保存到指定目录   

        su.save("c:\");  

 

    } catch (Exception e) {  

        e.printStackTrace();  

        sign = false;  

    }  

    if(sign==true)  

    {  

        out.println("<script>parent.callback('upload file success')</script>");  

    }else  

    {  

        out.println("<script>parent.callback('upload file error')</script>");   

    }   

%>   

upload.jsp 中只要注意最后输出的格式就可以了。其实原理就是输出一段js代码到 iframe 中,然后在iframe中来控制它的父页面。

 

    OK,至此一个无刷新的页面上传组件就做好了,不要忘了在 WEB-INF/lib 下加上必须的 jspSmartUpload.jar 包。

    需要说明的是使用Iframe来上传,状态栏还是会有刷新的,因为iframe 中的页面刷新了嘛,但是外部页面,就是你所看到的页面是没有刷新的,所以也可以说是类似Ajax上传。

原文地址:https://www.cnblogs.com/sily-boy/p/4647690.html