多文件上传

如果页面中有多个文件域要实现上传,可以分别为每个文件域提供3个属性,分别封装该文件域对应的文件名、文件类型和文件内容

1. uploadAction

public class UploadAction extends ActionSupport
{
 private String title;
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;

 //スモハワメタタオラ「ネ�トハヤ
    private String savePath;
 //スモハワメタタオラ「ネ�トキスキィ
    public void setSavePath(String value)
 {
        this.savePath = value;
    }

    private String getSavePath() throws Exception
 {
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
 
 public void setTitle(String title) {
  this.title = title;
 }

 public void setUpload(List<File> upload) {
  this.upload = upload;
 }

 public void setUploadContentType(List<String> uploadContentType) {
  this.uploadContentType = uploadContentType;
 }

 public void setUploadFileName(List<String> uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getTitle() {
  return (this.title);
 }

 public List<File> getUpload() {
  return (this.upload);
 }

 public List<String> getUploadContentType() {
  return (this.uploadContentType);
 }

 public List<String> getUploadFileName() {
  return (this.uploadFileName);
 }
 @Override
    public String execute() throws Exception
 {
  List<File> files = getUpload();
  for (int i = 0 ; i < files.size() ; i++)
  {
   FileOutputStream fos = new FileOutputStream(getSavePath() + "//" + getUploadFileName().get(i));
   FileInputStream fis = new FileInputStream(files.get(i));
   byte[] buffer = new byte[1024];
   int len = 0;
   while ((len = fis.read(buffer)) > 0)
   {
    fos.write(buffer , 0 , len);
   }
  }
        return SUCCESS;
    }
}

 

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="globalMessages"/>
 <constant name="struts.i18n.encoding" value="GBK"/>
 <package name="upload" extends="struts-default">
 
  <action name="upload" class="lee.UploadAction">
            <interceptor-ref name="fileUpload">
             <param name="maximumSize">20000000</param>
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,text/plain,application/octet-stream</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"/>   
            <param name="savePath">/upload</param>
   <result name="input">/upload.jsp</result> 
   <result>/succ.jsp</result> 
  </action>
  
 </package>
</struts> 

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter>
        <filter-name> struts-cleanup </filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 <welcome-file-list>
  <welcome-file>upload.jsp</welcome-file>
 </welcome-file-list>

</web-app>

4.download.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>使用List上传多个文件</title>
</head>
<body>
<s:fielderror/>
<form action="upload.action" method="post" enctype="multipart/form-data">
   文件标题:<input type="text" name="title" /><br>
   选择第一个文件:<input type="file" name="upload" /><br>
   选择第二个文件:<input type="file" name="upload" /><br>
   选择第三个文件:<input type="file" name="upload" /><br>
 <input value="上传" type="submit" />
</form>
</body>
</html>

5. succ.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>上传成功</title>
    </head>
    <body>
        上传成功!<br>
  文件标题:<s:property value=" + title"/><br>
  第一个文件为:<img src="<s:property value="'upload/' + uploadFileName[0]"/>"/><br>
  第二个文件为:<img src="<s:property value="'upload/' + uploadFileName[1]"/>"/><br>
  第三个文件为:<img src="<s:property value="'upload/' + uploadFileName[2]"/>"/><br>

    </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值