aspose file tools*
The moose likes Struts and the fly likes Struts 1.1 upload multiple files with multiple input  elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 1.1 upload multiple files with multiple input  elements" Watch "Struts 1.1 upload multiple files with multiple input  elements" New topic
Author

Struts 1.1 upload multiple files with multiple input elements

randal alan
Greenhorn

Joined: Jun 14, 2002
Posts: 9
URGENT!!!
Hello, can someone help me determine how to change my action class below to accept mulitple files being uploaded?

ACTION CLASS

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
UploadForm uploadForm = (UploadForm) form;

FormFile inputFile = uploadForm.getFile();
String fileName = inputFile.getFileName();


try {

MessageResources mr = getResources(request, "upload");

String location = mr.getMessage("location");
int max_size = NumberUtils.stringToInt(mr.getMessage("size"));

if (inputFile.getFileSize() > max_size) {
ActionErrors errors2 = new ActionErrors();
errors2.add(ActionErrors.GLOBAL_ERROR, new ActionError ("error.maxsize"));
saveErrors(request, errors2);
return mapping.findForward("failure");
}

InputStream inputStream = inputFile.getInputStream();

byte[] fileContents = new byte[inputFile.getFileSize()];
inputStream.read(fileContents);
File outputFile = new File (location + fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
fos.write(fileContents);
fos.close();

} catch (FileNotFoundException e) {
e.printStackTrace();

ActionErrors errors2 = new ActionErrors();
// Report the error using the appropriate name and ID.
errors2.add(ActionErrors.GLOBAL_ERROR, new ActionError ("error.io"));
saveErrors(request, errors2);
return mapping.findForward("failure");
}

catch (IOException e) {
e.printStackTrace();

ActionErrors errors2 = new ActionErrors();
// Report the error using the appropriate name and ID.
errors2.add(ActionErrors.GLOBAL_ERROR, new ActionError ("error.io"));
saveErrors(request, errors2);
return mapping.findForward("failure");

}

return mapping.findForward("success");

}
}


ACTION FORM

public class UploadForm extends ActionForm

{

private FormFile file1 = null;

private FormFile file2 = null;

private FormFile file3 = null;

/**
* Get file1
* @return FormFile
*/
public FormFile getFile1() {
return file1;
}

/**
* Set file1
* @param <code>FormFile</code>
*/
public void setFile1(FormFile f) {
this.file1 = f;
}

/**
* Get file2
* @return FormFile
*/
public FormFile getFile2() {
return file2;
}

/**
* Set file2
* @param <code>FormFile</code>
*/
public void setFile2(FormFile f) {
this.file2 = f;
}

/**
* Get file3
* @return FormFile
*/
public FormFile getFile3() {
return file3;
}

/**
* Set file3
* @param <code>FormFile</code>
*/
public void setFile3(FormFile f) {
this.file3 = f;
}

public void reset(ActionMapping mapping, HttpServletRequest request) {

// Reset values are provided as samples only. Change as appropriate.

file1 = null;
file2 = null;
file3 = null;

}

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.

// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new org.apache.struts.action.ActionError("error.field.required"));
// }
return errors;

}
}
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Where does this come from?


Your upload form class does not have this method. You must be calling getFile1(), getFile2() and getFile3() respectively.
randal alan
Greenhorn

Joined: Jun 14, 2002
Posts: 9
That is the line that is in question. I know it should be uploadForm.getFile1() but how do I get the others?

Is it as simple as this:
FormFile inputFile1 = uploadForm.getFile1();
FormFile inputFile2 = uploadForm.getFile2();
FormFile inputFile3 = uploadForm.getFile3();

If so, then what if only one file is uploaded? Do I just check all to see if they are null and pass on the one that is not null? I appologize if this is a very simple question. All the examples through google mentioned using a List or Collection.
randal alan
Greenhorn

Joined: Jun 14, 2002
Posts: 9


Guys, please help me out here!!!
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I guess I do not understand what your exact question is or where you are having problems. Does your page have three files to upload, or is the number of files dynamic? The check for null does not work, but you can check if getFileSize() returns a value of 0.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 1.1 upload multiple files with multiple input elements