struts2.0多文件上传(我自己用,好用,中文也可以上传)

这个页面是点一个添加就出一个浏览上传的

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<script language="javascript">
function addComponent()
{
var uploadHTML = document.createElement( "<input type='file'  name='upload'/>");
document.getElementById("files").appendChild(uploadHTML);
uploadHTML = document.createElement( "<p/>");
document.getElementById("files").appendChild(uploadHTML);
}
</script>
</head>
<body>
<s:fielderror/>
<input type="button" onclick="addComponent();" value="添加文件" /><br />
<form onsubmit="return true;" action="upload.action" method="post" enctype="multipart/form-data"><span id="files">
<input type='file' name='upload' /><p /></span>
<input type="submit" value="上传" />
</form>
</body>
</html>

写个action类

package org.qiujy.web.struts2;

import java.io.*;
import com.opensymphony.xwork2.ActionSupport;

public class UploadMoreAction extends ActionSupport {
private java.util.List uploads;
private java.util.List fileNames;
private java.util.List uploadContentTypes;

public java.util.List getUploadFileName() {
  return fileNames;
}

public void setUploadFileName(java.util.List fileNames) {
  this.fileNames = fileNames;
}

public java.util.List getUpload() {
  return uploads;
}

public void setUpload(java.util.List uploads) {
  this.uploads = uploads;
}

public void setUploadContentType(java.util.List contentTypes) {
  this.uploadContentTypes = contentTypes;
}

public java.util.List getUploadContentType() {
  return this.uploadContentTypes;
}

public String execute() throws Exception {
  if (uploads != null) {
   int i = 0;
   for (; i < uploads.size(); i++) {
    java.io.InputStream is = new java.io.FileInputStream((String) uploads.get(i));
    java.io.OutputStream os = new java.io.FileOutputStream(
      "e:\\mydir\\" + fileNames.get(i));
    byte buffer[] = new byte[8192];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
     os.write(buffer, 0, count);
    }
    os.close();
    is.close();
   }
  }
  return SUCCESS;
}
}

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="messages"/>
    <package name="fileUploadDemo" extends="struts-default">
               <action name="upload"
            class="org.qiujy.web.struts2.UploadMoreAction">
              <interceptor-ref name="fileUpload">
              <!-- 配置允许上传的文件类型,多个用","分隔 -->
            
                <param name ="allowedTypes" >
                <!--     image/bmp,image/png,image/gif,image/jpeg,image/jpg-->
                </param>      
              <!-- 配置允许上传的文件大小,单位字节 -->
              <param name="maximumSize">40000000</param>
           </interceptor-ref>
           <interceptor-ref name="defaultStack" />
            <result name="input">dddd.jsp</result>
            <result name="success">dddd.jsp</result>
        </action>
    </package>
</struts>

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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>struts2</filter-name>

  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

  <filter-name>struts2</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>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

写个提示错误的文件messages.properties

struts.messages.error.content.type.not.allowed=\u4E0D\u5141\u8BB8\u7684\u7C7B\u578B//类型
struts.messages.error.file.too.large=\u4F60\u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927//大小

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