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="width: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上傳。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM