JAVA编码(57)—— SpringMvc+easyui文件上传(传统提交form表单形式)

<!-- 上传文件 -->
<div id="uploadFiles" class="easyui-dialog"
     style="width: 500px; height: 200px; padding: 10px 20px" closed="true" align="center"
     data-options="modal:true,closed:true,resizable:true,collapsible:true,minimizable:true,maximizable:true"
     buttons="#uploadFile-buttons">
    <form id="uploadFormId" name="uploadForm" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td>
                    <input type="file" id="uploadId" name="file" style="width:200px;">
                </td>
            </tr>
        </table>
    </form>
    <div id="uploadFile-buttons">
        <a href="#" class="easyui-linkbutton showbutton" iconCls="icon-ok" onclick="upload()">上传</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#uploadFiles').dialog('close')">取消</a>
    </div>
</div>
/**
 * 文件上传
 */
function uploadFile() {
    $('#uploadFiles').dialog('open').dialog('setTitle', '文件上传');
}

function upload() {
    //获取上传文件控件内容
    var file = document.getElementById('uploadId').files[0];
    //判断控件中是否存在文件内容,如果不存在,弹出提示信息,阻止进一步操作
    if (_fn.empty(file)) {
        alert('错误,请选择文件');
        return;
    }
    uploadForm.action = basedir + "/Activityinfo/uploadForm.do";
    uploadForm.submit();
}
@RequestMapping(path = "/uploadForm", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult uploadForm(@RequestParam("file") MultipartFile file) {
        try {
            System.out.println("fileName:" + file.getOriginalFilename());
            if (!file.isEmpty()) {
                //上传文件路径
                String path = PubFun.getRealPath();
                //上传文件名
                String filename = file.getOriginalFilename();
                File filepath = new File(path, filename);
                //判断路径是否存在,如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                File file1 = new File(path + File.separator + filename);
                file.transferTo(file1);
                System.out.println("临时文件的完整路径:" + path + filename);
            }
            return InvokeResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return InvokeResult.failure(e.getMessage());
        }
    }

 

/**
     * 获取项目的绝对路径 upload文件夹
     * 
     * @return
     */
    public static String getRealPath() {
        // TODO Auto-generated method stub
        PubFun pubFun = new PubFun();
        String realPath = pubFun.getClass().getResource("/").getPath();
        if (realPath.contains("WEB-INF/")) {
            realPath = "/"+realPath.substring(0, realPath.indexOf("WEB-INF/") - 1);
            realPath += "/upload/";
        } else if (realPath.contains("target/")) {
            realPath = realPath.substring(0, realPath.indexOf("target/") - 1);
            realPath += "/src/main/webapp/upload/";
        }
        return realPath;
    }

 

posted @ 2018-01-30 17:49  xu_shuyi  阅读(161)  评论(0编辑  收藏  举报