Struts中的动态多个文件上传处理

多个文件上传可以定义一个数组来实现. 不过这个数组的长度必须要有一个值. 如果我们是动态的多个文件上传的话,就不好设置这个长度了.

 

这边可以用MAP来实现.从网上找的资料,Struts1 中大体如下:

ActionForm中顶一个FormFile的MAP或者list来实现.

private Map fileMap = new HashMap();

public FormFile getFile( int index )
{
return (FormFile) fileMap.get( new Integer( index ) );
}

public FormFile setFile( int index, FormFile file )
{
fileMap.put( new Integer( index ), file );
}

public FormFile getFiles()
{
return (FormFile[]) fileMap.values().toArray( new FormFile[fileMap.size()] );
}

然后再JSP中如下的写法:
<html:file property="file[0]"・・・
<html:file property="file[1]"・・・
动态个数为止.



Strut2中如下:
内容来自:http://hi.baidu.com/286759889/blog/item/78507f122a439728dc540162.html

文件上传:

1,upload.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
%>

<title>My JSP 'upload.jsp' starting page</title>

<!--下面script实现多文件上传-->
   <script type ="text/javascript">
  
   function onMore (){
    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="删除";
    //当点击删除时,删除一行。
    button.onclick = function(){
    
     td.removeChild(br);
     td.removeChild(input);
     td.removeChild(button);
    }
   
    // 下面三句增加一行。
    td.appendChild(br);
    td.appendChild(input);
    td.appendChild(button);
   }
  
   </script>

</head>

<body>
   <table align = "center">
    <tr>
     <td><s:fielderror cssStyle="color:red"></s:fielderror></td>
    </tr>
   </table>
   <s:form action="upload" theme="simple" method="post" enctype="multipart/form-data">
    <table align="center" border="1" width="60%">
         <tr>
     <td>文件</td>
     <td id = "more">
      <s:file name="file"></s:file>
      <input type = "button" value = "更多.." onclick = "onMore()">
     </td>
    </tr><br>
   
    <tr>
     <td>
      <s:submit value="submit"></s:submit>
     </td>
     <td>
      <s:reset value="reset"></s:reset>
     </td>
    </tr>
   </table>
   </s:form>
</body>
</html>

 

2.struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd ">

<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
   <!-- 上传文件编码 -->
   <constant name="struts.i18n.encoding" value="gbk"></constant>
   <!-- 上传文件临时文件位置 -->
   <constant name="struts.multipart.saveDir" value="c:/"></constant>

   <package name="struts2" extends="struts-default">

<action name="upload" class = "com.struct2.test.UploadAction">
     <result name = "success">/UpLoad/uploadresult.jsp</result>
     <result name = "input">/UpLoad/upload.jsp</result>
     <interceptor-ref name="fileUpload">
      <!-- 单个上传文件的最大值-->
      <param name="maximumSize">409600</param>
      <!-- 只能上传的文件的类型,可到tomcat的web-xml中查看各种文件类型-->
      <param name="allowedTypes">text/html</param>
     </interceptor-ref>
     <interceptor-ref name="defaultStack"></interceptor-ref>
    </action>

</package>

 

 

 

 

3.UploadAction.jsp:

 

package com.struct2.test;

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 List<File> file;
private List<String> fileFileName;//文件名+FileName
private List<String> fileContentType;//文件名+ContentType
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 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;
}

@Override
public String execute() throws Exception {
   for(int i = 0 ;i<file.size();++i){
    System.out.println(file.get(i));
    if(!file.get(i).exists())
    {return INPUT;}
    //拿到上传的文件
    InputStream is = new FileInputStream(file.get(i));
    //设置文件存储位置.
    String root = ServletActionContext.getRequest().getRealPath("/UpLoad");
    File destFile = new File(root,this.getFileFileName().get(i));
    //将上传得到的文件输出.
    OutputStream os = new FileOutputStream(destFile);
    byte[] buffer = new byte[400];
    int length = 0;
    while((length=is.read(buffer))>0){
     os.write(buffer,0,length);
    }
    is.close();
    os.close();
   }
   return SUCCESS;
}
}
</struts>

4.uploadresult.jsp

...............................

<body>
      username:<s:property value="username"/><br>
      password:<s:property value = "password"/><br>
      file:<s:property value = "fileFileName"/>
</body>

 

 

文件下载:

1.download.jsp:

<body>
   <a href = "download.action">下载</a>
</body>

2.struts.xml中配置:

<action name="download" class = "com.struct2.test.DownloadAction">
     <result name = "success" type = "stream">
     <!-- 设置为attachment,否则浏览器直接打开而不会出现下载页面 -->
      <param name="contentDisposition">attachment;filename=${fileName}</param>
      <!-- downloadFile为DownloadAction中的方法名中的属性-->
      <param name="inputName">downloadFile</param>
      <!-- 下载文件的类型,可到tomcat的web-xml中查看各种文件类型-->
      <param name="contentType">text/html</param>
      <!-- 输出时缓冲区的大小 -->
      <param name="bufferSize">4096</param>
     
     
     </result>

3.DowmloadAction.java

package com.struct2.test;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
//String fileName = "";
public InputStream getDownloadFile (){
  
   return ServletActionContext.getServletContext().getResourceAsStream("/下载中文文件测 试.html");
}

@Override
public String execute() throws Exception {
   //这里添加下载权限设置.
   return SUCCESS;
}

public String getFileName () {
        // DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
         String fileName = "下载中文文件测试.html";
       // fileName = "序列号(" + df.format(new Date()) + ").html";
         try {
             //设置下载文件名编码

     return new String(fileName.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            return "impossible.txt";
        }
}

}

国际化资源文件message.properties:

xwork.default.invalid.fieldvalue={0}/u8f93/u5165/u9519/u8bef

struts.messages.error.file.too.large=File too large
struts.messages.error.content.type.not.allowed=file type not be allowed



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值