Struts2入门(七)——Struts2的文件上传和下载
一、前言
在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载。
注意:文件上传时,我们需要将表单提交方式设置为"POST"方式,并且将enctype属性设置为"multipart/form-data",该属性的默认值为"application/x-www-form-urlencoded",就是说,表单要写成以下这种形式:
<form action="" method="post" enctype="multipart/form-data"></form>
而且Struts2中并没有提供自己的文件上传解析器,默认使用的是Jakarta的Common-FileUpload的文件上传组件,所以我们还需要在添加两个包:
commons-io
commons-fileupload
至于版本根据自己需要选择(笔者在第一篇已经搭建好环境了。地址)
注意点如下:
1.1、文件上传的前提是表单属性method="post" enctype="multipart/form-data";
1.2、web应用中必须包含common-fileupload.jar和common-io.jar,因为struts2默认上传解析器使用的是jakarta;
1.3、可以在struts.xml中配置最大允许上传的文件大小:<constant name="struts.multipart.maxSize" value="....."/>,默认为2M;
二、文件上传案例
2.1、在Action中定义属性:
private File upload; //包含文件内容
private String uploadFileName; //上传文件的名称;
private String uploadContentType; //上传文件的MIME类型;
这些属性都会随着文件的上传自动赋值;
2.2、上传图片的例子
新建视图界面:Upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts2 FileUpload</title>
</head>
<body>
<form action="fileupload" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title"/><br/>
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
新建UploadAction继承ActionSupport
package com.Struts2.load; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; 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 String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} //返回上传文件的保存位置
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
} //接受依赖注入的方法
public void setSavePath(String savePath) {
this.savePath = savePath;
} public String execute() throws Exception{
System.out.println(getSavePath());
System.out.println(getUploadFileName());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName()); //以上传文件建立一个文件上传流
FileInputStream fis = new FileInputStream(getUpload()); //将上传文件的内容写入服务器
byte[] buffer = new byte[1024];
int leng = 0;
while((leng=fis.read(buffer))>0){
fos.write(buffer,0,leng);
buffer = new byte[1024];
}
return SUCCESS;
}
}
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="default" extends="struts-default">
<action name="fileupload" class="com.Struts2.load.UploadAction">
<!--使用拦截器过滤:1、配置默认拦截器,2、配置input的逻辑视图-->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/jpg,/image/gif,image/png</param>
</interceptor-ref>
<!-- 必须显示配置defaultStack拦截器的引用 -->
<interceptor-ref name="defaultStack"/>
<param name="savePath">/upload</param>
<result name="success">succ.jsp</result>
<!--必须配置input的逻辑视图 -->
<result name="input">Error.jsp</result>
</action>
<struts>
注意:web.xml中需要配置struts2的拦截器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>LearStruts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!--为Struts2定义一个过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
web.xml
succ.jsp视图代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传成功</title>
</head>
<body>
上传成功<br/>
文件标题:<s:property value="title"/><br/>
<s:property value="uploadFileName"/>
文件为:<img src="<s:property value="'/LearStruts2/upload/'+uploadFileName"/>"/><br/>
</body>
</html>
Error.jsp视图代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error 界面</title>
</head>
<body>
<s:fielderror/>
</body>
</html>
Error.jsp
代码效果如下:
注意:在struts.xml配置文件中,已经限制只能上传图片格式,如果上传别的文件的话,则会报错。
2.3、多个文件上传
新建Upliads.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传多个文件</title>
</head>
<body>
<form action="filesupload" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title"><br/>
选择第一个文件:<input type="file" name="uploads"><br/>
选择第二个文件:<input type="file" name="uploads"><br/>
选择第三个文件:<input type="file" name="uploads"><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
Upliads.jsp
新建UploadsAction类继承ActionSupport
package com.Struts2.load; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; //上传多个文件
public class UploadsAction extends ActionSupport {
private String title; //对应jsp的title
private File[] uploads; //对应jsp的uploads
private String[] uploadsContentType;
private String[] uploadsFileName;
//savePath:通过配置文件进行赋值"'\'upload",
//其中的'\'表示项目的根目录D:\\tomcat-8.0\\wtpwebapps\\LearStruts2\\upload
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUploads() {
return uploads;
}
public void setUploads(File[] upload) {
this.uploads = upload;
}
public String[] getUploadsContentType() {
return uploadsContentType;
}
public void setUploadsContentType(String[] uploadsContentType) {
this.uploadsContentType = uploadsContentType;
}
public String[] getUploadsFileName() {
return uploadsFileName;
}
public void setUploadsFileName(String[] uploadsFileName) {
this.uploadsFileName = uploadsFileName;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception{
File[] files = getUploads();
for(int i = 0;i<files.length;i++){
System.out.println(getSavePath());
System.out.println(getUploadsFileName()[i]);
//getSavePath : 获得根目录
//getUploadsFileName() : 获得文件名
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadsFileName()[i]); FileInputStream fis = new FileInputStream(files[i]); byte[] buffer = new byte[1024]; int len = 0; while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
buffer = new byte[1024];
}
}
return SUCCESS;
}
}
struts.xml中配置信息
<!--多个文件上传 -->
<action name="filesupload" class="com.Struts2.load.UploadsAction">
<!--该属性是依赖注入:通过配置文件给savePath赋值 ,是必须的 -->
<param name="savePath">/upload</param>
<result name="success">succ1.jsp</result>
</action>
succ1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>多个文件上传</title>
</head>
<body>
上传成功
</body>
</html>
succ1.jsp
代码效果如下:
2.3、Struts2的文件下载
视图下载界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts 2实现的文件下载</title>
</head>
<body>
<a href="down.action">图片下载</a>
</body>
</html>
fuledown.jsp
Action类
package com.Struts2.load; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownAction extends ActionSupport {
private String inputPath;
private String contentType;
private String filename; public String getContentType() {
return contentType;
} public void setContentType(String contentType) {
this.contentType = contentType;
} public String getFilename() {
return filename;
} public void setFilename(String filename) {
this.filename = filename;
} public String getInputPath() {
return inputPath;
} public void setInputPath(String inputPath) {
this.inputPath = inputPath;
} //下载用的Action应该返回一个InputStream实例
//该方法对应在result里的inputName属性值为targetFile
//第二步
public InputStream getTargetFile() throws Exception{
System.out.println("1");
InputStream in=ServletActionContext.getServletContext().getResourceAsStream(inputPath);
return in;
}
//第一步
public String execute(){
System.out.println("???");
inputPath="/upload/1.jpg";//要下载的文件名称
filename="1.jpg"; //保存文件时的名称
contentType="image/jpg";//保存文件的类型
return SUCCESS;
}
}
Struts.xml配置文件
<!-- 下载文件的Action -->
<action name="down" class="com.Struts2.load.DownAction">
<!-- 指定被下载资源的位置 -->
<param name="inputPath">${inputPath}</param> <!-- 配置结果类型为stream的结果 -->
<result name="success" type="stream">
<!--
contentType:指定下载文件的类型 ,和互联网MIME标准中的规定类型一致,
例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片
-->
<param name="contentType">${contentType}</param>
<!-- 指定下载文件的位置 -->
<!--
inputName:下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,
例如取值为inputStream的属性需要编写getInputStream()方法
-->
<param name="inputName">targetFile</param>
<!--
contentDisposition
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,
而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。
默认情况是代表inline,浏览器会尝试自动打开它
-->
<param name="contentDisposition">attachement;filename="${filename}"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">50000000</param>
</result>
</action>
代码都有笔者测试过,至于解析,笔者会在后期理解好之后再重新写。
Struts2入门(七)——Struts2的文件上传和下载的更多相关文章
- Struts2第六篇【文件上传和下载】
前言 在讲解开山篇的时候就已经说了,Struts2框架封装了文件上传的功能--..本博文主要讲解怎么使用Struts框架来完成文件上传和下载 回顾以前的文件上传 首先,我们先来回顾一下以前,我们在we ...
- struts2一个和多个文件上传及下载
struts2的文件上传相比我们自己利用第三方jar包(commons-fileupload-1.2.1.jar commons-io-1.3.2.jar )要简单的多,当然struts2里面也是 ...
- Struts2学习(六)———— 文件上传和下载
一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相应的处理,而在struts2中就不需要了,因为有一个fileUpload拦截器帮 ...
- struts2文件上传和下载
1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...
- Struts2 之 实现文件上传和下载
Struts2 之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...
- 十六、Struts2文件上传与下载
文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...
- 【SSH2(实用文章)】--Struts2文件上传和下载的例子
回想一下,再上一篇文章Struts2实现机制,该步骤做一步一步来解决,这种决心不仅要理清再次Struts2用法.映射机制及其在深入分析.最后一个例子来介绍Struts2一种用法,这里将做一个有关文件上 ...
- Struts2学习总结——文件上传与下载
Struts2文件上传与下载 1.1.1新建一个Maven项目(demo02) 在此添加Web构面以及 struts2 构面 1.2.1配置Maven依赖(pom.xml 文件) <?xml v ...
- struts2学习(14)struts2文件上传和下载(4)多个文件上传和下载
四.多个文件上传: 五.struts2文件下载: 多个文件上传action com.cy.action.FilesUploadAction.java: package com.cy.action; i ...
- 七牛云存储 qiniu 域名 回收 文件上传 备份 下载 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
随机推荐
- 常用 meta 整理
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 --> <meta name="HandheldFriendly" con ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- 深入理解 Android 之 View 的绘制流程
概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...
- 从零自学Hadoop(22):HBase协处理器
阅读目录 序 介绍 Observer操作 示例下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Sour ...
- LVM基本介绍与常用命令
一.LVM介绍LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制LVM - 优点:LVM通常用于装备大量磁盘的系统,但它同样适 ...
- SpringMVC初步
SpringMVC框架介绍 1) Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...
- 在Linux上运行ASP.NET vNext
最新的ASP.NET vNext完全开源且可以跨多个平台运行,在Windows环境下我尝试了下,几乎没花什么工夫就跑起了Sample,而在Linux环境下则要多花了不少时间,所以特别记录下整个过程,希 ...
- .NET基础拾遗(7)Web Service的开发与应用基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...
- 详解前端模块化工具-webpack
webpack是一个module bundler,抛开博大精深的汉字问题,我们暂且管他叫'模块管理工具'.随着js能做的事情越来越多,浏览器.服务器,js似乎无处不在,这时,使日渐增多的js代码变得合 ...
- 让Unity NavMesh为我所用
Unity里面整合了一个NavMesh功能,虽然让人又爱又恨. 但当你在其他地方需要这个NavMesh的数据时,就更让人欲罢不能了. 比如说服务器需要Unity的NavMesh数据时. 比如说你想将U ...