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

在struts2中实现多个文件的上传
1,upload.jsp
<%@ 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..." onclick="addMore()"/>
  </td>
  </tr><tr>
  <td><s:submit/></td>
  <td><s:reset/></td>
  </tr>
  </table>
 
  </s:form>
  </body>
</html>
2,struts.xml配置文件
<!-- 文件的上传功能 -->
<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>
3,对应的action
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;
}
}
4,result.jsp
  <body>
  username:<s:property value="username"/><br>
  password:<s:property value="password"/><br>
  file:<s:property value="fileFileName"/><br>
  </body>

你可能感兴趣的:(java,apache,jsp,struts,OS)