Upload files using struts
1: Html code
<% @ Page contentType = "text/html; charset = GBK" language = "java" %>
<% @ Taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>
<Html>
<Head>
<Title> example of uploading a file </title>
</Head>
<BODY>
<H1> example of uploading a file <Html: errors/> <br>
<Html: form action = "uploadfile. do" enctype = "multipart/form-data">
<Html: file property = "file"/> <br>
<Html: submit> </Html: form>
</BODY>
</Html>
Note: The
2: sucess. jsp code
Html code
<% @ Page contentType = "text/html; charset = GBK" language = "java" %>
<% @ Taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>
<Html>
<H1> upload successful... </Html>
3: ActionForm class code: UploadFileForm. java
Java code
// Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com. genuitec. eclipse. cross. easystruts. eclipse_4.0.0/xslt/JavaClass. xsl
Package testfile. forms;
Import javax. servlet. http. HttpServletRequest;
Import org. apache. struts. action. ActionErrors;
Import org. apache. struts. action. ActionForm;
Import org. apache. struts. action. ActionMapping;
Import org. apache. struts. action. ActionError;
Import org. apache. struts. upload. FormFile;
Import org. apache. struts. upload. MultipartRequestHandler;
/**
* MyEclipse Struts
* Creation date: 11-14-2005
*
* XDoclet definition:
* @ Struts. form name = "uploadForm"
*/
Public class UploadFileForm extends ActionForm {
Private FormFile file = null;
// Private String fname;
// Private String size;
// Private String url;
Public FormFile getFile (){
Return file;
}
Public void setFile (FormFile file ){
This. file = file;
}
// ************** If the verification file is empty, ***************** is returned *****************
Public ActionErrors validate (
ActionMapping mapping,
HttpServletRequest request ){
ActionErrors errors = new ActionErrors ();
If (file = null | file. getFileSize () <5 | file. getFileName () = null ){
Errors. add ("file", new ActionError ("error. file. null "));
}
Return errors;
}
Public void reset (ActionMapping mapping, HttpServletRequest request ){
}
}
4: code in the Action class: UploadFileAction. java
Java code
// Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com. genuitec. eclipse. cross. easystruts. eclipse_4.0.0/xslt/JavaClass. xsl
Package testfile. actions;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import org. apache. struts. action. Action;
Import org. apache. struts. action. ActionForm;
Import org. apache. struts. action. ActionForward;
Import org. apache. struts. action. ActionMapping;
Import org. apache. struts. upload. FormFile;
Import org. apache. struts. action. ActionErrors;
Import org. apache. struts. action. ActionError;
Import org. apache. struts. upload .*;
Import testfile. forms .*;
Import java. io .*;
/**
* MyEclipse Struts
* Creation date: 11-14-2005
*
* XDoclet definition:
* @ Struts. action validate = "true"
*/
Public class UploadFileAction extends Action {
Public ActionForward execute (
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
ActionErrors errors = new ActionErrors ();
UploadFileForm hff = (UploadFileForm) form;
FormFile file = hff. getFile ();
// ************* If the ActionForm is not verified, add this section *****************
// If (file = null | file. getFileSize () <10 | file. getFileName () = null ){
// Errors. add ("file", new ActionError ("error. file. null "));
// SaveErrors (request, errors );
// Return (new ActionForward (mapping. getInput ()));
//}
// ************* Obtain the information of the uploaded file ****************
String dir = servlet. getServletContext (). getRealPath ("/upload ");
String fname = file. getFileName ();
String size = Integer. toString (file. getFileSize () + "bytes ";
String url = dir + "\" + fname;
*******************
String fileType = (fname. substring (fname. lastIndexOf ('.') + 1). toLowerCase ();
If ((! FileType. equals ("jpg "))&&(! FileType. equals ("bmp "))&&(! FileType. equals ("gif "))&&(! FileType. equals ("jpeg "))){
Errors. add ("filetype", new ActionError ("error. file. type "));
SaveErrors (request, errors );
Return (new ActionForward (mapping. getInput ()));
}
// ************* The file cannot exceed 2 MB ******************
If (file. getFileSize () & gt; 2097152 ){
Errors. add ("size", new ActionError ("error. file. size "));
SaveErrors (request, errors );
Return (new ActionForward (mapping. getInput ()));
}
*****************
InputStream streamIn = file. getInputStream ();
OutputStream streamOut = new FileOutputStream (url );
// ************* Output the file to the target file *****************
Int bytesRead = 0;
Byte [] buffer = new byte [8192];
While (bytesRead = streamIn. read (buffer, 0,8192 ))! =-1 ){
StreamOut. write (buffer, 0, bytesRead );
}
StreamOut. close ();
StreamIn. close ();
File. destroy ();
Return mapping. findForward ("sucess ");
}
}
5: properties file content:
Xml Code
Error. file. size = The file is too large, the size must less than 2 M!
Error. file. null = Please choose the file!
Error. file. type = Type of the file must be ". jpg" or ". jpeg" or ". bmp" or ". gif "!
6: struts-config.xml code
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts-config PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 1.1 // EN"
Http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd>
<Struts-config>
<Form-beans>
<Form-bean name = "uploadForm" type = "testfile. forms. UploadFileForm"/>
</Form-beans>
<Global-forwards>
<Forward name = "sucess" path = "/sucess. jsp"/>
<Forward name = "index" path = "/uploadfile. jsp"/>
</Global-forwards>
<Action-mappings>
<Action path = "/uploadfile"
Name = "uploadForm"
Scope = "request"
Input = "/uploadfile. jsp"
Type = "testfile. actions. UploadFileAction"/>
</Action-mappings>
<Message-resources parameter = "com. yourcompany. struts. ApplicationResources"/>
</Struts-config>
7: web. xml Code
Xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE web-app PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
<Web-app>
<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> org. apache. struts. action. ActionServlet </servlet-class>
<Init-param>
<Param-name> config </param-name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-name> debug </param-name>
<Param-value> 3 </param-value>
</Init-param>
<Init-param>
<Param-name> detail </param-name>
<Param-value> 3 </param-value>
</Init-param>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> uploadfile. jsp </welcome-file>
</Welcome-file-list>
<Taglib>
<Taglib-uri>/WEB-INF/struts-html.tld </taglib-uri>
<Taglib-location>/WEB-INF/struts-html.tld </taglib-location>
</Taglib>
</Web-app>