struts2——文件下载(简单的功能)

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>struts2的一个例子</title> 9 </head> 10 <body> 11 <s:form action="downloadFile.action" method="post" enctype="multipart/form-data"> 12 <s:submit value="下载文件"/> 13 </s:form> 14 15 </body> 16 </html>

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"/> 7 <package name="hello" extends="struts-default" namespace="/"> 8 <action name="downloadFile" class="com.xiaostudy.web.DownloadFile" method="downloadFile"> 9 <result name="success" type="stream"> 10 <param name="inputName">fileInputStream</param> 11 <param name="contentDisposition">attachment;filename=image.jpg</param> 12 <param name="contentType">application/octet-stream</param> 13 </result> 14 </action> 15 </package> 16 </struts>

1 package com.xiaostudy.web; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.InputStream; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 12 public class DownloadFile extends ActionSupport { 13 14 public InputStream fileInputStream; 15 public String downloadFile() throws FileNotFoundException { 16 17 String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files/CSS.jpg"); 18 fileInputStream = new FileInputStream(new File(path)); 19 20 return SUCCESS; 21 } 22 public InputStream getFileInputStream() { 23 return fileInputStream; 24 } 25 public void setFileInputStream(InputStream fileInputStream) { 26 this.fileInputStream = fileInputStream; 27 } 28 29 30 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)