com.wabacus.util.TestFileUploadInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.wabacus.util.TestFileUploadInterceptor.java

Source

/* 
 * Copyright (C) 2010-2012 <349446658@qq.com>
 * 
 * This file is part of Wabacus 
 * 
 * Wabacus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wabacus.util;

import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;

import com.wabacus.system.intercept.AbsFileUploadInterceptor;

public class TestFileUploadInterceptor extends AbsFileUploadInterceptor {
    public boolean beforeDisplayFileUploadInterface(HttpServletRequest request,
            Map<String, String> mFormAndConfigValues, PrintWriter out) {
        String inputboxid = null;
        String param_no = null;
        String param_name = null;
        String param_txtno = null;
        String oldvalue = null;
        String testfieldname = null;
        if (mFormAndConfigValues != null) {//??
            inputboxid = mFormAndConfigValues.get("INPUTBOXID");
            if (inputboxid != null && !inputboxid.trim().equals("")) {//????request????enctype="multipart/form-data"mFormAndConfigValues???
                param_no = mFormAndConfigValues.get("param_no");
                param_name = mFormAndConfigValues.get("param_name");
                param_txtno = mFormAndConfigValues.get("param_txtno");
                oldvalue = mFormAndConfigValues.get("OLDVALUE");
            }
            testfieldname = mFormAndConfigValues.get("testfieldname");
            System.out.println("????.........");
        } else {//??
            inputboxid = request.getParameter("INPUTBOXID");
            //     if(inputboxid!=null&&!inputboxid.trim().equals(""))
            //    {//??
            //         param_no=request.getParameter("param_no");
            //         param_name=request.getParameter("param_name");
            //        param_txtno=request.getParameter("param_txtno");
            //        oldvalue=request.getParameter("OLDVALUE");
            //     }
            testfieldname = request.getParameter("testfieldname");
            System.out.println("??.........");
        }
        //   if(inputboxid!=null&&!inputboxid.trim().equals(""))
        //   {//?fileuploadpage1.report5<inputbox inputboxparams="param_no=@{no}&amp;param_name=@{name}&amp;param_txtno=condition{txtno}"???
        //       out.println("??<b>?</b>"+param_no+";<b>??</b>"+param_name+";<b>??</b>"+param_txtno+";<b></b>"+oldvalue+"<hr>");
        //  }
        testfieldname = testfieldname == null ? "" : testfieldname.trim();
        out.println("<input type='text' name='testfieldname' value='" + testfieldname + "'><hr>");
        return true;
    }

    public boolean beforeFileUpload(HttpServletRequest request, FileItem fileitemObj,
            Map<String, String> mFormAndConfigValues, PrintWriter out) {

        try {

            String filename = fileitemObj.getName();

            // System.out.println("--------------??"+filename);
            if (filename != null && !filename.trim().equals("")) {
                int pos = filename.lastIndexOf("\\");
                filename = filename.substring(pos + 1);
                String[] filenameSplit = filename.split("\\.");
                if (filenameSplit != null && filenameSplit.length > 0) {
                    Calendar ca = Calendar.getInstance();
                    filename = filenameSplit[0] + "(" + ca.get(Calendar.YEAR) + (ca.get(Calendar.MONTH) + 1)
                            + ca.get(Calendar.DATE) + ca.get(Calendar.HOUR_OF_DAY) + ca.get(Calendar.MINUTE)
                            + ca.get(Calendar.SECOND) + ")" + "." + filenameSplit[1];
                    // + ca.get(Calendar.MILLISECOND)      
                    mFormAndConfigValues.put(FILENAME_KEY, filename);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;// ?      
    }

    /**
     * ??/??????
     */
    public boolean beforeDisplayFileUploadPrompt(HttpServletRequest request, List lstFieldItems,
            Map<String, String> mFormAndConfigValues, String failedMessage, PrintWriter out) {
        if (lstFieldItems == null || lstFieldItems.size() == 0)
            return true;
        FileItem fItemTmp;
        StringBuffer fileNamesBuf = new StringBuffer();
        for (int i = 0; i < lstFieldItems.size(); i++) {
            fItemTmp = (FileItem) lstFieldItems.get(i);
            if (fItemTmp.isFormField())
                continue;//?
            fileNamesBuf.append(fItemTmp.getName()).append(";");
        }
        if (fileNamesBuf.charAt(fileNamesBuf.length() - 1) == ';')
            fileNamesBuf.deleteCharAt(fileNamesBuf.length() - 1);
        if (failedMessage != null && !failedMessage.trim().equals("")) {//
            out.println("<h4>??" + fileNamesBuf.toString() + "" + failedMessage
                    + "</h4>");
        } else {//?
            out.println("<script language='javascript'>");
            out.println("alert('" + fileNamesBuf.toString() + "?');");

            String inputboxid = mFormAndConfigValues.get("INPUTBOXID");
            if (inputboxid != null && !inputboxid.trim().equals("")) {//?
                String name = mFormAndConfigValues.get("param_name");
                name = name + "[" + fileNamesBuf.toString() + "]";
                out.println("selectOK('" + name + "','name',null,true);");
            }
            out.println("</script>");
        }
        return false;//????
    }

}