不刷新上传的实现一 struts2

48 篇文章 0 订阅
25 篇文章 0 订阅

 

public class UploadAction extends ActionSupport {
    private File file;
    private String fileFileName;
    private String fileContentType;
    private String uploadFolder;

    public String getUploadFolder() {
        return uploadFolder;
    }

    public void setUploadFolder(String uploadFolder) {
        this.uploadFolder = uploadFolder;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    @Override
    public String execute() throws Exception {
       
        ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
        String returnPic = "";
        boolean sign = true;
        String root = "http://"+ServletActionContext.getRequest().getContextPath()+":"+ ServletActionContext.getRequest().getRemotePort();
        // 1.构建一个输入流
        try {
        InputStream is = new FileInputStream(file);
        // 2.构建一个上传文件路径
         root = ServletActionContext.getServletContext().getRealPath("/");
         System.out.println("1"+ root );
        // 3.获得一个本地文件
        root = root+"upload"+File.separator    + uploadFolder+File.separator;
        File tmp = new File(root);
        if ( !tmp.exists())
            tmp.mkdirs();
       
        root = ImageTool.getFolderPath(root, 0,false);
        File diskFile = new File(root, this.getFileFileName());
        returnPic = diskFile.getAbsolutePath();
        int begin = returnPic.indexOf("upload");
        returnPic = returnPic.substring(begin, returnPic.length() );
        returnPic = returnPic.replace("//", "/");
//        System.out.println( diskFile.getAbsolutePath() );
        // 4.构建输出流
        OutputStream os = new FileOutputStream(diskFile);
        // 5.能过字节写入输出流
        byte[] buffer = new byte[400];
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        is.close();
        os.close();
        }catch(Exception e){
            e.printStackTrace();
            sign = false;
        }
        if(sign==true)
        {
            out.println("<script>parent.callback('upload file success|"+ returnPic +"')</script>");
        }else{
            out.println("<script>parent.callback('upload file error')</script>");
        }
        return null;
    }

 


import java.io.File;

public class ImageTool {
    public static void dealPic(String prefix ,String suffix){
        String path = prefix + suffix;
        File f = new File(path);
        if ( f.exists())
            f.delete();
    }
    public static void dealPic(String suffix){
        String prefix = WebVariable.getWEB_ROOT_PATH();
        String path = prefix + suffix;
        File f = new File(path);
        if ( f.exists())
            f.delete();
    }
    public static boolean judgeFolderFileOverNumber(String path) {
        boolean flag = false;
        try {
            File f = new File(path);
            if (f.isDirectory()) {
                if (f.listFiles().length > Constant.PIC_NUMBER_LIMIT)
                    flag = true;
            }
        } catch (Exception e) {

        }
        return flag;
    }

 
    /**
     * file 2048*2048*2048*2048
     *
     * @param path
     *            path like /tomcat_home/webapps/windfones/upload/promotions/
     * @return
     */
    public static String getFolderPath(String path, int level, boolean rtv) {
        /**
         * 给出一个路径,1 如果该路径是存在,则统计该文件夹下有多少个子文件夹或文件,不存在则生成,1.1如果该路径下的文件夹包含的子文件夹
         * 或子文件夹不大于2048个,则再往下一级探索(递归探索),直至文件夹深度为3,1.1.1如果深度为3的文件夹下包含的文件超过
         * 2048个则退回上一级路径,再重新判断该级路径下的子文件夹是否已满足大于2048个,没有则新增一个文件夹,重新递归
         * 1.2如果文件夹大于2048则 执行1.1.1
         */
        // 0  1. upload/,0,false ==> 2. upload/1/,1,false,==> 3. upload/1/1/,2,false==>4 upload/1/1/1/,3,false
        if (level <= 3) {
            try {
                File f = new File(path);
                if (!f.exists())
                    f.mkdirs();
                if (judgeFolderFileOverNumber(path) && !rtv && (level==3 )) {
                    rtv = true;
                    path = getFolderPath(path, level, rtv);//如果是叶子才可以往回处理
 
                } else {
                    if (rtv){//处理判断已满的情况,路径后退一级 upload/1/1/1/=> upload/1/1/
                        level = level - 1;
                        int end = path.lastIndexOf(File.separator) - 1;
                        path = path.substring(0, end);
                        end = path.lastIndexOf(File.separator) + 1;
                        path = path.substring(0, end);
                        if ( judgeFolderFileOverNumber( path ) ){
                            path = getFolderPath(path, level, rtv);
                        } else {
                            File f2 = new File(path);
                            int fs = f2.listFiles().length;
                           
                            if (fs ==0 )
                                fs = 1;
                            else
                                fs = fs + 1;//对当前文件夹判断,不大于2048则切换文件夹
                            if (path.substring(path.length() - 1, path.length()).equals(File.separator))
                                path = path + "" + fs + File.separator ;
                            else
                                path = path + File.separator + "" + fs +  File.separator ;
                           
                            level= level + 1;
                            if ( level <=3 ) {
                                rtv = false;
                                path = getFolderPath(path, level, rtv);
                            }
                        }
                    } else {
                        int fs = f.listFiles().length;
                        if (fs ==0 )
                            fs = 1;
                       
                        if ( level <3 ){
                            if (path.substring(path.length() - 1, path.length()).equals(File.separator))
                                path = path + "" + fs + File.separator ;
                            else
                                path = path + File.separator + "" + fs +  File.separator ;
                            level = level + 1;
                            path = getFolderPath(path, level, rtv);
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return path;
    }

 
}

 

=====================================================================================

jsp

 

<script>

     function callback(msg) {
         document.getElementById("file").outerHTML = document.getElementById("file").outerHTML;
         if ( msg.indexOf("success") !=-1){
              var begin = msg.indexOf("|");
              var path = msg.substring(begin+1,msg.length);
              document.getElementById("returnValue").value= path;
              msg = msg.substring(0,begin);
        }   
         document.getElementById("msg").innerHTML = "<font color=red>"+msg+"</font>";
     }
</script>

 

<form action="upload.html" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden_frame" >
<table border="0" class="perview">
    <tr>
            <th>选择文件</th>
            <th width="60%">预览图</th>
        </tr>
        <tr>
            <td height="200">
                <input id="file" name="file" type="file" />
                <input type="hidden"  name="returnValue"  id="returnValue"/>
                <input type="hidden"  name="uploadFolder" id="uploadFolder" value=""/>
                <INPUT type="submit" value="上传文件"><br/>
                <span id="msg"></span>
                <br>                           
                <iframe name='hidden_frame' id="hidden_frame"     style='display:none'></iframe>
            </td>
            <td align="center"><img id="idImg"/></td>
        </tr>
        <tr><td colspan="2">
        <input type="button" value="确定" οnclick="closeWindows(1)" />&nbsp;
        <input type="button" value="取消" οnclick="closeWindows(0)" />
        </td></tr>
</table>
</form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值