struts2文件上传

Struts2框架下的文件上传与下载实现指南 Struts2是Apache组织下的一个开源的Web应用框架,其基于MVC设计模式,为Java Web应用提供了一个强大的开发基础。其特点在于将业务逻辑与展示层进行有效分离,极大地方便了开发人员对应用程序的维护和扩展。由于其良好的模块化和可配置性,Struts2在企业级应用中尤为流行,尤其适合于那些需要快速开发而又具有复杂业务逻辑的应用系统。Struts2框架主要分为几个核心组件:Model组件:用于处理业务逻辑和数据持久化。 阅读详情

 

      struts2没有提供自己的请求解析器,也就是说,struts2不会自己区处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传

 Struts2的struts.properties配置文件中,配置struts2的上传文件解析器

struts.multipart.parser=jakarta  (srtuts2默认),也可以设置为常用的cos,pell等

 

配置上传页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>
简单的文件上传</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
      
文件标题:<input type="text" name="title" /><br>
      
选择文件:<input type="file" name="upload" /><br>
    
<input value="上传" type="submit" />
</form>
</body>
</html>

 

配置上传action

 

package lee;

import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;

import com.opensymphony.xwork2.ActionSupport;



public class UploadAction extends ActionSupport
...{
    
private String title;
    
private File upload;
    
private String uploadContentType;
    
private String uploadFileName;

    
//
接受依赖注入的属性
    private String savePath;
    
//接受依赖注入的方法
    public void setSavePath(String value)
    
...{
        
this.savePath = value;
    }

    
private String getSavePath() throws Exception 
    
...{
        
return ServletActionContext.getRequest().getRealPath(savePath);
    }
    
    
public void setTitle(String title) ...{
        
this.title = title; 
    }

    
public void setUpload(File upload) ...{
        
this.upload = upload; 
    }

    
public void setUploadContentType(String uploadContentType) ...{
        
this.uploadContentType = uploadContentType; 
    }

    
public void setUploadFileName(String uploadFileName) ...{
        
this.uploadFileName = uploadFileName; 
    }

    
public String getTitle() ...{
        
return (this.title); 
    }

    
public File getUpload() ...{
        
return (this.upload); 
    }

    
public String getUploadContentType() ...{
        
return (this.uploadContentType); 
    }

    
public String getUploadFileName() ...{
        
return (this.uploadFileName); 
    }
    @Override
    
public String execute() throws Exception
    
...{
        System.out.println("
开始上传单个文件-----------------------");
        System.out.println(getSavePath());
        System.out.println("==========" + getUploadFileName());
        System.out.println("==========" + getUploadContentType());
        System.out.println("==========" + getUpload());
        
//以服务器的文件保存地址和原文件名建立上传文件输出流
        FileOutputStream fos = new FileOutputStream(getSavePath() + "/" + getUploadFileName());
        FileInputStream fis = 
new FileInputStream(getUpload());
        
byte[] buffer = new byte[1024];
        
int len = 0;
        
while ((len = fis.read(buffer)) > 0)
        
...{
            fos.write(buffer , 0 , len);
        }
        
return SUCCESS;
    }
}

 

struts配置文件

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
    
<struts>

    
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>

    
<package name="lee" extends="struts-default">
    
        
<action name="upload" class="lee.UploadAction">
            
<param name="savePath">/upload</param>
            
<result>/succ.jsp</result>    
        
</action>        
    
</package>
</struts>    

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns
="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    
<filter>
      
<filter-name>struts2</filter-name>
      
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>
    
<filter-mapping>
      
<filter-name>struts2</filter-name>
      
<url-pattern>/*</url-pattern>
    
</filter-mapping>
 
 
    
<filter>
      
<filter-name>struts-cleanup</filter-name>
      
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    
</filter>
    
<filter-mapping>
      
<filter-name>struts-cleanup</filter-name>
      
<url-pattern>/*</url-pattern>
    
</filter-mapping>
</web-app>

 

这里web.xml多配置了一个ActionContextCleanUp的配置,这个类是一个filter,他的作用是方便strut2与sitemesh整合,与文件上传本没有关系,但不加载这个filter,可能在上传出出现莫名的异常,加入后就稳定了,可能是strut2的bug吧

文件上传工程页面:

 

<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%
...@taglib prefix="s" uri="/struts-tags"%>
<html>
    
<head>
        
<title>
上传成功</title>
    
</head>
    
<body>
        
上传成功!<br>
        
文件标题:<s:property value=" + title"/><br>
        
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
    
</body>
</html>

 

运行,选择一个jpg图片,提交,就可以看到上传的页面显示出来了

通常,我们希望限定上传文件的类型,比如说只能上传图片,还需要进一步改造

首先改造Action,增加一个属性和一个方法,属性表示允许上传的文件类型,方法进行验证,是否允许上传

 

package lee;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import org.apache.struts2.ServletActionContext;
import java.io.*;

import com.opensymphony.xwork2.ActionSupport;

/** *//**
 * 
@author  yeeku.H.lee kongyeeku@163.com
 * 
@version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */


public class UploadAction extends ActionSupport
...{
    
private String title;
    
private File upload;
    
private String uploadContentType;
    
private String uploadFileName;
    
private String allowTypes;

    
//
接受依赖注入的属性
    private String savePath;
    
//接受依赖注入的方法
    public void setSavePath(String value)
    
...{
        
this.savePath = value;
    }

    
private String getSavePath() throws Exception 
    
...{
        
return ServletActionContext.getRequest().getRealPath(savePath);
    }
    
    
public void setTitle(String title) ...{
        
this.title = title; 
    }

    
public void setUpload(File upload) ...{
        
this.upload = upload; 
    }

    
public void setUploadContentType(String uploadContentType) ...{
        
this.uploadContentType = uploadContentType; 
    }

    
public void setUploadFileName(String uploadFileName) ...{
        
this.uploadFileName = uploadFileName; 
    }

    
public String getTitle() ...{
        
return (this.title); 
    }

    
public File getUpload() ...{
        
return (this.upload); 
    }

    
public String getUploadContentType() ...{
        
return (this.uploadContentType); 
    }

    
public String getUploadFileName() ...{
        
return (this.uploadFileName); 
    }
    @Override
    
public String execute() throws Exception
    
...{
        System.out.println("
开始上传单个文件-----------------------");
        System.out.println(getSavePath());
        System.out.println("==========" + getUploadFileName());
        System.out.println("==========" + getUploadContentType());
        System.out.println("==========" + getUpload());
        
        
//判断是否允许上传
        String filterResult=filterType(this.getAllowTypes().split(","));
        
if(filterResult!=null)...{
            ActionContext.getContext().put("typeError", "
您要上传的文件类型不正确");
            
return filterResult;
        }
        
//以服务器的文件保存地址和原文件名建立上传文件输出流
        FileOutputStream fos = new FileOutputStream(getSavePath() + "/" + getUploadFileName());
        FileInputStream fis = 
new FileInputStream(getUpload());
        
byte[] buffer = new byte[1024];
        
int len = 0;
        
while ((len = fis.read(buffer)) > 0)
        
...{
            fos.write(buffer , 0 , len);
        }
        
return SUCCESS;
    }
    
    
public String filterType(String[] types)...{
        String fileType=
this.getUploadContentType();
        
for(String type:types)...{
            
if(type.equals(fileType))...{
                
return null;
            }
        }
        
return INPUT;
    }

    
public String getAllowTypes() ...{
        
return allowTypes;
    }

    
public void setAllowTypes(String allowTypes) ...{
        
this.allowTypes = allowTypes;
    }
}

 

修改struts.xml定义文件类型及错误返回页面:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
    
<struts>

    
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>

    
<package name="lee" extends="struts-default">
    
        
<action name="upload" class="lee.UploadAction">
            
<param name="allowTypes">image/bmp,image/jpg,image/png,image/gif,image/jpeg</param>
            
<param name="savePath">/upload</param>
            
<result>/succ.jsp</result>    
            
<result name="input">/upload.jsp</result>    
        
</action>        
    
</package>
</struts>    

 

修改上传页面:upload.jsp

 

<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%
...@taglib prefix="s" uri="/struts-tags"%>
<%
...@ page isELIgnored="false" %>
<%
...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
${requestScope.typeError}
<form action="upload.action" method="post" enctype="multipart/form-data">
      
<input type="text" name="title" /><br>
      
<input type="file" name="upload" /><br>
    
<input value="upload" type="submit" />
</form>
</body>
</html>

 
【点云可视化】自制ply文件并使用open3d可视化点云 使用open3d可视化点云,需要将点云制作为ply文件传入函数中。 安装open3d 直接使用pip安装 pip install open3d ply文件 ply文件的结构为:文件头+数据内容,文件头从 ply 一行开始,到 end_header 为止,里面定义了此 ply文件中数据的组织形式,包括数据类型和个数。后面是点云的坐标信息及每个点的颜色属性的,参考自 如下为具有5个点的点云,end_header后每一行表示一个点的x,y,z,R,G,B信息 ply format ascii 1.0 comme 阅读详情

相关推荐

MindIE与vLLM-Ascend性能对决:Qwen3在昇腾910B上的推理效率实测

本文深度对比了昇腾910B平台上两大推理框架MindIE与vLLM-Ascend的性能表现。通过实测Qwen3系列模型,详细分析了二者在单卡、多卡场景下的推理效率、扩展性及生产环境适用性,为AI算力基础设施选型提供关键参考。

weixin_29214335的博客 1183

Struts 文件上传 java后台接收

一般的都是form里面加enctype="multipart/form-data这个参数,这就会导致后台只能接收二进制流,不能接收其他的参数了。。所以才用这个办法。 首先你得有个页面 upload.jsp 文件:<input type="file" name="uploadFile" id="myFile"/> <input type="button" value="上...

Jon_jing的博客 954

华为昇腾Atlas 200I DK A2实战:从开箱到运行首个AI应用

本文详细介绍了华为昇腾Atlas 200I DK A2开发板从开箱到运行首个AI应用的全过程。包括硬件准备、系统烧录、开发板连接方案、AI应用运行及性能优化技巧,帮助开发者快速上手部署AI项目,提升开发效率。

weixin_27038245的博客 590

Struts2文件上传(2)_细节

一.Struts2文件上传实现细节简介     Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来。但Struts2在原有的上传解析器基础上做了进一步封装,更进一步简化了文件上传。     Struts2默认使用的是Jakarta的Common-FileUpload

yanwushu的博客 4463

手把手教你用LangChain4j打造一个“会追问”的AI客服:以航空货运下单为例

本文详细介绍了如何使用LangChain4j构建一个能够主动追问的航空货运AI客服系统。通过对话状态管理、信息缺口识别和渐进式引导等核心技术,实现高效的多轮对话交互。文章包含从交互设计到代码实现的完整指南,特别强调了提示词(Prompt)在优化AI行为中的关键作用,帮助开发者打造更智能的客服解决方案。

weixin_28731223的博客 154

用HFSS仿真平面线圈的电感量

平面线圈是指在平面上绕制而成的线圈,如PCB上的电感线圈、无线供电使用的金属丝绕制而成的线圈等。根据线圈的不同形状可将平面线圈分为方形线圈,六角形线圈、八角形线圈、螺旋原型线圈等。

johnboat的专栏 1万+

Struts2的文件上传

文件上传的概述:文件上传是将本地文件上传到服务器的指定目录下。要使用Struts2进行文件上传,首先将form表单的enctype属性值设置为multipart/form-data。文件上传还需要

Keep Doing this的博客 1480

解决VM 与 Device/Credential Guard 不兼容。在禁用 Device/Credential Guard 后,可以运行 VM 的方法

在启用了Credential Guard或Device Guard的Windows 10主机上启动12.5版之前的VMware Workstation中的虚拟机时,将显示蓝色诊断屏幕(BSOD)。 会看到类似于以下内容的错误: VMware Workstation和Device / Credential Guard不兼容。禁用Device / Credential Guard后,可以运行VMwar...

Hern(宋兆恒) 39万+

开发者导航:Struts2学习笔记之:文件上传

Struts也提供了内置标签用于文件上传,我们称为文件选择域,这与html的标签是一样用的。

开发者导航 1190

机器人控制算法九之 位姿描述与空间变换

机器人控制算法九之 位姿描述与空间变换

yohnyang的博客 1031

SD卡烧写Linux kernel——SD卡分区,并烧写uboot,kernel,DTB及filesystem

Linux Ubuntu环境下对SD卡分区(烧写uboot,kernel,及filesystem) 介绍dd命令的使用,SD卡分区、格式化、复制文件等 fdisk命令

weixin_42408707的博客 5056

Struts2文件上传(全)

一、单文件上传 上传表单页面:select.jsp <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html> <head&g...

荒的博客 725

YOLOv8改进---BiFPN特征融合

yolov8结合BiFPN进行改进

qq_41920323的博客 4523

struts2 文件上传

在开发基于Web的应用的时候,经常会碰到需要实现文件上传、下载的功能,比如编辑某个商品,需要给它上传一个图片等等。  不使用Struts2的话,可以有很多种方式来实现文件上传,比如使用Apache的Common-FileUpload等。但是这些传统的方式,实现起来非常麻烦,需要写很多代码来进行控制。  现在使用Struts2来实现文件上传的功能,会更加简单和方便,事实上,Struts2

学习笔记 1990

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

浅谈Struts2文件上传

  目录 一、struts2文件上传大前提 二、Action实现 三、struts.xml配置【包含拦截器实现文件格式与文件大小的过滤】 常用struts2 配置上传文件格式  一、struts2文件上传大前提 必须将&lt;form&gt;表单中的method设置为”post” 必须将&lt;form&gt;表单中的enctype设置为”multipart/form-data” ...

nailnehc的博客 1037

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

struts2的文件上传

如何在Struts 2文件上传作品? Struts 2中使用的文件上传拦截器添加在Struts应用程序上传文件的支持服务。 基于Struts 2的文件上传拦截MultiPartRequestWrapper,这是自动应用的要求,如果它包含文件中的元素。然后,它增加了以下参数要求(假设上传的文件名 是文件MyFile)。 MyFile :文件-实际的文件MyFileContentType

ba5189tsl的专栏 894

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Struts2文件上传(基于表单的文件上传)

•Commons-FileUpload组件 –Commons是Apache开放源代码组织的一个Java子项目,其中的FileUpload是用来处理HTTP文件上传的子项目 •Commons-FileUpload组件特点 –使用简单:可以方便地嵌入到JSP文件中,编写少量代码即可完成文件的上传功能 –能够全程控制上传内容 –能够对上传文件的大小、类型进行控制 •需要下载Common-FileUpla...

qq_42574855的博客 168

Struts2文件上传

1、Tomcat报错处理: 2、文件上传jar包引入: 3、form表单配置: 4、Action添加属性: 5、其它: 6、源代码示例: 7、配置文件: 8、页面源码: 9、运行结果: 10、文件类型和后缀名:

Remoa的休闲茶馆 2347

Struts2 文件上传

 之前我们可能使用过文件上传组件进行开发系统中文件上传的功能,但在Struts2 中为我们提供了更为简单易用的上传功能,当然虽然易用,但其实际不是对底层文件IO和HTTP的封装。下面我们介绍一下,如何在Struts2 中使用文件上传的功能。我们知识Strust2中大部分的功能都是通过拦截器实现的,当然,这里的文件上传也不例外。同样也是也采用拦截器来支持的。 下面以例子来看一下如何使

lilp_ndsc的专栏 3087

Struts2之文件上传

由于Struts2已经对文件上传进行了封装,所以较为简单,导入Struts2相应的java包,commons-fileupload.jar、commons-io.jar是其依赖的jar包。 使用Struts2的文件上传主要有一下步骤: (1)定义文件上传的表单,包含文件上传表单项。 (2)在form上将enctype属性设置为"multipart/form-

麒麟木木 2309

struts2文件上传和下载

1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截。 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截,对拦截到的方法可以进行扩展,增强 Struts2中的拦截器主要针对Action中方法的调用进行拦截 <interceptors> <!—声明n个拦截器--> <interceptor name="fileUpload" class="org.

光何 198
上一篇: Struts2 Spring2 整合
下一篇: FileUpload组件实现文件上传
jamesjun
博客等级 码龄19年 25粉丝 33原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值