1. 单个文件上传
1 <s:form method="post" action="upload" namespace="/" enctype="multipart/form-data"> 2 <s:file name="upload"/> 3 <s:submit/> 4 </s:form>
<action name="upload" class="tonny.study.struts.action.file.FileUploadAction"> <param name="savePath">F:\</param><!-- 文件保存路径 --> <result>/index.jsp</result> </action>
1 public class FileUploadAction extends ActionSupport { 2 private static final long serialVersionUID = 1L; 3 4 private File upload; //上传文件页面标签name属性 5 private String uploadContentType; //上传文件的类型, 格式固定: name+ContentType 6 private String uploadFileName; //上传文件的名称, 格式固定: name+FileName 7 8 private String savePath; //通过struts.xml配置的保存路径 9 10 @Override 11 public String execute() throws Exception { 12 FileOutputStream fos = new FileOutputStream(getSavePath() + getUploadFileName()); 13 FileInputStream fis = new FileInputStream(getUpload()); 14 15 byte[] buffer = new byte[1024]; 16 int len = 0; 17 while ((len = fis.read(buffer)) > 0) { 18 fos.write(buffer, 0, len); 19 } 20 21 fis.close(); 22 fos.close(); 23 24 return SUCCESS; 25 } 26 27 //省略一堆 getter/setter方法 28 }