easyUI+ssh上传图片

来源:互联网 发布:虎牙直播点歌软件 编辑:程序博客网 时间:2024/05/01 20:52

jsp页面:

<div id="UploadPanel" class="easyui-dialog"
                                data-options="closed:true" style="width: 320px;" title="图片上传 ">
                                <form id="upLoadForm" method="POST" enctype="multipart/form-data">
                                <table>
                                    <tr>
                                        <td><input type="file" name="image" class="file"
                                            id="fileField" size="28" /></td>
                                    </tr>
                                    <tr>
                                        <td>添加的图片大小不能超过2MB</td>
                                        <td><a class="easyui-linkbutton"
                                            data-options="plain:true" href="javascript:void(0)" onclick="FunUpLoad()">上传</a></td>
                                    </tr>
                                    <tr>
                                </table>
                                </form>

注意:红色字体一定要有,就像jsp动态跳转上传一样

js代码:

function FunUpLoad(){
    if($("#fileField").val() == ""){
         alert("请选择上传文件!");
         return;
    }
    $("#upLoadForm").form('submit',
    {
        url : 'mesher_upLoad.action',
        onsubmit : function() {
            return $(this).form("validate");
        },
        success : function(data) {
            var obj = eval("("+data+")");
            $("#UploadPanel").dialog("close");//关闭添加文件页面
            $("img").attr("src",obj.merIntro.gridContent)//将src地址修改为保存在数据库中的地址,用来回显图片
        }
    });
}


basicAction中的代码

    /**
     * 以源文件类型来保存上传的文件,使用UUID作为文件名,并返回为文件存储的全路径
     * @param upload
     * @param uploadFileName
     * @param key
     * @return savePath + UUIDfileName
     * @throws IOException
     */
    public String saveFile(String key, File upload, String uploadFileName)
            throws IOException {
        long now = new Date().getTime();
        if (upload != null) {
            // 读取配置文件中的路径
            Properties pro = new Properties();
            InputStream path = Thread.currentThread().getContextClassLoader().//
                    getResourceAsStream("path.properties");// 获取路径并转换成流
            pro.load(path);
            String savePath = pro.getProperty(key);// key表示配置文件中的key

//其实如果项目不需要的话没必要用配置文件区路径,可以自己直接获取

            // 1.设置保存上传的文件全部路径
            String uploadPath = ServletActionContext.getServletContext()
                    .getRealPath("/") + savePath;

            // 判断上传文件名是否有扩展名
            int index = uploadFileName.lastIndexOf('.');
            if (index != -1) {
                imageFileName = now + uploadFileName.substring(index);
            } else {
                imageFileName = Long.toString(now);
            }
            // 使用UUID作为文件名,以解决重名的问题
            String UUIDfileName = Long.toString(now)
                    + UUID.randomUUID().toString() + imageFileName;
            // 如果文件夹不存在,就创建
            File dir = new File(uploadPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File destFile = new File(uploadPath, UUIDfileName);
            upload.renameTo(destFile);// 移动到目的地
            return savePath + UUIDfileName;
        }
        return null;
    }


path.properties文件

#图片保存路径
basePath = pages/upload_image/



action代码(调用basicAction)

    public String upLoad() {
        try {

//注释掉的部分代码也是可以用的,但是没有改写文件名,并且不能在别的action中复用
            //读取配置文件
//            Properties pro = new Properties();
//            InputStream path = Thread.currentThread().getContextClassLoader().//
//                    getResourceAsStream("path.properties");//获取路径并转换成流
//                pro.load(path);
//                String savePath = pro.getProperty("basePath");
//                String uploadPath =ServletActionContext.getServletContext().getRealPath("/") + savePath;
//            
//            if (image != null) {
//                File savefile = new File(new File(uploadPath), imageFileName);
//                if (!savefile.getParentFile().exists())
//                    savefile.getParentFile().mkdirs();
//                FileUtils.copyFile(image, savefile);
//            }
            String key = "basePath";
            String path = saveFile(key,image, imageFileName); //调用basicAction方法
            mesherIntro.setGridContent(path);
            save();//调用save方法保存文件路径
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }

struts.xml

<action name="mesher_*" class="mesherIntroAction" method="{1}">
            <result type="json">
                <param name="root">jsonObj</param>
            </result>
            <interceptor-ref name="fileUpload">
                <!-- 文件过滤 -->
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                <!-- 文件大小, 以字节为单位 -->
                <param name="maximumSize">1025956</param>
            </interceptor-ref>
            <!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
            <interceptor-ref name="defaultStack" />
        </action>

0 0
原创粉丝点击