在<samples工程目录>"src"actionform目录中建立一个UploadForm.java文件,代码如下:
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm
{
private FormFile myFile; // 这个myFile代表要上传的文件
public FormFile getMyFile()
{
return myFile;
}
public void setMyFile(FormFile myFile)
{
this.myFile = myFile;
}
}
【第3步】建立Struts动作类(Action的子类)
在Struts中,一般在Struts的动作类中处理上传的文件。在<samples工程目录>"src"action目录中建立一个UploadAction.java文件,代码如下:
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import java.io.*;
import actionform.*;
public class UploadAction extends Action
{
protected void saveFile(FormFile formFile) throws Exception
{
// 从web.xml文件中获得指定的上传路径
String path = this.getServlet().getServletConfig().getInitParameter("uploadPath");
InputStream in = formFile.getInputStream(); // 获得上传文件的InputStream
// 在服务端指定的上传路径中建立一个空的文件(文件名为getFileName()方法返回的值)
FileOutputStream fout = new FileOutputStream(path + formFile.getFileName());
byte buffer[] = new byte[8192];
int count = 0;
// 开始向上传路径中刚建立的文件写入数据,每次写8k字节
while ((count = in.read(buffer)) > 0)
{
fout.write(buffer, 0, count);
}
fout.close();
formFile.destroy(); // 上传成功后,销毁当前上传文件的资源
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
UploadForm uForm = (UploadForm) form;
PrintWriter out = null;
try
{
response.setCharacterEncoding("GBK");
out = response.getWriter();
saveFile(uForm.getMyFile()); // 将上传文件保存到指写的路径(在web.xml中配置)
out.println("上传文件成功.");
}
catch (Exception e)
{
out.println(e.getMessage());
}
return null;
}
}
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> 在saveFile方法中,首先从web.xml的Servlet初始化参数中获得一个用于保存上传文件的路径。然后从FormFile对象中获得上传文 件的InputStream对象,并对这个InputStream对象中的字节进行循环读取,并写到新的文件中。最后,在上传成功后,将FormFile 对象销毁。
【第4步】配置struts-config.xml
在这一步来配置一下在第2步和第3步分别建立的ActionForm和Action的子类。打开struts-config.xml文件,在<form-beans>中加入如下的子标签:
在<action-mappings>中加入如下的子标签:
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> 【第5步】设置用于保存上传文件的路径
打开web.xml文件,找到一个叫action的Servlet(也就是用于处理Struts动作的Servlet),并在<servlet>中加入如下的子标签(假设保存上传文件的路径为D:"upload",路径的最后一个字符必须是“"”):
<param-name>uploadPath</param-name>
<param-value>D:\upload\</param-value>
</init-param>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
【第6步】限制上传文件的大小
这一步是可选的,如果不限制上传文件的大小,就意味着可以上传任意大小的文件。而一般的应用程序,如电子相册,网络硬盘都会限制上传文件的最大尺寸。
打开struts-config.xml文件,在<struts-config>中加入如下的子标签:
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> 上面的<controller>标签将上传文件的最大尺寸设为2M,maxFileSize属性值的单位可以是M,也可以是K或G,如2K,5G等。
启动Tomcat后,在IE中输入如下的URL来测试程序:
http://localhost:8080/samples/uploadFile.jsp