Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Struts FileUpload

Status
Not open for further replies.

TJahelka

Programmer
Aug 4, 2004
2
US
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
 
It will not throw FileNotFoundException if file does not exist on client side. The server has no idea if file exist on client side or not. The only thing it get is a empty input steam, i.e. the file size is 0.

Prehaps you can treat file size == 0 as FileNotFound.
 
That's was pretty much the decision I had come to as well. I was just confused because the API says it will throw a FileNotFoundException. Do you know under what (if any) conditions it will throw the FileNotFoundExcpetion.

Thanks,

Terry
 
It throws FileNotFoundException if the uploaded file is not found on server (Not client). I guess this could only happen if the uploaded file got delete or the file didn't get created in the first place due to IO problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top