struts2配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="front" namespace="/" extends="struts-default">
<action name="upload" class="action.UplodeAction" method="upload">
<param name="savePath">/uploadFile</param>
<result>/success.jsp</result>
</action>
</package>
</struts>
action配置
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UplodeAction extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private String savePath;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
public String upload() throws Exception {
System.out.println("upload=============");
for (int i = 0; i < upload.length; i++) {
System.out.println("======================="+i);
FileInputStream fis = new FileInputStream(upload[i]);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getTitle()+uploadFileName[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
index页面即上传界面的设置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:form action="/upload" enctype="multipart/form-data">
<s:textfield name="title" label="文件名称" />
<s:file name="upload" label="选择文件" />
<s:file name="upload" label="选择文件" />
<s:file name="upload" label="选择文件" />
<s:submit value="开始上传" />
</s:form>
</body>
</html>
上传成功的设置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.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>
上传成功!
<br /> 文件标题:
<s:property value="title" />
<br /> 文件为:
<s:iterator value="uploadFileName" var="fileName">
<img src="<s:property value="'uploadFile/'+title+ #fileName"/>" />
</s:iterator>
<s:debug />
</body>
</html>
web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Struts2Uplode</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
分享到:
相关推荐
struts2实现多文件上传
一个swfupload+struts2实现多文件上传的案例。
struts2 实现多文件上传功能,是一个很好的学习参考资料
文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...
ajaxFileUpload+struts2实现多文件上传(动态添加文件上传框)(项目源码) 博文地址:http://blog.csdn.net/itmyhome1990/article/details/36433621
ajaxFileUpload+struts2实现多文件上传(源码) 博文地址:http://blog.csdn.net/itmyhome1990/article/details/36396291
extjs3.2+struts2实现多文件上传excel并插入到数据库
struts2实现文件上传,解压后直接导入eclipse即可运行。
struts2实现的文件上传struts2实现的文件上传struts2实现的文件上传struts2实现的文件上传
struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传
在struts2基础上实现的包括单文件、多文件的上传及下载
自己做的一个struts2 实现文件批量上传功能,
struts与hibernate实现文件的上传与动态下载 主要就是运用struts2.2和hibernate3.6进行文件上传与下载,并且能动态的下载,里面包括了详细的构建文档,以及代码
本文重点给大家介绍AjaxFileUpload+Struts2实现多文件上传功能,具体实现代码大家参考下本文。 单文件和多文件的实现区别主要修改两点, 一是插件ajaxfileupload.js里接收file文件ID的方式 二是后台action是数组形式...
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
用Struts2实现压缩文件上传到Tomcat服务器该工程指定目录下,用JS实现上传控制。
网上有些这样的例子,但是下了几个都没有跑起来,哎,希望那些发文章的人要发就发全的,别发个半生不熟的。... 现在自己整理了一个Struts2+ExtJS2实现文异步文件上传,没法上传图片无法看到效果,直接上源码吧。
struts2实现的文件上传下载,上传文件重命名(按时间戳)