Struts2 file upload and file download

The Struts2 default interceptor stack contains a file upload interceptor, and its underlying implementation relies on apache's commons.fileUpload, and web forms can directly upload files through the Struts2 file upload interceptor.

The interceptor automatically stores the files uploaded by the client in the system temporary directory. What Struts2 Action needs to do is to transfer the files in the temporary directory to the specified directory through the IO stream.

Note:
① Pay attention to the file upload size limit in struts2-core-2.3.37.jar\org.apache.struts2\default.properties when uploading files.
Insert picture description here
② The JSP page file upload form form needs to add enctype="multipart/form- Data"
Insert picture description here
file upload Action:

package action;

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

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

public class UploadAction {

	private File[] images;
	// 此处命名必须以File类型文件属性名称 + FileName结尾,否则无法识别
	private String[] imagesFileName;

	// 此处省略字段的Getters和Setters

	public String execute() {
		// 考虑images[]未创建的情况,预防未选择任何文件而直接提交时触发的空指针异常
		if (null != images) {
			for (int i = 0; i < images.length; i++) {
				try {
					// 实际Web项目无权操作客户机的文件路径,仅项目路径可供使用
					String path = ServletActionContext.getServletContext().getRealPath("/images");
					File destFile = new File(path, imagesFileName[i]);
					FileUtils.copyFile(images[i], destFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return "success";
		}
		return "message";
	}

}

When the server sends the file types supported by the browser (such as txt, jpg, etc.) to the client, it will be directly displayed in the browser. On the contrary, if the server requires the user to save the file in the form of an attachment, it is also called file download. To provide the file download function to the browser, you need to set the Content-Disposition=attachment of the HTTP response header.

The Action class needs to provide two attributes: file input stream (used to specify the server to provide downloaded file resources to the client) and file name (the name of the resource file downloaded by the user), and the type of the result tag of the corresponding Action in the struts.xml configuration file Should be set to stream.

File download Action:

package action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

public class DownloadAction {

	private InputStream is;
	// 此处的文件名称无需仿照文件上传时的xxxFileName,命名规范即可
	private String fileName;

	// 此处省略字段的Getters和Setters

	public String execute() throws UnsupportedEncodingException {
		fileName = "16.jpeg"; // 将项目pojoImgs下的16.jpeg作为资源下载目标
		is = ServletActionContext.getServletContext().getResourceAsStream("/pojoImgs/" + fileName);
		// 手动指定资源下载时的文件名称,实际宜从DB中加载
		fileName = "下载测试.jpeg";
		// 解决中文资源名称乱码问题:先以utf-8拆解,再以ISO-8859-1组装
		byte[] bytes = fileName.getBytes("utf-8");
		fileName = new String(bytes, "ISO-8859-1");
		return "success";
	}

}

struts.xml (important)

<?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>
	<!-- 手动指定default.properties中总上传文件大小的限制 -->
	<constant name="struts.multipart.maxSize" value="20971520"/>
	<package name="strutsFiles" namespace="/upAndDownload" extends="struts-default">
		<action name="uploadAction" class="action.UploadAction">
			<result name="success">/welcome.jsp</result>
			<result name="message">/message.jsp</result>
			<!-- 指定上传文件的扩展名 -->
			<interceptor-ref name="defaultStack">
				<param name="fileUpload.allowedExtensions">bmp,gif,jpg,jpeg,png,webp</param>
			</interceptor-ref>
		</action>
		<action name="downloadAction" class="action.DownloadAction">
			<result type="stream">
				<!-- 指定文件资源以存于Action中的fileName属性值命名  -->
				<param name="contentDisposition">attachment;filename=${fileName}</param>
				<!-- param标签的name属性值默认为inputName -->
				<!-- 当且仅当文件下载Action中的InputStream名为inputStream时此句可省略 -->
				<param name="inputName">is</param> <!-- is为文件下载Action中的InputStream名称 -->
			</result>
		</action>
	</package>
</struts>

WebRoot directory structure:
Insert picture description here
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Index</title>
	</head>
	<body>
		<!-- form的enctype="multipart/form-data"属性值是表单文件上传的必填选项 -->
		<form action="upAndDownload/uploadAction" method="post" enctype="multipart/form-data">
			请选择图片:<br /><br />
			<input type="file" name="images" /><br /><br />
			<input type="file" name="images" /><br /><br />
			<input type="file" name="images" /><br /><br />
			<input type="submit" value="上传" />
		</form><br /><br />
		<a href="upAndDownload/downloadAction">下载一张图片...</a>
	</body>
</html>

message.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Message</title>
	</head>
	<body>
		<h3 align="center">文件上传失败!</h3>
	</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Welcome</title>
	</head>
	<body>
		<h3 align="center">文件上传成功!</h3>
	</body>
</html>

Guess you like

Origin blog.csdn.net/qq_44965393/article/details/112189637