The first is the webpage part, upload_file.jsp
<%@ page language = "java" image = "java.util.*" Pageencoding = "UTF-8"%> <! Doctype html> <head> <Title> UPLOAD FILE </Title> </Head > <body> <form action = "uploadFile" method = "post" enCTYPE = "Multipart /Form-Data"> <!-File domain-> <input type = "file" name = "source" /< /> input type = "submit" value = "upload"> </form> </body> </html>
For the form of uploading the file, MetHo must be set to post, and Enctype must be set to Multipart/Form-Data.
From the above code, you can see that this form is submitted to the UPLOADFILE this action to process it.
<action name = "uploadFile"> <result name = "success">/uploadResult.jsp </Result> <!-Fileuploading, which can be used to limit the type and document size of upload documents-> <Internetor- Ref Name = "Fileupload"> <!-Limit the size of the file 20m, the unit is byte-> <sem name = "maximumsize"> 20971520 </param> </interceptor-ref> <!-The default interceptor must be declared. After the Fileupload interceptor-> <interceptor-ref name = "defaultStack" /> < /action>
Fileupload interceptor is used to set up upload paths to limit file types and size.
Regarding the size of the file, it is not possible to have <PARAM NAME = "Maximumsize">> You must add it under the <struts> tag
<constant name = "struts.multipart.maxsize" value = "21000000"/>/>
This line of code indicates the maximum size of the file size allowed to upload the files in the entire project, that is to say, any single file size uploaded in this project cannot exceed 21000000 bytes (about 20m). Code, the maximum size of the file that is allowed to be uploaded by default is 2m, so this is also a method that breaks the restrictions of 2M files that can only upload 2M files.
Regarding the type of file, if you need to limit it as a picture file, you can configure it like this
<!-Set up only the upload of the picture file-> <intercepter-ref name = "fileupload"> <posm name = "allowedtypes"> image/bmp, image/x-png, image/gif, image/jpeg </</ Param> < /intercepter-ref> <interceptor-ref name = "defaultStack" /> />
The value in the label is that the value of the file is the MIME type, and the MIME type of the common file can be found in%tomcat_home%/conf/web.xml.
If you want to be limited to Word file, you can <Terecedor> configured like this
<!-Settling only allows upload Word document-> <intercepter-ref name = "fileupload"> <sem name = "Allowedtypes"> Application/Msword, Application/VND.OPENXMLFORMATS -OFFIC EDOCUMENT.WORDPROCESSINGML.DOCUMENT </Param> < /intercepter-ref> <interceptor-ref name = "defaultStack" />
However, I feel like this to limit the file type, it is better to use JavaScript to implement restrictions at the front end.
Next, write uploadAction, the necessary private attributes necessary for UPLOADACTION are Source, which is consistent with the name attribute of the file domain in UPLOAD_FILE.JS P, that is, the name property value of the file domain is Source, then there must Ource, in addition, There are two more important private attributes:
Private String SourceFilename; // The file name of the file to be uploaded
Private String SourceContentType; // The format of the two variable names of the file type to be uploaded is the same as the previous prefix Source and UPLOAD_FILE.JSP.
In summary, that is, for example, the name = "ABC" of the Chinese Field Domain in Upload_file.jsp, then this needs to be defined in the action
Private file abc; Private String abcfilename; Private String AbcContentType;
ABC will automatically obtain the file object to be uploaded, ABCFILENAME automatically obtains the file name, and ABCCONTETTYPE automatically obtains the file type.
I have to focus on uploading paths.
If it is uploaded to the absolute path, it is pretty good, but if you want to upload to the UPLOAD folder in the project root directory, how to get the complete path of this UPLOAD folder?
I have tried to use
ServletActionContext.getServletContext (). GetRealPath ("/upload");
But returned NULL. Also used
ServletActionContext.getRequest (). GetRealPath ("/upload");
Still returned to NULL. But checking this problem online, many people recommend this writing, proving that it may be feasible in some cases, but there are people who return to NULL like me. They recommend a new method at the same time, to let let it UploadAction implements the ServiceConTextAre interface. The specific method is as follows:
Public Class UploadAction Extends ActionSupport ImvletContextAre { / ** * omitting other code ... * / Prive ServletContext Context; Public ServletContext ETCONTEXT () {Return Context;} Public Void SetContext (ServiceContext Context) {this.Context = Context; @ Override Public Void SetServletContext (ServletContext Context) {this.Context = context;}}
Then use
String path = context.getRealpath ("/upload"); // Important: the slash must not be less
Get the path of the UPLOAD folder. Then execute upload:
/*Upload the file to the UPLOAD folder*/file saveful = new file (PATH, SourceFilename); fileutils.copyfile (source, savefile);
I personally recommend this method, because this method seems to avoid the correct path when the project is packed and transferred to other environments.
Paste the complete code of UploadAction in the back UPLOADACTION.JAVA
package com.lidi.ACTION; Import Java.iO.File; Import Java.io.ioException; Import Javax.ServletContext; Import TILS; Import org.apache.Struts2.util.servletContextAre; Import com.opensymphony.xwork2.ActionSupport; Public Class Upload Extends ActionSupport ImvletConTextAre { / ** * / Private Static Inl long serviceVersionuid = 1L; Private file source; // to be uploaded files private string source source; // to be uploaded files File name Private String SourceContentType; // The file type of the file type of the file type Private ServletContext Context; // Important*/*/ Public ServletContext () {RetuRN Context; } PUBLIC VOID SETCONTEXT (ServiceContext Context) {this.Context = context; } / * Getters & setters * / Public File Getsource () {Return Source;} Public Void Setsource (File Source) {this.source = source; ing GetSourceFilename () {Return SourceFilename;} Public Void SetsourceFilename (String SourceFilename) {This.sourceFilename = SourceFilename;} Public String GetsourceContentType () {Return SourceContentType;} Public Voidth E (String SourceContentType) {This.SourceContentType = SourceContentType;} @Override Public Void SetservletContext (servicetcontext content) {this .context = context;} Public String Execute () Throws IOEXCEPTION {/*Get the path to the upload file: project root directory upload folder*/string path; path = context.getRealpath ("/upload"); Can less system. Out.println (Path); /*upload the file to the UPLOAD folder* / file savefile = new file (PATH, SourceFilename); Fileutils.copyfile (SAVEFILE); System.out. Println (Savefile.getabsolutePath () ); Return Success;}}
Upload results page uploadResult.jsp
<%@ page language = "java" image = "java.util.*" pageEncoding = "UTF-8"%> <%@ taglib uri = "/struts-tags" prefix = "s"%> <! <! Doctype html > <HTML> <Head> <Title> UPLOAD Result </Title> </Head> <body> <p> File name: <s: Property Value = "SourceFilename"/> </P> <p> File Type: <s: Property Value = "SourceContentType" /> < /P> <p> File: <A href = "Upload /<s: Property Value =" SourceFilename " /"> <s: Property = "SourceFi lename " / > </a> </p> </body> </html>
The above is all the contents of this article. I hope everyone can like it.