Hello,
Any help or suggestions would be greatly appreciated.
I am running in Weblogic 7.0. I have a jsp that contains an html:file tag, an upload form, and an upload action class. The file upload works great, what is not working is error handling. Specifically, if I enter a file that does not exist, I expected a FileNotFoundException to be thrown. This is not happening.
Here are code snippets from my jsp,form, and action:
<html:form action="fileUploadAction.do" enctype="multipart/form-data">
<html:hidden property="jspname" value="fileUpload.jsp"/>
<html:file property="theFile"/><br>
</html:form>
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileUploadForm extends ActionForm {
protected String theText;
protected boolean writeFile;
protected FormFile theFile;
protected String filePath;
private FileUploadRequest fileUploadRequest = new FileUploadRequest();
/**
* Retrieve a representation of the file the user has uploaded
*
* @return theFile
*/
public FormFile getTheFile() {
return theFile;
}
/**
* Set a representation of the file the user has uploaded
*
* @param theFile
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.MessageResources;
import com.fedex.common.j2ee.mcd.MCDClient;
import com.fedex.gdp.util.ErrorMessages;
import com.fedex.regs.msg.FileUploadReply;
import com.fedex.regs.msg.FileUploadRequest;
import com.fedex.sims.j2ee.forms.FileUploadForm;
import com.fedex.sims.j2ee.util.*;
public class FileUploadAction extends LookupDispatchActionBase {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
if (logger.isDebugEnabled()) {
logger.debug("execute: called");
}
FileUploadForm theForm = (FileUploadForm) form;
String text = theForm.getTheText();
FormFile file = theForm.getTheFile();
String fileName = file.getFileName();
String contentType = file.getContentType();
String size = (file.getFileSize() + " bytes");
String data = null;
LinkedList fileLines = new LinkedList();
String dataLine;
if (logger.isDebugEnabled()) {
logger.debug("fileName : " + fileName);
logger.debug("contentType : " + contentType);
logger.debug("size: " + size);
}
try {
InputStream stream = file.getInputStream();
BufferedReader input = new BufferedReader(
new InputStreamReader(stream));
if (!input.ready()) {
throw new IOException("Input file not ready");
}
while ((dataLine = input.readLine()) != null) {
fileLines.add(dataLine);
}
input.close();
}
catch (FileNotFoundException fnfe) {
logger.info("File not found, filename: " + fileName);
ActionErrors ae = new ActionErrors();
loadErrorMessage(getResources(request).getMessage("error.file.notFound"),
"theFile", ae, request, true, false);
return returnUsingKey(request, mapping, "failure");
}
catch (IOException ioe) {
logger.warn("execute: IO error occurred - ", ioe);
ActionErrors ae = new ActionErrors();
loadErrorMessage(getResources(request).getMessage("error.file.accessing"),
"theFile", ae, request, true, false);
return returnUsingKey(request, mapping, "failure");
}
Logger output from a run where the file does not exist provided below:
10:31:34,195 DEBUG (actions.FileUploadAction) - execute: called
10:31:34,207 DEBUG (actions.FileUploadAction) - fileName : text.csv
10:31:34,207 DEBUG (actions.FileUploadAction) - contentType : application/octet-
stream
10:31:34,208 DEBUG (actions.FileUploadAction) - size: 0 bytes
10:31:34,217 WARN (actions.FileUploadAction) - execute: IO error occurred -
java.io.IOException: Input file not ready
Thanks,
Terry
Any help or suggestions would be greatly appreciated.
I am running in Weblogic 7.0. I have a jsp that contains an html:file tag, an upload form, and an upload action class. The file upload works great, what is not working is error handling. Specifically, if I enter a file that does not exist, I expected a FileNotFoundException to be thrown. This is not happening.
Here are code snippets from my jsp,form, and action:
<html:form action="fileUploadAction.do" enctype="multipart/form-data">
<html:hidden property="jspname" value="fileUpload.jsp"/>
<html:file property="theFile"/><br>
</html:form>
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileUploadForm extends ActionForm {
protected String theText;
protected boolean writeFile;
protected FormFile theFile;
protected String filePath;
private FileUploadRequest fileUploadRequest = new FileUploadRequest();
/**
* Retrieve a representation of the file the user has uploaded
*
* @return theFile
*/
public FormFile getTheFile() {
return theFile;
}
/**
* Set a representation of the file the user has uploaded
*
* @param theFile
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.MessageResources;
import com.fedex.common.j2ee.mcd.MCDClient;
import com.fedex.gdp.util.ErrorMessages;
import com.fedex.regs.msg.FileUploadReply;
import com.fedex.regs.msg.FileUploadRequest;
import com.fedex.sims.j2ee.forms.FileUploadForm;
import com.fedex.sims.j2ee.util.*;
public class FileUploadAction extends LookupDispatchActionBase {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
if (logger.isDebugEnabled()) {
logger.debug("execute: called");
}
FileUploadForm theForm = (FileUploadForm) form;
String text = theForm.getTheText();
FormFile file = theForm.getTheFile();
String fileName = file.getFileName();
String contentType = file.getContentType();
String size = (file.getFileSize() + " bytes");
String data = null;
LinkedList fileLines = new LinkedList();
String dataLine;
if (logger.isDebugEnabled()) {
logger.debug("fileName : " + fileName);
logger.debug("contentType : " + contentType);
logger.debug("size: " + size);
}
try {
InputStream stream = file.getInputStream();
BufferedReader input = new BufferedReader(
new InputStreamReader(stream));
if (!input.ready()) {
throw new IOException("Input file not ready");
}
while ((dataLine = input.readLine()) != null) {
fileLines.add(dataLine);
}
input.close();
}
catch (FileNotFoundException fnfe) {
logger.info("File not found, filename: " + fileName);
ActionErrors ae = new ActionErrors();
loadErrorMessage(getResources(request).getMessage("error.file.notFound"),
"theFile", ae, request, true, false);
return returnUsingKey(request, mapping, "failure");
}
catch (IOException ioe) {
logger.warn("execute: IO error occurred - ", ioe);
ActionErrors ae = new ActionErrors();
loadErrorMessage(getResources(request).getMessage("error.file.accessing"),
"theFile", ae, request, true, false);
return returnUsingKey(request, mapping, "failure");
}
Logger output from a run where the file does not exist provided below:
10:31:34,195 DEBUG (actions.FileUploadAction) - execute: called
10:31:34,207 DEBUG (actions.FileUploadAction) - fileName : text.csv
10:31:34,207 DEBUG (actions.FileUploadAction) - contentType : application/octet-
stream
10:31:34,208 DEBUG (actions.FileUploadAction) - size: 0 bytes
10:31:34,217 WARN (actions.FileUploadAction) - execute: IO error occurred -
java.io.IOException: Input file not ready
Thanks,
Terry