七 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中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...
随机推荐
- 漏洞经验分享丨Java审计之XXE(上)
最近在审计公司的某个项目时(Java方面),发现了几个有意思的Blind XXE漏洞,我觉得有必要分享给大家,尤其是Java审计新手,了解这些内容可以让你少走一些弯路. Java总体常出现的审计漏洞如 ...
- 网络协议 18 - CDN:家门口的小卖铺
[前五篇]系列文章传送门: 网络协议 13 - HTTPS 协议:加密路上无尽头 网络协议 14 - 流媒体协议:要说爱你不容易 网络协议 15 - P2P 协议:小种子大学问 网络协议 16 - D ...
- IntelliJ IDEA~gradle环境配置
Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置. Int ...
- LindDotNetCore~docker里图像上生成中文乱码问题
回到目录 因为docker上的大部分镜像都是基于linux系统的,所以在向图像中写中文时需要考虑中文字体问题,例如在microsoft/aspnetcore2.0这个镜像,它是基于debian系统的, ...
- C#委托、事件、线程
这是几个简单的例子,但是实际的开发中委托还还只在反射时用到过,事件的话只自己做了一次,并且还是特意去用的 ,实际上可以不用.线程的话,因为需要,所以用的会多点,这里主要是WS上的线程. 委托 在前面的 ...
- 30分钟ES6从陌生到熟悉
前言 ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScript 语言可以用来编写复杂的 ...
- Docker最全教程之Ubuntu下安装Docker(十四)
前言 Ubuntu是一个以桌面应用为主的开源GNU/Linux操作系统,应用很广.本篇主要讲述Ubuntu下使用SSH远程登录并安装Docker,并且提供了Docker安装的两种方式,希望对大家有所帮 ...
- SLAM+语音机器人DIY系列:(四)差分底盘设计——3.底盘通信协议
摘要 运动底盘是移动机器人的重要组成部分,不像激光雷达.IMU.麦克风.音响.摄像头这些通用部件可以直接买到,很难买到通用的底盘.一方面是因为底盘的尺寸结构和参数是要与具体机器人匹配的:另一方面是因为 ...
- DSAPI QQ用户相关
获取指定QQ号头像 Label1.Image=DSAPI.QQ用户相关.下载QQ头像("20353841") 获取指定QQ群头像 Label1.Image = DSAPI.QQ用户 ...
- 校园生活app结对开发第一天
今天刚开始开发,要安装android studio及熟悉软件操作