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

本文介绍了一个基于Struts2框架实现的文件上传和下载功能。具体包括使用Struts2表单上传文件、配置文件类型及大小限制、处理文件上传逻辑及下载页面的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面代码:

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

<%@ 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配制文件:

<?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 上传类的代码:

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;
	}
	

}


下载页面测试 :

<%@ 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 下载代码:

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();
			}
			
			//设置一个请求头告诉浏览器有文件要下载
			response.setContentType("text/html;charset=utf-8");		
			response.setHeader("Content-Disposition","attachment;filename="+encodFileNmae);
	*/
	
	
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}

}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值