在struts2中实现多个文件的上传

在struts2中实现多个文件的上传
[b]1,upload.jsp[/b]
<%@ page language="java" pageEncoding="GB18030"%>
<%@ 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 'upload.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">
-->

<script type="text/javascript">
function addMore(){

var td = document.getElementById("more");

var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");

input.type="file";
input.name="file";

button.type="button";
button.value="remove";

button.onclick = function() {
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}

td.appendChild(br);
td.appendChild(input);
td.appendChild(button);

}
</script>

</head>

<body>
<s:form action="upload" theme="simple" enctype="multipart/form-data" method="post">
<h3 style="color:red"><s:fielderror></s:fielderror></h3>
<table border="1" width="60%">
<tr>
<td>username:</td>
<td><s:textfield name="username"/></td>
</tr><tr>
<td>password:</td>
<td><s:password name="password"/></td>
</tr><tr>
<td>file:</td>
<td id="more">
<s:file name="file"/>
<input type="button" value="Add More..." οnclick="addMore()"/>
</td>
</tr><tr>
<td><s:submit/></td>
<td><s:reset/></td>
</tr>
</table>

</s:form>
</body>
</html>
[b]2,struts.xml配置文件[/b]
<!-- 文件的上传功能 -->
<action name="upload" class="com.test.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">92262</param>
<param name="allowedTypes">image/pjpeg,text/plain</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
[b]3,对应的action[/b]
package com.test.action;

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;

public class UploadAction extends ActionSupport {
private String username;
private String password;
//以单个文件的形式上传文件
// private File file;
// private String fileFileName;
// private String fileContentType;
//以集合的形式上传文件
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 getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// public File getFile() {
// return file;
// }
// public void setFile(File file) {
// this.file = file;
// }
// public String getFileFileName() {
// return fileFileName;
// }
// public void setFileFileName(String fileFileName) {
// this.fileFileName = fileFileName;
// }
// public String getFileContentType() {
// return fileContentType;
// }
// public void setFileContentType(String fileContentType) {
// this.fileContentType = fileContentType;
// }

public String execute() throws Exception{

for(int i = 0;i< file.size();i++){
//字节流 来构造文件输入流
InputStream is = new FileInputStream(file.get(i));
//写到目的文件的路径
String root = ServletActionContext.getRequest().getRealPath("/upload");
//写到目的文件中
/**
* this.getFileFileName():传上来的文件名字
* root:上传来的文件的路径
*/
File descFile = new File(root,this.getFileFileName().get(i));
//文件输出流
OutputStream os = new FileOutputStream(descFile);
//通过字节数组作为中间变量,完成输入输出的转换
byte[] buffer = new byte[400];
//读入到数组里的长度
int length = 0;
while((length = is.read(buffer)) >0){
os.write(buffer,0,length);
}
os.close();
is.close();
}

return SUCCESS;
}
}
[b]4,result.jsp[/b]
<body>
username:<s:property value="username"/><br>
password:<s:property value="password"/><br>
file:<s:property value="fileFileName"/><br>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值