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"/>
</form>
Action
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 = Fis.read (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;
}
}
Struts.xml
<!--file upload interception in Struts2
The default.properties 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" >
<result>
/welcome.jsp
</result>
</action>
</package>
Struts2 Single File Upload (7)