七 Struts2 文件上传和下载
配置文件
<?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="my" namespace="/" extends="struts-default">
<!-- 文件上传 -->
<action name="upload" class="com.action.UploadAction">
<result name="success">/jsp/fileupload.jsp</result>
</action>
<!-- 文件下载 -->
<action name="download" class="com.action.DownAction">
<result name="success" type="stream">
<!-- inputNamec参数对应action中的getFileDownload方法,参数的值就是此方法去掉get前缀、首字母小写的字符串 -->
<param name="inputName">fileDownload</param>
<!-- 下载缓冲区的大小 -->
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
文件上传:
fileupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'fileupload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input name="miaoshu"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
UploadAction.java
package com.action;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
String miaoshu;
File file;
String fileFileName;//获取文件名,该属性名的组成为file+FileName 后面是固定写法
String fileContentType;//获取文件类型
public String getMiaoshu() {
return miaoshu;
}
public void setMiaoshu(String miaoshu) {
this.miaoshu = miaoshu;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(miaoshu);
//获取文件存储路径
String path = ServletActionContext.getServletContext().getRealPath("/upload");
//输出流
OutputStream os = new FileOutputStream(new File(path,fileFileName));
//输入流
InputStream is = new FileInputStream(file);
byte[] buf = new byte[1024];
int length = 0 ;
while(-1 != (length = is.read(buf) ) )
{
os.write(buf, 0, length) ;
}
is.close();
os.flush();
os.close();
return SUCCESS;
}
}
文件下载:
filedownload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'filedownload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="download" method="post">
输入文件名:<input name="filename"><br/>
<input type="submit" value="下载">
</form>
<p><a href="javascript:window.location='download?filename='+encodeURI('李四.png')">李四下载</a></p>
</body>
</html>
DownAction.java
package com.action; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownAction extends ActionSupport{
String filename; public String getFilename() {
return filename;
} public void setFilename(String filename) {
this.filename = filename;
} public InputStream getFileDownload(){
try {
//设置下载的头部信息,包括文件名等等
ServletActionContext.getResponse().setHeader("Content-Disposition",
"attachment;fileName=" + URLEncoder.encode(filename, "utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ServletActionContext.getServletContext().getResourceAsStream("upload/"+filename);
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
附:文件批量上传
filesupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'filesupload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<script type="text/javascript">
function add(){
var input = document.createElement("input");
input.setAttribute("type","file");
input.setAttribute("name","file"); document.getElementById("files").appendChild(input);
}
</script>
<body>
<form action="uploads" method="post" enctype="multipart/form-data">
<div id="files">
<input type="file" name="file" />
</div>
<input type="button" value="添加" onclick="add()">
<input type="submit" value="提交">
</form>
</body>
</html>
UploadsAction.java
package com.upload.action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadsAction extends ActionSupport{
List<File> file;
List<String> fileFileName;
List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
//获取文件存储路径
String path = ServletActionContext.getServletContext().getRealPath("/upload");
for(int i=0;i<file.size();i++){
//输出流
OutputStream os = new FileOutputStream(new File(path,fileFileName.get(i)));
//输入流
InputStream is = new FileInputStream(file.get(i));
byte[] buf = new byte[1024];
int length = 0 ;
while(-1 != (length = is.read(buf) ) )
{
os.write(buf, 0, length) ;
}
is.close();
os.flush();
os.close();
} addActionMessage("成功");
return SUCCESS;
}
}
七 Struts2 文件上传和下载的更多相关文章
- Struts2文件上传和下载(原理)
转自:http://zhou568xiao.iteye.com/blog/220732 1. 文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1) ...
- 十六、Struts2文件上传与下载
文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...
- 【SSH2(实用文章)】--Struts2文件上传和下载的例子
回想一下,再上一篇文章Struts2实现机制,该步骤做一步一步来解决,这种决心不仅要理清再次Struts2用法.映射机制及其在深入分析.最后一个例子来介绍Struts2一种用法,这里将做一个有关文件上 ...
- 学习Struts--Chap07:Struts2文件上传和下载
1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...
- struts2 文件上传和下载,以及部分源代码解析
struts2 文件上传 和部分源代码解析,以及一般上传原理 (1) 单文件上传 一.简单介绍 Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form ...
- (八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...
- struts2学习(13)struts2文件上传和下载(1)
一.Struts2文件上传: 二.配置文件的大小以及允许上传的文件类型: 三.大文件上传: 如果不配置上传文件的大小,struts2默认允许上传文件最大为2M: 2097152Byte: 例子实现 ...
- Struts2文件上传与下载
一,页面 index.html 在页面中最重要的就是这个文件上传用的 form 表单,注意这里一定要把 form 的encyType属性明确标定为“multipart/form-data”,只有这样. ...
- struts2文件上传和下载
1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...
随机推荐
- js导出excel表格并生成多sheet
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【TensorFlow篇】--Tensorflow框架初始,实现机器学习中多元线性回归
一.前述 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,T ...
- Python--开发简单爬虫
简单爬虫架构 动态运行流程 URL管理器的作用 URL管理器的3种实现方式 网页下载器的作用 Python网页下载器的种类 urllib2下载网页的3种方法 网页解析器的作用 Python的几种网页解 ...
- 强化学习(八)价值函数的近似表示与Deep Q-Learning
在强化学习系列的前七篇里,我们主要讨论的都是规模比较小的强化学习问题求解算法.今天开始我们步入深度强化学习.这一篇关注于价值函数的近似表示和Deep Q-Learning算法. Deep Q-Lear ...
- Flink从入门到放弃(入门篇4) DataStreamAPI
戳更多文章: 1-Flink入门 2-本地环境搭建&构建第一个Flink应用 3-DataSet API 4-DataSteam API 5-集群部署 6-分布式缓存 7-重启策略 8-Fli ...
- SLAM+语音机器人DIY系列:(四)差分底盘设计——2.stm32主控软件设计
摘要 运动底盘是移动机器人的重要组成部分,不像激光雷达.IMU.麦克风.音响.摄像头这些通用部件可以直接买到,很难买到通用的底盘.一方面是因为底盘的尺寸结构和参数是要与具体机器人匹配的:另一方面是因为 ...
- C#工具:Ado.Net SqlServer数据库 MySql数据库
数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库. SqlServer调用数据库 using System; using System.Coll ...
- iis正确安装了,但是还是无法访问,这是iis和.net安装顺序问题,记录一下
正确顺序:先安装iis,后安装net 如果没有按照正常顺序进行安装的,可能就无法访问了,这就需要手动 注册asp.net 4.0 到iis ,可以使用此命令重新注册一下: 32位的Windows: 1 ...
- Java学习点滴——Class和反射
基于<Java编程思想>第四版 前言 我们要操作一个类实例对象时,一般都要先知道这个类有哪些方法或者成员变量.反射就是在我们不知道这个类有哪些方法或成员变量时,使用特定方式得到类的这些信息 ...
- Java建造(Builder)模式
一.什么是建造模式: 建造模式可以将一个产品的内部表象与产品的生成过程分割开来,从而使一个建造过程生成具有不同内部表象的产品.客户端不需要知道产品内部的结构和生产过程. 二.建造模式的结构: Buil ...