Struts2——文件上传和下载

一、文件上传

1.单文件上传

先在WebRoot下创建一个upload文件夹:用于存放本地上传文件

导入13个jar包
在这里插入图片描述

fileUpload.jsp:上传文件的jsp页面,form的enctype属性要设置成"multipart/form-data"

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'fileUpload.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="fileUploadAction" method="post" enctype="multipart/form-data">
    		<s:textfield label="用户名"/><br/><br/>
    		<s:file name="file" label="上传文件"/><br/><br/>
    		<s:submit value="上传"/>
    	</form>
  </body>
</html>

FileUploadAction:继承ActionSupport,在execute方法中完成上传文件的功能

package com.action;

import java.io.File;

//单文件进行文件转存
//文件上传是指将本地的文件上传到服务器指定目录下,默认上传最大2M的文件
//文件上传需要设置form表单的属性enctype="multipart/form-data"
//还需要使用<s:file name="file" lable="上传文件">,这个name要与action中用户上传文件名设置一致

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

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
	//用户上传的文件
	private File file;
	//用户上传文件的文件名,必须是xxxFileName,只能以FileName结尾
	private String fileFileName;
	//上传文件类型,必须以ContentType结尾
	private String fileContentType;
	//提供get,set方法
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	@Override
	public String execute() throws Exception {
		//获取Servletcontext上下文对象,getRealPath("upload")获取upload的上传到tomcat服务器后的绝对路径(upload文件为所上传的文件新创建的文件夹)
		String uploadAbsolutePath = ServletActionContext.getServletContext().getRealPath("/upload");
		//创建一个目标文件对象,路径为uploadAbsolutePath,文件名为fileName,与原用户上传文件文件名一致
		File toFile=new File(uploadAbsolutePath,fileFileName);
		//FileUtils流对口,将用户上传文件直接copy到名为fileName的目标文件中
		FileUtils.copyFile(file, toFile);
		return SUCCESS;
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <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>
</web-app>

struts.xml文件:放在src文件夹下

<?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>
	<package name="struts2" namespace="/" extends="struts-default">
		<!-- 将FileUploadAction配置进来,执行成功跳到/result.jsp -->
		<action name="fileUploadAction" class="com.action.FileUploadAction">
			<result name="success">/result.jsp</result>
		</action>
	</package>
</struts>

result.jsp

<%@ 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>

  </head>
  
  <body>
    	文件上传成功
    	上传文件名:${fileFileName}
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值