zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Struts 框架 之 文件上传下载案例

案例文件框架 struts 上传下载
2023-09-14 08:58:21 时间

导jar包:

  jar包的具体作用在前面的文章有讲。

配置 web.xml

复制代码
 !-- Struts核心拦截器 -- 

 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 
复制代码

然后开始开发action 

上传action类:FileUpload src/com.yif.fileupload

jsp :  input type="file" name="file1"

action类中使用  File file1 和 jsp中名字要相同

file1FileName  file1ContentType 中的file1 需要与jsp中对应

复制代码
import java.io.File;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport {

 // 对应表单: input type="file" name="file1" 

 private File file1;

 // 文件名

 private String file1FileName;

 // 文件的类型(MIME)

 private String file1ContentType;

 public void setFile1(File file1) {

 this.file1 = file1;

 public void setFile1FileName(String file1FileName) {

 this.file1FileName = file1FileName;

 public void setFile1ContentType(String file1ContentType) {

 this.file1ContentType = file1ContentType;

 @Override

 public String execute() throws Exception {

 /****** 拿到上传的文件,进行处理 ******/

 // 把文件上传到upload目录

 // 获取上传的目录路径

 String path = ServletActionContext.getServletContext().getRealPath(

 "/upload");

 // 创建目标文件对象

 File destFile = new File(path, file1FileName);

 // 把上传的文件,拷贝到目标文件中

 FileUtils.copyFile(file1, destFile);

 return SUCCESS;

}
复制代码

 

开发下载文件 action类: DownAction  src/com.yif.fileupload

list方法 将上传文件的路径下的文件 放在list中写道请求中。

复制代码
package com.yif.fileupload;

import java.io.File;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownAction extends ActionSupport {

 /*************1. 显示所有要下载文件的列表*********************/

 public String list() throws Exception {

 //得到upload目录路径

 String path = ServletActionContext.getServletContext().getRealPath("/upload");

 // 目录对象

 File file = new File(path);

 // 得到所有要下载的文件的文件名

 String[] fileNames = file.list();

 // 保存

 ActionContext ac = ActionContext.getContext();

 // 得到代表request的map (第二种方式)

 Map String,Object request= (Map String, Object ) ac.get("request");

 request.put("fileNames", fileNames);

 return "list";

 /*************2. 文件下载*********************/

 private String fileName;

 public void setFileName(String fileName) {

 // 处理传入的参数中问题(get提交)

 try {

 fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");

 } catch (UnsupportedEncodingException e) {

 throw new RuntimeException(e);

 // 把处理好的文件名,赋值

 this.fileName = fileName;

 //2. 下载提交的业务方法 (在struts.xml中配置返回stream)

 public String down() throws Exception {

 return "download";

 // 3. 返回文件流的方法

 public InputStream getAttrInputStream(){

 return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);

 // 4. 下载显示的文件名(浏览器显示的文件名)

 public String getDownFileName() {

 // 需要进行中文编码

 try {

 fileName = URLEncoder.encode(fileName, "UTF-8");

 } catch (UnsupportedEncodingException e) {

 throw new RuntimeException(e);

 return fileName;


 result name="download" type="stream" 

 !-- 运行下载的文件的类型:指定为所有的二进制文件类型 -- 

 param name="contentType" application/octet-stream /param 

 !-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -- 

 param name="inputName" attrInputStream /param 

 !-- 下载头,包括:浏览器显示的文件名 -- 

 param name="contentDisposition" attachment;filename=${downFileName} /param 

 !-- 缓冲区大小设置 -- 

 param name="bufferSize" 1024 /param 

 /result 

 

复制代码
 ?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="upload_" extends="struts-default" 

 !-- 注意: action 的名称不能用关键字"fileUpload" -- 

 action name="fileUploadAction" 

 !-- 限制运行上传的文件的类型 -- 

 interceptor-ref name="defaultStack" 

 !-- 限制运行的文件的扩展名 -- 

 param name="fileUpload.allowedExtensions" txt,jpg,jar /param 

 !-- 限制运行的类型 【与上面同时使用,取交集】 param name="fileUpload.allowedTypes" text/plain /param -- 

 /interceptor-ref 

 result name="success" /e/success.jsp /result 

 !-- 配置错误视图 -- 

 result name="input" /e/error.jsp /result 

 /action 

 action name="down_*" method="{1}" 

 !-- 列表展示 -- 

 result name="list" /e/list.jsp /result 

 !-- 下载操作 -- 

 result name="download" type="stream" 

 !-- 运行下载的文件的类型:指定为所有的二进制文件类型 -- 

 param name="contentType" application/octet-stream /param 

 !-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -- 

 param name="inputName" attrInputStream /param 

 !-- 下载头,包括:浏览器显示的文件名 -- 

 param name="contentDisposition" attachment;filename=${downFileName} /param 

 !-- 缓冲区大小设置 -- 

 param name="bufferSize" 1024 /param 

 /result 

 /action 

 /package 

 /struts 
复制代码

 

配置struts.xml src/struts.xml

复制代码
 ?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 

 include file="com/yif/fileupload/upload.xml" /include 

 /struts 
复制代码

 

upload.jsp页面(前端采用Bootstrap框架)   WebRoot/e/upload.jsp

复制代码
 body 

 div 

 form action="${pageContext.request.contextPath }/fileUploadAction"

 method="post" enctype="multipart/form-data" 

 h1 文件上传 /h1 

 div 

 label for="name" 用户名: /label 

 div 

 input type="text" id="name" placeholder="用户名"

 name="userName" 

 /div 

 /div 

 div 

 label 文件: /label 

 div 

 input type="file" name="file1" 

 /div 

 /div 

 div 

 div 

 input type="submit" value="上传" /input 

 /div 

 /div 

 /form 

 /div 

 /body 
复制代码
复制代码
 body 

 h1 下载列表 /h1 

 table 

 td 编号 /td 

 td 文件名 /td 

 td 操作 /td 

 /tr 

 %@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"% 

 c:forEach var="fileName" items="${fileNames}" varStatus="vs" 

 td ${vs.count } /td 

 td ${fileName } /td 

 !-- 构建一个url -- c:url var="url" value="down_down" 

 c:param name="fileName" value="${fileName}" /c:param 

 /c:url a href="${url }" 下载 /a /td 

 /tr 

 /c:forEach 

 /table 

 /body 
复制代码
WEB核心【Servlet配置和注解重构用户登录】第八章 本篇章主要讲Servlet,两个重点:Servlet路径配置,Servlet注解开发,了解内容Servlet继承数及生命周期,Servlet路径配置,ServletConfig配置
第四章:Spring项目文件上传两种方式(全解析) 如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系。本系列教程希望您能站在上帝的角度去观察(了解)Java体系。使Java的各种后端技术在你心中模块化;让你在工作中能将Java各个技术了然于心;能够即插即用。
搭建一个struts2的框架,在之前已经搭建过struts的框架了,这里的流程基本上差不多,详见 struts1的搭建 首先到官网上下载jar包,这里附一个git的链接struts2jar包下载 新建工程,将下载的jar解压至工程中,项目结构如下: