1

Anybody knows how to upload a file and save it into a database. And it can be viewed in a .jsp page. When the user clicks on it, the user can download it and/or if it is a .doc file (like MS Word .doc, .docx files) it can be viewed online like how facebook implements it.

I am a very very new to uploading files. Please be patient with me. What only I knew is this:

<form>
  <input type="file"/>
  <input type="submit" value="Upload"/>
</form>

And also, how to limit the file size, and limit only a group of file type like upload only .txt,.doc,.pdf etc files.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2975385
  • 25
  • 1
  • 7

1 Answers1

1

From the File Upload Interceptor documentation:

Parameters

  • maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this is not related to the various properties found in struts.properties. Default to approximately 2MB.

  • allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.

  • allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded.

For example, to block all files except png, gif and jpeg under 10 MegaBytes:

<interceptor-ref name="fileUpload">
  <param name="maximumSize">
      10485760
  </param>
  <param name="allowedTypes">
     image/png,image/gif,image/jpeg
  </param>
</interceptor-ref>

Read more on File size limit and overall Multipart request size limit.

Be sure to check out the official Struts2 File Upload page too for a complete overview of the subject.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243