七 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中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...
随机推荐
- [.net core] 在 Windows 中运行出现 WinHttpException: The parameter is incorrect
有一个 web 服务一直跑在 docker 中,今天需要在 Windows 上部署一个备份版本,于是,签出源代码,编译,运行.结果抛出 500 ,日志中有如下记录: System.Net.Http.H ...
- AJAX跨域请求详解
最近开始学习ajax,学习ajax必须得掌握的就是跨域请求,实际上在不同源的地址上发送请求就是跨域请求 域名地址的组成: http:// www . google : 8080 / script/jq ...
- cAdvisor+Prometheus+Grafana监控docker
cAdvisor+Prometheus+Grafana监控docker 一.cAdvisor(需要监控的主机都要安装) 官方地址:https://github.com/google/cadvisor ...
- MS SQL 锁与事务
加锁的主要目的是为了防止并发操作时导致的数据不一致等问题,锁分为共享锁(S).更新锁(U).排他锁(X),共享锁与更新只是单向兼容?传说中的单相思? 事务 事务能保证数据操作的原子性,要么内部操作都提 ...
- ASP.NET Core 使用 Google 验证码(reCAPTCHA v3)代替传统验证码
写在前面 友情提示: Google reCAPTCHA(v3下同) 的使用不需要"梯子",但申请账号的时候需要! Google reCAPTCHA 的使用不需要"梯子&q ...
- .net core api +swagger(一个简单的入门demo 使用codefirst+mysql)
前言: 自从.net core问世之后,就一直想了解.但是由于比较懒惰只是断断续续了解一点.近段时间工作不是太忙碌,所以偷闲写下自己学习过程.慢慢了解.net core 等这些基础方面学会之后再用.n ...
- MySQL数据库中的四种隔离级别
事务的隔离性比想象的要复杂,在 SQL 标准中定义了四种级别的隔离级别.通常而言,较低级别的隔离通常可以执行更高的并发,系统的开销也更低 READ UNCOMMITTED 该级别为未提交读.在该级别中 ...
- Yii2设计模式——Yii2中用到哪些设计模式?
"Yii2设计模式"包含了两个方面的内容:1.设计模式,2.Yii2框架. <设计模式>一书虽然以JAVA语言来表达设计模式的思想,但是设计模式远不限制于某一种特定的语 ...
- hive基本操作与应用
通过hadoop上的hive完成WordCount 启动hadoop Hdfs上创建文件夹 上传文件至hdfs 启动Hive 创建原始文档表 导入文件内容到表docs并查看 用HQL进行词频统计,结果 ...
- Basic Linux Privilege Escalation
(Linux) privilege escalation is all about: Collect - Enumeration, more enumeration and some more enu ...