uploadifive上传文件

1、uploadifive-demo.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    String path = request.getContextPath();

%>
<html>
<head>
    <title>uploadifive-demo</title>
    <link rel="stylesheet" type="text/css" href="jslib/uploadifive-v1.2.2-standard/uploadifive.css">
    <script src="jslib/uploadifive-v1.2.2-standard/jquery.min.js" type="text/javascript"></script>
    <script src="jslib/uploadifive-v1.2.2-standard/jquery.uploadifive.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            $('#signup-idimage1').uploadifive({
                'auto' : true,
                'uploadScript' : '<%=path%>/userController/uploadPhoto.do',
                'fileObjName' : 'userPhoto',
                'buttonText' : '上传图片',
                'queueID' : 'tip-queue1',
                'fileType' : 'image/*',
                'multi' : false,
                'fileSizeLimit'   : 5242880,
                'uploadLimit' : 1,
                'queueSizeLimit'  : 1,
                'height' : 20,
                'width' : 70,
                'onUploadComplete' : function(file, data) {
                    var obj = JSON.parse(data);
                    if (obj.img == "500") {
                        alert("系统异常!");
                    } else {
                        $("#frontSide").val(obj.img);
                        document.getElementById("submit").disabled = false;
                    }
                },
                onCancel : function(file) {
                    $("#frontSide").val("");
                    /* 注意:取消后应重新设置uploadLimit */
                    $data = $(this).data('uploadifive'),
                            settings = $data.settings;
                    settings.uploadLimit++;
                    alert(file.name + " 已取消上传~!");
                },
                onFallback : function() {
                    alert("该浏览器无法使用!");
                },
                onUpload : function(file) {
                    document.getElementById("submit").disabled = true;//当开始上传文件,要防止上传未完成而表单被提交
                }
            });
        });
    </script>
</head>
<body>
    <h1>UploadiFive Demo</h1>

    <form class="cd-form" id="subForm" action="openaccount" method="post">
        <div id="cd-signup">
            <p class="fieldset">
                <label class="image-replace cd-photo1" for="signup-idimage1"></label>
                <input class="full-width has-padding has-border" id="signup-idimage1" type="file" name="userPhoto">
                <input type="hidden" name="frontSide" id="frontSide">
            <div id="tip-queue1"></div>
            </p>
            </p>
            <p class="fieldset">
                <input class="full-width2" id="submit" type="submit" value="提交">
            </p>
          </div>
    </form>
</body>
</html>

2、UploadController.java

/**
 * 头像上传
 * @param response
 * @param request
 * @param uploadPhoto
 * @throws IOException
 */
@RequestMapping("/uploadPhoto")
public void uploadPhoto(HttpServletResponse response,HttpServletRequest request, @RequestParam(value="userPhoto", required=false) MultipartFile userPhoto)throws IOException {
    byte[] bytes = userPhoto.getBytes();
    System.out.println(userPhoto.getOriginalFilename());
    String uploadDir = request.getRealPath("/")+"upload";
    File dirPath = new File(uploadDir);
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    String sep = System.getProperty("file.separator");
    File uploadedFile = new File(uploadDir + sep + userPhoto.getOriginalFilename());
    System.out.println("uploadedFile :"+uploadedFile.toString());
    FileCopyUtils.copy(bytes, uploadedFile);
    String msg = "true";
    response.getWriter().write(msg);
}

3、网上下载uploadifive-v1.2.2-standard

4、测试上传

转载于:https://my.oschina.net/chaun/blog/655154

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值