Java三大框架之struts的上传文件

本文介绍使用Struts框架简化Java文件上传的过程。通过配置核心jar包、设置拦截器及编写简单的前端页面与Action类,即可轻松实现文件上传功能。

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

在没有struts之前,想要用Java上传文件还得写很多复杂的代码,利用servlet3.0的新特性,或者在servlet3.0之前比如借助其他的插件,比如commons-fileupload组件,或者其他的。并且代码写得十分复杂。利用struts框架进行上传文件核心代码只需要几行。但是框架用起来感觉有点复杂的就是搭建这个环境了。

首先应该包含核心jar包。这些文件可以从网上下载。我用到的核心jar包如下:


第二步:struts是基于拦截器实现的。因此需要在web.xml里面配置一个拦截器,把控制权交给struts来处理。

<filter>
    <filter-name>Struts2</filter-name>
    <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
第三步:在struts.xml文件中写action的配置

<?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>
	<package name="fileAction" extends="struts-default" namespace="/">
		<action name="MyfileUpload" class="com.levi.action.FileUpload">
			<result name="success">/fileUpload_Success.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="input">/fileUpload.jsp</result>
		</action>
	</package>

</struts>
第四步前端上传页面代码为:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="MyfileUpload" method="post" enctype="multipart/form-data">
	<input type="file" name="fileUpload"/><br>
	<button type="submit">上传</button>
</form>
<br>
错误信息如下:<s:fielderror></s:fielderror>
</body>
</html>
前端上传成功页面如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
文件上传成功<br>
文件名:<s:property value="#session.fileUploadFileName"/><br>
文件类型为:<s:property value="#session.fileUploadContentType"/><br>

</body>
</html>
最后编写处理Action的类

package com.levi.action;

import java.io.File;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private File fileUpload;
	private String fileUploadFileName;
	public File getFileUpload()
	{
		return fileUpload;
	}
	public void setFileUpload(File fileUpload)
	{
		this.fileUpload = fileUpload;
	}
	public String getFileUploadFileName()
	{
		return fileUploadFileName;
	}
	public void setFileUploadFileName(String fileUploadFileName)
	{
		this.fileUploadFileName = fileUploadFileName;
	}
	public String getFileUploadContentType()
	{
		return fileUploadContentType;
	}
	public void setFileUploadContentType(String fileUploadContentType)
	{
		this.fileUploadContentType = fileUploadContentType;
	}
	private String fileUploadContentType;
	@Override
	public String execute() throws Exception
	{
		ActionContext actionContext=ActionContext.getContext();
		Map<String, Object> session=actionContext.getSession();
		session.put("fileUploadFileName", fileUploadFileName);
		session.put("fileUploadContentType", fileUploadContentType);
		System.out.println("文件名为:"+fileUploadFileName);
		System.out.println("文件的类型为:"+fileUploadContentType);
		String filePath="d:/upload/"+fileUploadFileName;
		File saveFile=new File(filePath);
		FileUtils.copyFile(fileUpload, saveFile);
		return SUCCESS;
	}
}

然后就可以实现上传了。这个绝对简单很多的,只是前面配置有点麻烦。这个只实现了一个单文件的上传,后面可以增加一些配置,比如设定最大文件大小,上传类型等等,下一篇再详细实现




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值