在SSM框架中使用AJAX实现多文件上传

今天来学习一下在SSM框架中使用ajax实现文件的上传。
1.首先我们需要一个搭建好的SSM框架项目,这个在这篇文章里不是重点,自行先搭建好需要的项目。
这里我是用的jsp页面来和后台接口关联,在jsp文件中我们需要一个form表单,请求方法为POST,enctype="multipart/form-data",设置这样的一些属性,在表单的子标签里面要一个type=“file”的input标签;像这样的<input type="file" id="file" name="file"/> ,当然也可以指定multiple="multiple",这样就可以支持多个文件的上传了。除了file类型的标签,我们还需要一个辅助的标签来响应ajax请求,一般来说就是用最简单的button类型的input标签了,记住 这里的ipnut标签的类型不能写submit,否则就直接是表单的提交了,和咱们的ajax没啥关系了。


<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2018/6/28
  Time: 20:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <script type="text/javascript" src="../lib/js/jquery-1.9.0.min.js"></script>
    <title>文件上传</title>
</head>
<body>
    <div style="align-items: center;align-content: center;">
        <h1>文件上传</h1>
        <form id="form" method="post" enctype="multipart/form-data">
            <p>请选择要上传的文件:</p>
            <input id="file" name="file" type="file" multiple="multiple"/>
            <br>
            <input id="upload" name="upload" type="button" value="上传">
        </form>
    </div>
</body>
<script>
    $(window).ready(function () {
        $("#upload").click(function () {
           var formData = new FormData($('#form')[0]);//获取表单中的文件
           //ajax请求
           $.ajax({
               url:"/file/upload",//后台的接口地址
               type:"post",//post请求方式
               data:formData,//参数
               cache: false,
               processData: false,
               contentType: false,
               success:function (data) {
                   alert(data);
               },error:function () {
                   alert("操作失败~");
               }
           })
        });
    });
</script>
</html>

controller


package com.ssm.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
 
/**
 * created by viking 2018/06/28
 * 文件上传接口
 */
@RestController
@RequestMapping("file")
public class UploadFileController {
 
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile[] files){//支持多个文件的上传
        //实例化一个文件存放的目录地址
        String dir = "F:/uploadFile/";
        for (MultipartFile file : files){
            System.out.println("文件类型:"+file.getContentType());
            String filename = file.getOriginalFilename();
            String suffix = filename.substring(filename.length() - 3);
            System.out.println("文件名:"+filename);
            System.out.println("文件后缀:"+suffix);
            System.out.println("文件大小:"+file.getSize()/1024+"KB");
            //创建要保存文件的路径
            File dirFile = new File(dir,filename);
            if (!dirFile.exists()){
                dirFile.mkdirs();
            }
            try {
                //将文件写入创建的路径
                file.transferTo(dirFile);
                System.out.println("文件保存成功~");
            } catch (IOException e) {
                e.printStackTrace();
            }
 
 
        }
        return "OK";
    }
}

原文:https://blog.csdn.net/Mr__Viking/article/details/80866626

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值