文件上传
今天学习到Struts2的文件上传部分,由于风中叶的视频声音和画面对不起来,于是自己写了一个。代码贡献出来,算是自己初步接触Struts2文件上传的纪念吧。
1、建立web项目,名字为FileUpload
2、导入Struts2的jar包
项目图如下:
upload.jsp的代码为
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 4 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 6 <html> 7 <head> 8 9 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 10 11 <title>上传文件</title> 12 13 </head> 14 15 <body> 16 17 <!-- 上传文件表单定义 --> 18 19 <s:form action="upload" method="post" enctype="multipart/form-data"> 20 21 22 23 24 <tr> 25 26 <td>username:</td> 27 28 <td><s:textfield name="username"></s:textfield></td> 29 30 </tr> 31 <tr> 32 33 <td>password:</td> 34 35 <td><s:password name="password"></s:password></td> 36 37 </tr> 38 39 <tr> 40 41 <!-- 上传文件标签定义 --> 42 43 <td>上传文件:<s:file name="file"></s:file></td> 44 45 </tr> 46 47 <tr> 48 49 <td align="left"><s:submit name="submit" value="提交"></s:submit></td> 50 51 </tr> 52 53 </s:form> 54 55 </body> 56 57 </html>
因为看了风老师的视频,想与之对应下面的学习,所以添加了username和password。
result.jsp的代码如下
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>上传结果</title> </head> <body> userName:${requestScope.username }<br> password:${requestScope.password }<br> 上传文件:<s:property value="fileFileName" /> </body> </html>
UploadAction.java代码如下:
package org.lsj; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private final static String UPLOADDIR = "/upload"; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private List<File> file; private List<String> fileFileName; private List<String> fileContentType; public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } public String execute() throws Exception { for (int i = 0; i < file.size(); i++) { uploadFile(i); } return SUCCESS; } //上传 private void uploadFile(int i) throws FileNotFoundException, IOException { try { InputStream in = new FileInputStream(file.get(i)); String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR); File uploadFile = new File(dir, this.getFileFileName().get(i)); OutputStream out = new FileOutputStream(uploadFile); byte[] buffer = new byte[1024 * 1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } }
web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>Struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>Struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
最后是Struts.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="C04.4" extends="struts-default"> <action name="upload" class="org.lsj.UploadAction"> <result name="input">/upload.jsp</result> <result name="success">/result.jsp</result> </action> </package> </struts>
然后运行成功!