-
<%@ 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 'fileupload.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>
-
<!-- enctype 默认是 application/x-www-form-urlencoded -->
-
<form action="FileUpload2" enctype="multipart/form-data" method="post" >
-
-
用户名:<input type="text" name="usename"> <br/>
-
上传文件:<input type="file" name="file1"><br/>
-
-
<input type="submit" value="提交"/>
-
-
</form>
-
-
-
-
</body>
-
</html>
-
package com.struts2.fileupload;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
-
import org.apache.struts2.ServletActionContext;
-
-
import com.opensymphony.xwork2.ActionSupport;
-
-
/**
-
* 单个文件上传
-
* @author Administrator
-
* 上传文件其实是上传了两份,
-
*
-
* 首先将上传的文件保存到 default.properties 文件中 struts.multipart.saveDir键指定的目录中
-
* 默认是空的
-
* 保存在 Tomcat 6.0\work\Catalina\localhost\struts2目录下以.tmp后缀名的文件
-
*
-
* 如果要在 struts.multipart.saveDir 指定目录, 则可以在 src文件夹下 建一个 struts.properties,
-
* 覆盖 default.properties 的某些键值
-
*
-
* 还有一份是 存放在自己设定的目录下
-
*/
-
public class FileUpload extends ActionSupport {
-
-
private String usename ;
-
private File file1 ; //具体上传文件的 引用 , 指向临时目录中的临时文件
-
private String file1FileName ; // 上传文件的名字 ,FileName 固定的写法
-
private String file1ContentType ; //上传文件的类型, ContentType 固定的写法
-
-
public String getUsename() {
-
return usename;
-
}
-
public void setUsename(String usename) {
-
this.usename = usename;
-
}
-
public File getFile1() {
-
return file1;
-
}
-
public void setFile1(File file1) {
-
this.file1 = file1;
-
}
-
public String getFile1FileName() {
-
return file1FileName;
-
}
-
public void setFile1FileName(String file1FileName) {
-
this.file1FileName = file1FileName;
-
}
-
public String getFile1ContentType() {
-
return file1ContentType;
-
}
-
public void setFile1ContentType(String file1ContentType) {
-
this.file1ContentType = file1ContentType;
-
}
-
-
@Override
-
public String execute() throws Exception {
-
//获取文件存储路径
-
String path = ServletActionContext.getRequest().getRealPath("/upload");
-
//输出流
-
OutputStream os = new FileOutputStream(new File(path,file1FileName));
-
//输入流
-
InputStream is = new FileInputStream(file1);
-
-
byte[] buf = new byte[1024];
-
int length = 0 ;
-
-
while(-1 != (length = is.read(buf) ) )
-
{
-
os.write(buf, 0, length) ;
-
}
-
is.close();
-
os.close();
-
-
return SUCCESS;
-
}
-
-
-
-
-
}
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags" %>
-
-
<%
-
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 'filedemo.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/>
-
usename: <s:property value="usename" /><br/>
-
file: <s:property value="file1FileName"/><br/>
-
contentType: <s:property value="file1ContentType"/>
-
-
</body>
-
</html>
-
<%@ 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 'fileupload.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>
-
<!-- enctype 默认是 application/x-www-form-urlencoded -->
-
<form action="FileUpload2" enctype="multipart/form-data" method="post" >
-
-
用户名:<input type="text" name="usename"> <br/>
-
上传文件:<input type="file" name="file1"><br/>
-
上传文件: <input type="file" name="file1"><br/> <!-- 两个名字相同 都是file1 -->
-
<input type="submit" value="提交"/>
-
-
</form>
-
-
-
-
</body>
-
</html>
多文件上传用集合的方式
-
package com.struts2.fileupload;
-
-
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 org.apache.struts2.ServletActionContext;
-
-
import com.opensymphony.xwork2.ActionSupport;
-
-
/**
-
* 多文件上传,用集合的方式
-
* @author Administrator
-
*
-
*/
-
-
public class FileUpload2 extends ActionSupport {
-
-
private String usename ;
-
private List<File> file1 ;
-
private List<String> file1FileName ;
-
private List<String> file1ContentType ;
-
-
public String getUsename() {
-
return usename;
-
}
-
public void setUsename(String usename) {
-
this.usename = usename;
-
}
-
public List<File> getFile1() {
-
return file1;
-
}
-
public void setFile1(List<File> file1) {
-
this.file1 = file1;
-
}
-
public List<String> getFile1FileName() {
-
return file1FileName;
-
}
-
public void setFile1FileName(List<String> file1FileName) {
-
this.file1FileName = file1FileName;
-
}
-
public List<String> getFile1ContentType() {
-
return file1ContentType;
-
}
-
public void setFile1ContentType(List<String> file1ContentType) {
-
this.file1ContentType = file1ContentType;
-
}
-
-
@Override
-
public String execute() throws Exception {
-
-
//获取文件存储路径
-
String path = ServletActionContext.getRequest().getRealPath("/upload");
-
-
for(int i = 0 ; i < file1.size() ; i++ )
-
{
-
OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));
-
-
InputStream is = new FileInputStream(file1.get(i));
-
-
byte[] buf = new byte[1024];
-
int length = 0 ;
-
-
while(-1 != (length = is.read(buf) ) )
-
{
-
os.write(buf, 0, length) ;
-
}
-
-
is.close();
-
os.close();
-
-
}
-
-
return SUCCESS;
-
}
-
-
}
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags" %>
-
-
<%
-
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 'filedemo2.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/>
-
usename:<s:property value="usename"/><br/>
-
<!-- 遍历值 -->
-
<s:iterator value="file1FileName" id="f"> <!-- id是一个对象,目前是一个字符串集合 可任意命名 -->
-
文件:<s:property value="#f"/> <br/>
-
<!-- 这里也可以调用方法 <s:property value="#f.toUpperCase()"/> -->
-
</s:iterator>
-
-
-
</body>
-
</html>
<s:iterator value="file1FileName" id="f"> <!-- id是一个对象,目前是一个字符串集合 可任意命名-->
文件:<s:property value="#f"/> <br/>
<!-- 这里也可以调用方法 <s:property value="#f.toUpperCase()"/> toUpperCase()字符串的方法是把字母转为大写 -->
</s:iterator>
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。