转自:http://blog.csdn.net/itwit/article/details/7031785
环境:Myeclipse
前提:为Web项目添加Struts2支持后:
内容是struts2的文件上传汇总,包括单文件和多文件上传,记录下来,留作备份
Struts1实现文件上传的文章(http://blog.csdn.net/itwit/article/details/7031371)
1、单文件上传
1)JSP页面(simple.jsp)
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@taglib prefix = "s" uri = "/struts-tags"%>
- <html>
- <body>
- <H1><s:actionmessage/></H1>
- <s:form action = "simpleAction" enctype="multipart/form-data">
- <s:file name = "upload" label = "请输入要传入的文件名"></s:file>
- <s:submit value = "save"></s:submit>
- </s:form>
- </body>
- </html>
2)Action(SimpleUploadAction)
- package upload.singleUpload;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import com.opensymphony.xwork2.ActionSupport;
- public class SimpleUploadAction extends ActionSupport {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private File upload;//上传的文件
- private String uploadFileName;//上传的文件名称
- private String uploadContentType;//上传的文件类型
- @Override
- public String execute() throws Exception {
- InputStream in = new FileInputStream(upload);
- OutputStream out = new FileOutputStream("d:\\"+uploadFileName);
- byte[] buffer = new byte[8192];
- int count = 0;
- while((count = in.read(buffer)) > 0){
- out.write(buffer,0,count);
- }
- out.close();
- in.close();
- this.addActionMessage("上传成功");
- return SUCCESS;
- }
- public File getUpload() {
- return upload;
- }
- public void setUpload(File upload) {
- this.upload = upload;
- }
- public String getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(String uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- public String getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(String uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- }
3)Struts配置文件(struts.xml)
- <action name="simpleAction" class = "upload.singleUpload.SimpleUploadAction">
- <result name = "success">/upload/simple/simple.jsp</result>
- <result name = "input">/upload/simple/simple.jsp</result>
- </action>
2、多文件上传
1)JSP页面(mutity.jsp)
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@taglib prefix = "s" uri = "/struts-tags"%>
- <html>
- <head>
- <script language="javascript">
- function addComponent()
- {
- var uploadHTML = document.createElement( "<input type='file' name='upload'/>");
- document.getElementById("files").appendChild(uploadHTML);
- uploadHTML = document.createElement( "<p/>");
- document.getElementById("files").appendChild(uploadHTML);
- }
- </script>
- </head>
- <body>
- <input type="button" οnclick="addComponent();" value="添加文件" />
- <br />
- <form action="mutityUpload.action"
- method="post" enctype="multipart/form-data">
- <span id="files">
- <input type='file' name='upload' />
- </span>
- <input type="submit" value="上传" />
- </form>
- </body>
- </html>
2)Action(MutityUploadAction)
- package upload.mutityUpload;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.List;
- import com.opensymphony.xwork2.ActionSupport;
- public class MutityUploadAction extends ActionSupport {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private List<File> upload;
- private List<String> uploadFileName;
- private List<String> uploadContentType;
- @Override
- public String execute() throws Exception {
- for(int i = 0;upload != null && i<upload.size();i++){
- File file = upload.get(i);
- InputStream in = new FileInputStream(file);
- OutputStream out = new FileOutputStream("d:\\"+uploadFileName.get(i));
- byte[] buffer = new byte[8192];
- int count = 0;
- while((count = in.read(buffer)) > 0){
- out.write(buffer,0,count);
- }
- out.close();
- in.close();
- }
- this.addActionMessage("上传成功");
- return SUCCESS;
- }
- public List<File> getUpload() {
- return upload;
- }
- public void setUpload(List<File> upload) {
- this.upload = upload;
- }
- public List<String> getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(List<String> uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- public List<String> getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(List<String> uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- }
3)Struts配置文件(struts.xml)
- <action name="mutityUpload" class = "upload.mutityUpload.MutityUploadAction">
- <result name = "success">/upload/mutity/mutity.jsp</result>
- <result name = "input">/upload/mutity/mutity.jsp</result>
- </action>
经过对struts1和2的文件上传学习,总结两者的区别为:
struts2中不需要再单独定义form,在action中可以定义action需要用到的属性
struts1文件上传使用的类是FileForm,struts2使用的类是File
struts2较struts1降低了耦合度,却也增加了action的代码量,总体上来说是简洁了不少。