struts2上传多个文件

本文详细介绍了使用Apache Commons FileUpload实现文件上传及如何通过多线程提高上传效率的过程,包括配置、表单设置、Action类实现、线程类创建及Struts XML配置等关键步骤。

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

前面的配置和上传单个文件差不多,只是在Action类中不同,而且加入了线程的操作。

1.在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar两个包。(如果当前项目在之前配置好了struts就不需要重复添加了)

2.在form表单中enctype设置为:"multipart/form-data",如下:<form action="dofile/fileac" enctype="multipart/form-data" method="post">

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
   <form action="dofile/fileac" enctype="multipart/form-data" method="post">
   <input type="file" name="newfile"><br/>
   <input type="file" name="newfile"><br/>
   <input type="file" name="newfile"><br/>
   <input type="submit">
   </form>
  </body>
</html>


 

3.建立FileAction类 声明属性(我设计的该类在com.action包内)

需要说明的是你定义的这些属性的名字,newfile必须和表单中的name的值一样。其他属性的名字前缀必须也是表单中name的值后面加上FileName获得它的文件名称。如果名字不一致会报错。因为上传的是多个文件,因此全都变为数组

package com.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.tool.FileUploadTool;

public class FileAction {

	private File newfile[];
	private String newfileFileName[];
	public File[] getNewfile() {
		return newfile;
	}
	public void setNewfile(File[] newfile) {
		this.newfile = newfile;
	}
	public String[] getNewfileFileName() {
		return newfileFileName;
	}
	public void setNewfileFileName(String[] newfileFileName) {
		this.newfileFileName = newfileFileName;
	}
	public String execute(){
		
		String path=ServletActionContext.getServletContext().getRealPath("/Images");
		for (int i = 0; i < newfile.length; i++) {
			//启动线程
			new FileUploadTool(newfile[i], path+"/"+newfileFileName[i]).start();
		}
		return "success";
	}
	
}

4.利用线程进行操作,写了一个线程类(我放在com.tool包下)

package com.tool;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class FileUploadTool extends Thread{

	private File f;
	private String path;
	
	public FileUploadTool(File f,String path) {
		this.f=f;
		this.path=path;
	}
	@Override
	public void run() {
		try {
			FileUtils.copyFile(f, new File(path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


5.配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="104857600"></constant>
	<package name="file" extends="struts-default" namespace="/dofile">
		<action name="fileac" class="com.action.FileAction">
			<result name="success">/welcome.jsp</result>
		</action>
	</package>
</struts>    


 

 

这是用刚学的文件上传写的一个例子 希望大家多多指教。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值