页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <h2>request</h2> <table border="1"> <c:forEach items="${requestScope.request_list}" var="user"> <tr> <td>${user}</td> </tr> </c:forEach> </table> <h2>session</h2> <table border="1"> <c:forEach items="${sessionScope.session_list}" var="user"> <tr> <td>${user}</td> </tr> </c:forEach> </table> <h2>context</h2> <table border="1"> <c:forEach items="${applicationScope.context_list}" var="user"> <tr> <td>${user}</td> </tr> </c:forEach> </table> </body> </html>struts2.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="upload" extends="struts-default" namespace="/"> <action name="upload" class="com.bin.action.UploadAction" method="upload"> <param name="savePath">e:/images/</param> <result name="success">/index.jsp</result> </action> </package> </struts>action:
package com.bin.action; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class UploadAction extends ActionSupport { //接收上传文件,名字来源于页面的File的name属性 public File attach; //接收文件的类型 public String attachContentType; //接收文件的名称 public String attachFileName; //接收描述 public String info; public String savePath; public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public File getAttach() { return attach; } public void setAttach(File attach) { this.attach = attach; } public String getAttachContentType() { return attachContentType; } public void setAttachContentType(String attachContentType) { this.attachContentType = attachContentType; } public String getAttachFileName() { return attachFileName; } public void setAttachFileName(String attachFileName) { this.attachFileName = attachFileName; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String upload() throws IOException { System.out.println(attach); System.out.println(attachContentType); System.out.println(attachFileName); System.out.println(info); FileUtils.copyFile(attach,new File(savePath+attachFileName)); return SUCCESS; } }视图类型一定是stream类型 页面:
<%-- Created by IntelliJ IDEA. User: bin Date: 2020-07-02 Time: 9:21 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <table border="1" width="300xp"> <tr> <td>编号</td> <td>文件名称</td> <td>操作</td> </tr> <c:forEach items="${requestScope.list}" var="list" varStatus="downStu"> <tr> <td>${downStu.count}</td> <td>${list}</td> <td><a href="${pageContext.request.contextPath}/down?name=${list}">下载</a></td> </tr> </c:forEach> </table> </body> </html>struts。xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="down" extends="struts-default" namespace="/"> <action name="downlist" class="com.bin.action.DownLoad" method="downLoad"> <param name="downPath">e:/images/</param> <result name="list">downlist.jsp</result> </action> <action name="down" class="com.bin.action.DownLoad" method="down"> <param name="downPath">e:/images/</param> <result name="down" type="stream"> <!-- 往StreamResult类中的属性注入内容 --> <!-- 返回给浏览器的文件类型。返回通用的二进制 --> <param name="contentType">application/octet-stream</param> <!-- 返回给浏览器的输入流 --> <param name="inputName">inputStream</param> <!-- 告诉浏览器的方式下载资源 ${name}: 获取Action中的getName()方法的数据 --> <param name="contentDisposition">attachment;filename=${name}</param> <!-- 缓存大小 --> <param name="bufferSize">1024</param> </result> </action> </package> </struts>action:
package com.bin.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; public class DownLoad extends ActionSupport { private String downPath; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDownPath() { return downPath; } public void setDownPath(String downPath) { this.downPath = downPath; } public String downLoad(){ File file =new File(downPath); String[] list = file.list(); ActionContext requestMap = ActionContext.getContext(); requestMap.put("list",list); return "list"; } //给struts提供写出数据的输入流 public InputStream getInputStream(){ try { FileInputStream inputStream = new FileInputStream(new File(downPath + name)); return inputStream; } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } } public String down(){ return "down"; } }这是我自己写的一个小案例,如果又什么地方看不懂的话,欢迎随时提问,也可以加我的微信13283840906,只限小白,大神勿扰。