struts2详解(二)---->>单个文件上传
实现原理:
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。
具体实现
先要引入必要的jar包:
1、首先,创建文件上传页面index.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<!-- enctype="multipart/form-data" 这个属性是必须得有的 -->
<form action="csdn/upload.action" method="post"
enctype="multipart/form-data">
<table border="1px">
<tr>
<td>
标题:
</td>
<td>
<input type="text" name="title" />
</td>
</tr>
<tr>
<td>
文件:
</td>
<td>
<input type="file" name="pic" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="上传" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
备注:
在index.jsp中,先将表单的提交方式设为POST,然后将enctype设为multipart/form-data
2、其次是UserAction.java代码
package cn.csdn.struts2.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/**
* @author z_xiaofei168
*/
private static final long serialVersionUID = 1L;
/** 标题 */
private String title;
/** 获取文件 文件名与视图层的名称一致 */
private File pic;
/** 获取文件的类型 必须是: 文件名+ContentType */
private String picContentType;
/** 获取文件的名称 必须是: 文件名+FileName */
private String picFileName;
/** 保存文件路径 /保存文件的文件名称 */
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public String getSavePath() {
/**获取上下的路径*/
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
/** copy读取 */
public String uploadFile(){
System.out.println("savePath:"+savePath);
System.out.println("pic:"+pic);
System.out.println("picContentType:"+picContentType);
System.out.println("picFileName:"+picFileName);
System.out.println("getSavePath():"+getSavePath());
/**保存的具体路径*/
String savepath = getSavePath();
/**根据保存的路径创建file对象*/
File file = new File(savepath);
if(!file.exists()){
/**创建此文件对象路径*/
file.mkdirs();
}
try {
/**使用的是:org.apache.commons.io.FileUtils FileUtils*/
FileUtils.copyFile(pic, new File(file,getPicFileName()));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
3、配置文件: struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="csdn" extends="struts-default" namespace="/csdn">
<global-results>
<result name="input">/index.jsp</result>
</global-results>
<action name="upload" class="cn.csdn.struts2.action.UserAction" method="uploadFile">
<!-- 文件保存的文件名称 -->
<param name="savePath">/uploads</param>
<result name="success">/sc.jsp</result>
</action>
</package>
</struts>
4、下面我们就来看看上传成功的页面:
分享到:
相关推荐
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
struts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jarstruts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jar
赠送Maven依赖信息文件:struts2-json-plugin-2.3.24.pom; 包含翻译后的API文档:struts2-json-plugin-2.3.24-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.struts:struts2-json-plugin:2.3.24; ...
struts2-json-plugin,Struts JSON插件
struts2-ssl-plugin-1.2.1.jar
struts2-convention-plugin-2.3.32
struts2-dojo-plugin-2.3.4 jar 日期控件的jar包 需要的自行下载
struts2-convention-plugin-2.3.15.1.jar
struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar
struts2-convention-plugin-2.3.24.1
struts2-dojo-plugin-2.2.1.jar, 直接导入到Struts2项目里即可,注意版本。
-- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --> <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...
默认的struts2-config-browser-plugin包中的ftl文件include标签路径用的相对路径,会找到包内的include文件,将包内ftl里include的路径改成的/开头的全路径。
struts2-dojo-plugin-2.3.15.3.jar.zip
struts2-jquery-plugin-3.1.0.jar
struts2-core-2.5.18.jar包下载,支持struts2的类库下载
struts2-core-2.5.10.jar ,struts核心包,struts2-core-2.5.10.jar
struts2-jfreechart-plugin-2.1.8.1.jar
struts2-convention-plugin-2.1.6.jar
struts2-spring-plugin-2.3.31.jar