Struts2 Single File Upload (7)

Source: Internet
Author: User
Tags naming convention

Front Page JSP

<form action= "uploadaction" enctype= "Multipart/form-data" method= "POST" >
<label> Uploading Files:</label>
<input type= "File" name= "MyFile"/>
<input type= "Submit" value= "Submission"/>


public class Uploadaction extends Actionsupport {
Three global attributes note the naming convention, the first half of the property name is consistent, or the null value
Uploaded files (old files)
Private File myfile;
Uploaded file name (old file)
Private String Myfilefilename;
Upload file type (old file)
Private String Myfilefilecontenttype;

Handling Upload Requests
Public String upload () {
Generate a new file name (using UUID)
String newmyfilename = Uuidutil.getuuid () +myfilefilename.substring (Myfilefilename.lastindexof ("."));
Specify the location of the upload
String path = Servletactioncontext.getservletcontext (). Getrealpath ("Upload");
Where to upload files
String filepath = path+file.pathseparator+newmyfilename;
SYSTEM.OUT.PRINTLN ("filepath =" +filepath);
Building a new file
File NewFile = new file (filepath);

Read in write out from old file read content to new file
FileInputStream FIS = null;
FileOutputStream fos = null;

try {
Encapsulating old files into an input stream
FIS = new FileInputStream (myfile);
Encapsulates a new file into an output stream
FOS = new FileOutputStream (newfile);
Sets a byte array buffer content
byte [] bt = new byte[1024];
int len = 0;
* Cyclic reading of the contents of the buffer
* Input stream continuously reads bytes into buffer (old file to buffer)
* Output stream writes bytes to new file (buffer to new file) continuously
while (len = (BT))!=-1) {
Fos.write (BT, 0, Len);
Fos.close ();
Fis.close ();
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();

return SUCCESS;

Public File Getmyfile () {
return myfile;

public void Setmyfile (File myfile) {
This.myfile = myfile;

Public String Getmyfilefilecontenttype () {
return myfilefilecontenttype;

public void Setmyfilefilecontenttype (String myfilefilecontenttype) {
This.myfilefilecontenttype = Myfilefilecontenttype;

Public String Getmyfilefilename () {
return myfilefilename;

public void Setmyfilefilename (String myfilefilename) {
This.myfilefilename = Myfilefilename;



<!--file upload interception in Struts2
The file in the STRUTS2 core package has a default size of struts.multipart.maxsize=2097152, which is 2M. This is the struts2 default intercept,
Workaround: In the Struts.xml configuration file, add
<constant name= "struts.multipart.maxSize" value= "10485760"/>
The value unit here is B, which is 10485760B = 10MB.
<constant name= "struts.multipart.maxSize" value= "10485760"/>
<package name= "Upload" namespace= "/" extends= "Struts-default" >
<action name= "uploadaction" class= "com.oak.action.UploadAction" method= "Upload" >

Struts2 Single File Upload (7)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.