struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Laurie Harper <lau...@holoweb.net>
Subject Re: ClassCast Exception for ExcelFile Upload
Date Fri, 27 Apr 2007 20:41:19 GMT
You didn't specify which line in the code you posted is throwing the 
exception, but I would guess it's this:

     InputStream in = (InputStream) formFile.getInputStream();
     FileInputStream fin = (FileInputStream) in;			

It's never wise to down-cast a value from an interface type to a 
concrete type like this. The API contract is to return you an 
InputStream; it doesn't have to be any particular type of InputStream.

To do this safely, use the following code instead:

     InputStream in = (InputStream) formFile.getInputStream();
     FileInputStream fin = new (FileInputStream(in);			

L.

Kirthi wrote:
> I am having problems with the FileUpload in struts.
> 
> I am uploading an Excel File through browser and sending to Struts Action
> Class, handling it with FormFile. I am casting this FormFile to
> FileInputStream which is working fine.
> 
> But when I am deleting some of the rows in that excel file and uploading it
> again its giving me a ClassCastException when converting the Form File to
> FileInputStream.
> 
> I have no idea y its happening like this. Can any one help me with this?
> 
> Thanks
> 
> Here is the error I am getting
> 
> root cause 
> 
> java.lang.ClassCastException
> 	at rcsa.struts.ExcelFileUpload.execute(ExcelFileUpload.java:48)
> 	at
> org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
> 	at
> org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
> 	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
> 	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:716)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
> 	at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198)
> 	at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
> 	at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
> 	at
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
> 	at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
> 
> ******************************************************************************************
> 
> Here is the code
> 
> import java.io.*;
> 
> import javax.servlet.ServletException;
> import javax.servlet.http.*;
> 
> import org.apache.struts.upload.FormFile;
> import org.apache.struts.action.*;
> 
> import rcsa.poi.RcsaPOIEventListner;
> import rcsa.exceptions.RcsaException;
> import rcsa.dao.OpRiskSSheet;
> import rcsa.misc.ErrorHandler;
> 
> public class ExcelFileUpload extends Action {
> 	
> //	 --------------------------------------------------------- Instance
> Variables
> 	String target;
> 
> 	// --------------------------------------------------------- Methods
> 
> 	/** 
> 	 * Method execute
> 	 * @param mapping
> 	 * @param form
> 	 * @param request
> 	 * @param response
> 	 * @return ActionForward
> 	 */
> 	public ActionForward execute(
> 		ActionMapping mapping,
> 		ActionForm form,
> 		HttpServletRequest request,
> 		HttpServletResponse response) throws IOException, ServletException,
> RcsaException{
> 			
> 		ActionMessages errors = new ActionMessages();
> 		if(form != null){
> 			ExcelFileUploadForm excelForm = (ExcelFileUploadForm) form;
> 			FormFile formFile = excelForm.getExcelFile();
> 			InputStream in = (InputStream) formFile.getInputStream();
> 			FileInputStream fin = (FileInputStream) in;			errors.clear();
> 			RcsaPOIEventListner.readExcelStream(fin);
> 			//errors = new ErrorHandler(opSheet, errors).checkErrors();
> 			if(errors.isEmpty()){
> 				target = "success";
> 			}else{
> 				target = "failure";			
> 			}
> 		}else{
> 			errors.add(ActionMessages.GLOBAL_MESSAGE, 
> 					new ActionMessage("UNABLE TO LOCATE THE EXCEL FILE", false));
> 			target = "failure";
> 		}
> 		saveErrors(request, errors);
> 		return mapping.findForward(target);
> 
> 	}
> 
> }
> 
> 
> **********************************************************************
> 
> import org.apache.struts.validator.ValidatorForm;
> import org.apache.struts.upload.FormFile;
> 	
> 	/**
> 	 *@author
> 	 *
> 	 */
> 
> 	/**
> 	 *Form bean for File Upload.
> 	 *
> 	 */
> public class ExcelFileUploadForm extends ValidatorForm{
> 	
> 	private FormFile excelFile;
> 
> 	/**
> 	 * @return Returns the theFile.
> 	 */	 
> 	 public FormFile getExcelFile() {
> 	    return excelFile;
> 	  }
> 	 
> 	 /**
> 	  * @param theFile The FormFile to set.
> 	  */
> 	  public void setExcelFile(FormFile excelFile) {
> 	    this.excelFile = excelFile;
> 	  }
> 	  
> }
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message