20-Sep-03 (Created: 20-Sep-03) | More in 'CS-Java'

SingleFileUploadPart: Some sample code to upload a file in JetSpeed

package com.indent.actions.portlets;
import org.apache.turbine.util.*;
import org.apache.turbine.util.upload.*;
import com.ai.application.interfaces.*;
import com.ai.application.utils.*;
import java.util.Map;
import com.ai.common.*;

/**
 * Copy a file to the designated directory
 * Return the copied filename
 *
 * Property file entries
 * **********************
 * 1. fileUploadFormFieldName
 *    Form field name on the html page
 *    default is "filename"
 *
 * 2. targetFilename_WS
 *    name of the target file
 *    You can use aspire substitutions
 *    You also can use aspire file directory prefix
 *    ex: aspire:\\mydir\\{user}\\{foldername}}\\abc.txt
 *
 * Output
 * ******
 * 1. Full target filename
 * 2. Also drops the following turbine entries into the hash table
 *    1. file item name: fileitem_name
 *    2. file item filename: fileitem_filename
 *    3. file item size : fileitem_size
 *
 * Example properties file
 * ************************
 * request.uploadFileRequest.classname=com.ai.htmlgen.DbPreTranslateArgsMultiRequestExecutor
 * request.uploadFileRequest.request.db=your-db-alias
 * request.uploadFileRequest.request.1=saveFile
 * request.uploadFileRequest.request.2=saveFilenameInDatabase
 * request.uploadFileRequest.redirectUrl=ShowResults
 *
 * request.saveFile.classname=com.indent.actions.portlets.SingleFileUploadPart
 * request.saveFile.fileUploadFormFieldName=filename
 * request.saveFile.targetFilename_WS=aspire:\\mydir\\{user}\\{foldername}}\\abc.txt
 * request.saveFile.resultName=fullfile_name
 *
 * request.saveFilenameInDatabase.classname=com.ai.db.DBRequestExecutor2
 * request.saveFilenameInDatabase.query_type=update
 * request.saveFilenameInDatabase.stmt=(your insert statement)
 */

public class SingleFileUploadPart extends ATurbinePart
{
      protected Object execute(String requestName, RunData rundata, Map inArgs)
         throws RequestExecutionException
      {
         try
         {
            String fileuploadFieldName = AppObjects.getValue(requestName + ".fileUploadFormFieldName","filename");
            FileItem fi = rundata.getParameters().getFileItem(fileuploadFieldName);
            //Get file item details
            String fiName = fi.getName();
            String fiFileName = fi.getFileName();
            long fiSize = fi.getSize();

            inArgs.put("fileitem_name",fiName);
            inArgs.put("fileitem_filename",fiFileName);
            inArgs.put("fileitem_size",Long.toString(fiSize));

            String targetFilename = AppObjects.getValue(requestName + ".targetFilename_WS");
            String finalFilename = SubstitutorUtils.generalSubstitute(targetFilename,inArgs);
            String fullFilename = com.ai.common.FileUtils.translateFileName(finalFilename);

            fi.write(fullFilename);
            return fullFilename;
         }
         catch(ConfigException x)
         {
            throw new RequestExecutionException("Error:Config error",x);
         }
         catch(Exception x)
         {
            throw new RequestExecutionException("Error:filewrite error",x);
         }

      }//eof-method
}//eof-class