温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringMVC中的异步上传功能怎么利用MultipartFile 实现

发布时间:2020-12-08 15:25:20 来源:亿速云 阅读:254 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关SpringMVC中的异步上传功能怎么利用MultipartFile 实现,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1.添加pom依赖

添加pom依赖,因为用的ajax,数据需要转成json的格式进行传输,所以还有加入一个JSON jar包:

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.1.37</version>
    </dependency>

2.修改配置文件

applicationContext.xml里面需要加上:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8"></property>
    <property name="maxUploadSize" value="5400000"></property>
  </bean>

3.前端页面上

前端页面:

<form id="uploadForm" name="uploadForm"
    enctype="multipart/form-data">
<input name="messageContent" value="多个参数的情况下">
      <label>文件</label> <input type="file" name="file">
      <button class="btn" type="button" id="doSave">提交</button>
  </form>
</body>
</html>

需要加入的JS:

<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui.min.js"></script> 
<script type="text/javascript" src="js/jquery.form.js"></script> 

JS方法:

<script>
  $(function() {
    $("#doSave")
        .click(
            function() {
              $("#uploadForm")
                  .ajaxSubmit(
                      {
                type : 'post',
                url : "/tmpInfo/method2.do",
    //data: //注意只要是写在表单里面的,都不需要加这个属性。在controller中可以根据@RequestParam String str获取到属性值。  
  contentType : "application/x-www-form-urlencoded; charset=utf-8",
    success: function(data) {
         //接受到的data还只是一个字符串,需要转成json对象
         var obj = JSON.parse(data);
         if(obj.flag==true){
          alert("上传成功");
       }else{
         alert("error");
       }
        },
        error: function (data)//服务器响应失败处理函数
        {
          alert("出错");
        } 
       });
  });
  }); 

controller代码:

 @RequestMapping("/method2") 
    @ResponseBody
    public String method2(@RequestParam MultipartFile file,
        @RequestParam String messageContent ) { 
        //多个参数的话只要多个@RequestParam即可,注意参数名要和表单里面的属性名一致
     JSONObject json =new JSONObject();
     System.out.println(messageContent);
      String orgiginalFileName = ""; 
      int m =new Random().nextInt(100)+10;
      System.out.println("m="+m);
      String path="D:/"+m+"b.txt";
      try { 
        File newFile =new File(path);
        file.transferTo(newFile);
        String fileName = file.getName(); 
        InputStream inputStream = file.getInputStream(); 
        String content = file.getContentType(); 
        orgiginalFileName = file.getOriginalFilename(); 
        System.out.println("fileName: "+fileName+", inputStream: "+ inputStream 
              +"\r\n content: "+content+", orgiginalFileName: ="+ orgiginalFileName 
              +"\r\n projectName: ");   
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
      json.put("flag", true);
      json.put("message", "success");
      System.out.println(json.toJSONString());
      return json.toJSONString(); 
    } 

以上就是SpringMVC中的异步上传功能怎么利用MultipartFile 实现,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI