eclipse学习(第二章:初识ssh)——9.Struts2 文件上传

前言

Struts2 框架为依据“基于表单的HTML文件上传”所进行的文件处理上传提供了内置支持。当文件上传时,它通常会存储在临时目录中,然后Action类应对其进行处理或移动到固定目录中,以确保数据不会丢失。
注意:服务器可能有适当的安全策略,禁止你写入临时目录以外的目录以及属于Web应用程序的目录。
通过一个名为FileUpload的预定义拦截器可以在Struts中上传文件,该拦截器可通过org.apache.struts2.interceptor.FileUploadInterceptor类获得,并作为defaultStack的一部分包含在内。你也将在接下来的内容中看到如何使用它在struts.xml文件中设置各种参数。


我是通过这个网站学习的
https://www.w3cschool.cn/struts_2/struts_file_uploads.html

创建项目

1、初始化项目及jar包

2、修改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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssh_learn_fileupload</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <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>

3、创建FileUploadAction类

FileUploadAction,这里的话,必须要注意,struts2提供的上传文件名命名规则:上传的字段名(form表单中的)+fileName ,如果你乱定义名称那么会获取不到值哦。
文件类型,struts2提供MIME类型命名规则:上传的字段名(form表单中的)+contentType

要注意的重点是,FileUpload拦截器和Parameters拦截器为我们承担了所有的重工作量。默认情况下,FileUpload拦截器为你提供三个参数,它们分别按以下方式命名

  • [文件名参数] - 这是用户已上传的实际文件。在这个例子中它将是“myTestFile”
  • [文件名参数]ContentType - 这是上传的文件的内容类型。在这个例子中,它将是“myTestFileContentType”
  • [文件名参数]FileName - 这是上传的文件的名称。在这个例子中,它将是“myTestFileFileName”

得益于Struts拦截器这三个参数均可供我们使用。我们要做的是在Action类中创建三个带有正确名称的参数,并使这些变量可以自动连接。所以,在下面的例子中,我们有三个参数和一个action方法。如果一切正常,则返回“success”,否则返回“error”。

package com.czx.fileUpload;

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

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

	public String getMyTestFileContentType() {
		return myTestFileContentType;
	}

	public void setMyTestFileContentType(String myTestFileContentType) {
		this.myTestFileContentType = myTestFileContentType;
	}

	public String getMyTestFileFileName() {
		return myTestFileFileName;
	}

	public void setMyTestFileFileName(String myTestFileFileName) {
		this.myTestFileFileName = myTestFileFileName;
	}

	public String getDestinationPath() {
		return destinationPath;
	}

	public void setDestinationPath(String destinationPath) {
		this.destinationPath = destinationPath;
	}

	public File getMyTestFile() {
		return myTestFile;
	}

	public void setMyTestFile(File myTestFile) {
		this.myTestFile = myTestFile;
	}

	//上传的原文件名称
	private File myTestFile;
	//提交上来的文件名(存放本地的文件名),struts2提供的上传文件名命名规则:上传的字段名(form表单中的)+fileName ,如果你乱定义名称那么会获取不到值哦
	private String myTestFileFileName;
	//文件类型,struts2提供MIME类型命名规则:上传的字段名(form表单中的)+contentType 
	private String myTestFileContentType;
	//文件目的地路径(实际保存在项目的路径)
	private String destinationPath;
	
	@Override
	public String execute() throws Exception {
		destinationPath= "E:\\重来尝试\\ssh上传文件夹\\";
		System.out.println("原文件名称:"+myTestFile);
		System.out.println("现文件名称:"+ myTestFileFileName);
		System.out.println("文件类型"+myTestFileContentType);
		
		try {
			//创建文件
			File destFile = new File(destinationPath,myTestFileFileName);
			//利用工具类复制文件
			FileUtils.copyFile(myTestFile, destFile);
		}catch(IOException e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
	
	
}

创建struts.xml文件

struts.xml,这里由于我是使用的默认继承的是2.3的,所以这里拦截器属性需要处理一下。
下面是一些控制文件上传过程的Struts2 配置属性:

序号属性和说明
1struts.multipart.maxSize
可接受的上传文件的最大值(以字节为单位),默认值为2M。
2struts.multipart.parser
用于上传多部分表单的库,默认为jakarta。
3struts.multipart.saveDir
存储临时文件的位置,默认是javax.servlet.context.tempdir。
<?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.devMode" value="true"></constant>
	<!-- 设置文件上传的最大值,可接受的上传文件的最大值(以字节为单位),默认值为250M。 -->
	<constant name="struts.multipart.maxsize" value="1000000"></constant>
	
	<package name="file" extends="struts-default">
		<action name="fileUpload" class="com.czx.fileUpload.FileUploadAction" method="execute">
			<interceptor-ref name="defaultStack">
       			<param name="fileUpload.allowedTypes">image/jpeg,image/gif</param>
       		</interceptor-ref> 
			
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

设置访问页面

index.jsp,默认访问页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
	<form action="fileUpload" method="post" enctype="multipart/form-data">
		<label>请上传文件</label><br/>
		<input name="myTestFile" type="file"/><br/>
		<input type="submit" value="提交"/><br/>
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	文件上传成功,请注意确认。<s:property value="myTestFileFileName"/>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误页面</title>
</head>
<body>
	文件上传失败,上传文件过程中发生了问题
</body>
</html>

测试

(温馨提示,这里请使用浏览器访问测试,eclipse内置浏览器会报错)访问http://localhost:8080/ssh_learn_fileupload/
在这里插入图片描述
选择文件后提交,正常情况都会到这里
在这里插入图片描述
后台输出内容如下:
在这里插入图片描述

遇到的问题:

点击提交后出现:
在这里插入图片描述
在网站的最下面还有一段话,意思是如果你设置struts.devMode这个属性为false就能,解决了,但是我觉得不应该这么解决。
在这里插入图片描述后台打印结果如下:
在这里插入图片描述
一看后台说是你没有放行对应的格式,那行,修改一下struts.xml的拦截器,让他能够顺便进来吧。
好了,放行“image/pjpeg”这种格式之后,又出现了一个新的问题,我转念一想,这个不是默认配置了吗?怎么突然就找不到了。好吧,自己配置那里在加上这个好了。

<constant name="struts.multipart.saveDir" value="javax.servlet.context.tempdir"></constant>

在这里插入图片描述
加上之后还是null,那么继续研究下吧。
先试着取消拦截器,然后一步步处理。取消拦截器后发现不再是null了,然后在慢慢思考下。
那么拦截器发生了问题,那么进一步去思考问题所在,后面发现原来是我的struts版本是2.3,所以里面的属性有所变更,要换一个属性对应就能解决了。

可能会出现的其他问题

fileUplaod拦截器使用几个默认的错误信息key:

序号错误信息key和说明
1struts.messages.error.uploading
无法上传文件时发生的常规错误。
2struts.messages.error.file.too.large
当上传的文件过大(由maximumSize指定)时发生。
3struts.messages.error.content.type.not.allowed
当上传的文件与指定的预期内容类型不匹配时发生。

项目地址

这个仓库里面的ssh_learn_fileupload
https://gitee.com/mrchen13427566118/ssh_learn.git

如果不会怎么弄到eclipse的话,就看这里
https://blog.csdn.net/weixin_43987277/article/details/116936221

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值