• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 1.1 upload multiple files with multiple input elements

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

}
}
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Guys, please help me out here!!!
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic