现在的位置: 主页 > 公司荣誉 > 文章正文

struts2上传多个文件,下载 配制!代码

作者:武警河南洛阳红丝带网 来源:www.027fck.com 未知发布时间:2017-09-07 13:20:30
struts2上传多个文件,下载 配制!代码 页面代码:

注意,我在页面在一个新建的文件夹下(TheNameSpace)

[html]

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

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

</head>

<body>

<h1>file upload</h1>

<s:form action="uploadList.action" enctype="multipart/form-data" theme="simple">

<table cellpadding="0" height="5" >

<tr>

<td>用户:</td>

<td>

<s:textfield name="username"/>

</td>

</tr>

<tr>

<td>密码:</td>

<td> <s:password name="passwrod"/></td>

</tr>

<tr >

<td>文件:</td>

<td id="fileID">

<s:fielderror name="file"/>

<s:file name="file" />

<a href="javascript:addFiles()" >添加上传</a>

</td>

</tr>

<tr>

<td colspan="2">

<s:submit value="提交"/>

<s:reset value="重置"/>

</td>

</tr>

</table>

</s:form>

<script language="javascript">

function addFiles(){

var tdID=document.getElementById("fileID");

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

var file=document.createElement("input");

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

file.type="file";

file.name="file";

button.type="button";

button.value="remove";

button.onclick=function(){

tdID.removeChild(br);

tdID.removeChild(file);

tdID.removeChild(button);

}

tdID.appendChild(br);

tdID.appendChild(file);

tdID.appendChild(button);

}

</script>

</body>

</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

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

</head>

<body>

<h1>file upload</h1>

<s:form action="uploadList.action" enctype="multipart/form-data" theme="simple">

<table cellpadding="0" height="5" >

<tr>

<td>用户:</td>

<td>

<s:textfield name="username"/>

</td>

</tr>

<tr>

<td>密码:</td>

<td> <s:password name="passwrod"/></td>

</tr>

<tr >

<td>文件:</td>

<td id="fileID">

<s:fielderror name="file"/>

<s:file name="file" />

<a href="javascript:addFiles()" >添加上传</a>

</td>

</tr>

<tr>

<td colspan="2">

<s:submit value="提交"/>

<s:reset value="重置"/>

</td>

</tr>

</table>

</s:form>

<script language="javascript">

function addFiles(){

var tdID=document.getElementById("fileID");

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

var file=document.createElement("input");

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

file.type="file";

file.name="file";

button.type="button";

button.value="remove";

button.onclick=function(){

tdID.removeChild(br);

tdID.removeChild(file);

tdID.removeChild(button);

}

tdID.appendChild(br);

tdID.appendChild(file);

tdID.appendChild(button);

}

</script>

</body>

</html>

struts.xml配制文件:

[html]

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

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!-- 常量 上传文件 最大大小 -->

<constant name="struts.multipart.maxSize" value="20971520"/>

<!-- 包括这个xml -->

<include file="strtus_1.xml"/>

<!-- 后台action -->

<package name="back" extends="struts-default" namespace="/TheNameSpace">

<action name="logins" class="com.rui.struts2.SpaceLogin">

<result name="success">showUser.jsp</result>

</action>

<action name="uploadList" class="com.rui.struts.UploadList">

<result name="success">ok.jsp</result>

<result name="input">/${pageContext.request.contextPath}/TheNameSpace/uploadList.jsp</result>

<!--拦截上上传文件的 大小、格式 -->

<interceptor-ref name="fileUpload">

<param name="setAllowedTypes">image/pjpeg,image/gif,image/bmp,image/jpeg,image/jpg, text/plain, application/java-archive</param>

<param name="maximumSize">524288</param>

</interceptor-ref>

<interceptor-ref name="basicStack"/>

</action>

<action name="upload" class="com.rui.struts.Upload">

<result name="success">ok.jsp</result>

<result name="input">upload.jsp</result>

</action>

<action name="download" class="com.rui.struts.DownLoald">

<result type="stream">

<!-- 要下载的文件

<param name="contentDisposition">attachment;filename="bbbb.txt"</param>

-->

<!-- 自动寻找方法 -->

<param name="inputName">downloadFile</param>

</result>

</action>

<action name="showlist" class="com.rui.struts.ShowFileList">

<result>download.jsp</result>

</action>

</package>

</struts>

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

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!-- 常量 上传文件 最大大小 -->

<constant name="struts.multipart.maxSize" value="20971520"/>

<!-- 包括这个xml -->

<include file="strtus_1.xml"/>

<!-- 后台action -->

<package name="back" extends="struts-default" namespace="/TheNameSpace">

<action name="logins" class="com.rui.struts2.SpaceLogin">

<result name="success">showUser.jsp</result>

</action>

<action name="uploadList" class="com.rui.struts.UploadList">

<result name="success">ok.jsp</result>

<result name="input">/${pageContext.request.contextPath}/TheNameSpace/uploadList.jsp</result>

<!--拦截上上传文件的 大小、格式 -->

<interceptor-ref name="fileUpload">

<param name="setAllowedTypes">image/pjpeg,image/gif,image/bmp,image/jpeg,image/jpg, text/plain, application/java-archive</param>

<param name="maximumSize">524288</param>

</interceptor-ref>

<interceptor-ref name="basicStack"/>

</action>

<action name="upload" class="com.rui.struts.Upload">

<result name="success">ok.jsp</result>

<result name="input">upload.jsp</result>

</action>

<action name="download" class="com.rui.struts.DownLoald">

<result type="stream">

<!-- 要下载的文件

<param name="contentDisposition">attachment;filename="bbbb.txt"</param>

-->

<!-- 自动寻找方法 -->

<param name="inputName">downloadFile</param>

</result>

</action>

<action name="showlist" class="com.rui.struts.ShowFileList">

<result>download.jsp</result>

</action>

</package>

</struts>

action 上传类的代码:

[html]

package com.rui.struts;

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 UploadList extends ActionSupport {

private String username;

private String passwrod;

private List<File> file;

private List<String> fileFileName;

private List<String> fileContentType;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPasswrod() {

return passwrod;

}

public void setPasswrod(String passwrod) {

this.passwrod = passwrod;

}

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 void validate() {

System.out.println("执行了验证器...");

if(null==file){

addFieldError("file", "请选择文件!");

}

}

@Override

public String execute() throws Exception {

if(null==file||file.size()<=0){

addFieldError("file", "请上传符合格式的文件....");

return INPUT;

}

//获得路么

String path=ServletActionContext.getRequest().getRealPath("upload");

for(int i=0;i<file.size();i++){

//输入流

InputStream is=new FileInputStream(file.get(i));

//File 对象

File fileObj=new File(path,fileFileName.get(i));

//输出流

OutputStream os=new FileOutputStream(fileObj);

byte [] by=new byte[400];

int length=0;

while(-1!=(length=is.read(by))){

os.write(by,0,length);

}

os.close();

is.close();

}

return SUCCESS;

}

}

package com.rui.struts;

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 UploadList extends ActionSupport {

private String username;

private String passwrod;

private List<File> file;

private List<String> fileFileName;

private List<String> fileContentType;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPasswrod() {

return passwrod;

}

public void setPasswrod(String passwrod) {

this.passwrod = passwrod;

}

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 void validate() {

System.out.println("执行了验证器...");

if(null==file){

addFieldError("file", "请选择文件!");

}

}

@Override

public String execute() throws Exception {

if(null==file||file.size()<=0){

addFieldError("file", "请上传符合格式的文件....");

return INPUT;

}

//获得路么

String path=ServletActionContext.getRequest().getRealPath("upload");

for(int i=0;i<file.size();i++){

//输入流

InputStream is=new FileInputStream(file.get(i));

//File 对象

File fileObj=new File(path,fileFileName.get(i));

//输出流

OutputStream os=new FileOutputStream(fileObj);

byte [] by=new byte[400];

int length=0;

while(-1!=(length=is.read(by))){

os.write(by,0,length);

}

os.close();

is.close();

}

return SUCCESS;

}

}

下载页面测试 :

[html]

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

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

</head>

<body>

<s:iterator value="listName" var="lname">

<s:property value="lname"/><br/><br/>

<a href="download.action?lname=<s:property value='#lname'/>" >下载文件<a>

<!--

<a href="download.action?lname=DWHJ_062001.jpg">下载文件</a>

-->

</s:iterator>

<br/><br/>

</body>

</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

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

</head>

<body>

<s:iterator value="listName" var="lname">

<s:property value="lname"/><br/><br/>

<a href="download.action?lname=<s:property value='#lname'/>" >下载文件<a>

<!--

<a href="download.action?lname=DWHJ_062001.jpg">下载文件</a>

-->

</s:iterator>

<br/><br/>

</body>

</html>

action 下载代码:

[html]

package com.rui.struts;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoald extends ActionSupport {

private String lname;

public String getLname() {

return lname;

}

public void setLname(String lname) {

this.lname = lname;

}

private String slname="" ;

public InputStream getDownloadFile(){

HttpServletResponse response=ServletActionContext.getResponse();

try {

slname=new String(lname.getBytes("ISO-8859-1"),"UTF-8");

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

lname=java.net.URLEncoder.encode(slname,"utf-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("ddd"+lname);

response.setHeader("Content-Disposition","attachment;filename="+lname);

return ServletActionContext.getServletContext().

getResourceAsStream("upload/"+slname);

}

//文件名如果有中文的话要进行uri中文转码

/*String encodFileNmae="";

try {

encodFileNmae=java.net.URLEncoder.encode(u.getOldname(),"utf-8");

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

//设置一个请求头告诉浏览器有文件要下载

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:神农架SEO http://shennongjia.4567w.com

上一篇:php-在WINDOWS中设置计划任务执行PHP文件 下一篇:最后一页