Springmvc文件上传

HTML

<form id="form" onsubmit="return false" enctype="multipart/form-data">
                <div class="form-group form-inline">
                    <input class="form-control " id="ipt_file"  disabled>
                    <span class="form-control " onclick="$('#ipt_file_hidden').click()" style="cursor: pointer">选择文件</span>
                    <input type="file" accept="audio/*" id="ipt_file_hidden" name="file" onchange="$('#ipt_file').val($('#ipt_file_hidden').val())" style="display: none" />
                </div>
                <div class="input-group">
                    <button type="button" class="btn btn-default" onclick="save()">提交</button>
                    <!--进度条-->
                    <div class="progress" style="margin-top: 10px">
                        <div class="progress-bar" role="progressbar" aria-valuenow="60"
                             aria-valuemin="0" aria-valuemax="100" style="width: 40%;">
                            <span class="sr-only">40% 完成</span>
                        </div>
                    </div>
                </div>
            </form>

js

import jquery.form.js

<script >


function save(){
    $('#form').ajaxSubmit({
        beforeSubmit:function(){
            if ($('#ipt_file').val()==''){
                alert("file为空")
                return false;
            }else {

            }
        },
        type:'post',
        url:'/system/upload',
        dataType:'json',
        success:function (res) {
            alert(res.msg)
        },
        error:function (res) {
            alert(JSON.stringify(res))
        }
    })
}

</script>

springmvc

controller:

@RequestMapping
    public void upload(HttpServletResponse res, @RequestParam(value = "file")MultipartFile file) throws IOException {
        printOK(res,systemService.upload(file));
    }

service:

public String upload(MultipartFile file)  {
        try {
            file.transferTo(new File("D:\\data\\"+file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "上传成功!";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值