配置文件

<?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 文件上传和下载的更多相关文章

  1. Struts2文件上传和下载(原理)

    转自:http://zhou568xiao.iteye.com/blog/220732 1.    文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1)     ...

  2. 十六、Struts2文件上传与下载

    文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...

  3. 【SSH2(实用文章)】--Struts2文件上传和下载的例子

    回想一下,再上一篇文章Struts2实现机制,该步骤做一步一步来解决,这种决心不仅要理清再次Struts2用法.映射机制及其在深入分析.最后一个例子来介绍Struts2一种用法,这里将做一个有关文件上 ...

  4. 学习Struts--Chap07:Struts2文件上传和下载

    1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...

  5. struts2 文件上传和下载,以及部分源代码解析

    struts2 文件上传 和部分源代码解析,以及一般上传原理 (1) 单文件上传 一.简单介绍 Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form ...

  6. (八)Struts2 文件上传和下载

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...

  7. struts2学习(13)struts2文件上传和下载(1)

    一.Struts2文件上传: 二.配置文件的大小以及允许上传的文件类型: 三.大文件上传: 如果不配置上传文件的大小,struts2默认允许上传文件最大为2M: 2097152Byte:   例子实现 ...

  8. Struts2文件上传与下载

    一,页面 index.html 在页面中最重要的就是这个文件上传用的 form 表单,注意这里一定要把 form 的encyType属性明确标定为“multipart/form-data”,只有这样. ...

  9. struts2文件上传和下载

    1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...

随机推荐

  1. .NET 机器学习生态调查

    机器学习是一种允许计算机使用现有数据预测未来行为.结果和趋势的数据科学方法. 使用机器学习,计算机可以在未显式编程的情况下进行学习.机器学习的预测可以使得应用和设备更智能. 在线购物时,机器学习基于历 ...

  2. GitHub的Repository权限将public转为private

    2019年1月7日,GitHub CEO Nat Friedman 于官方博客公开发文,称“New year, new GitHub”,宣布从此将免费无限地为普通用户提供私有仓库服务. 因此,我们可以 ...

  3. Kubernetes 笔记 11 Pod 扩容与缩容 双十一前后的忙碌

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...

  4. 卷积神经网络(Convolutional Neural Network,CNN)

    全连接神经网络(Fully connected neural network)处理图像最大的问题在于全连接层的参数太多.参数增多除了导致计算速度减慢,还很容易导致过拟合问题.所以需要一个更合理的神经网 ...

  5. Abp框架之执行Update-Database 命令系列错误

    废话不多说,直接开门见山.首先的 第一个错误:一般都是,碰到这个问题不要慌,先不要急着去查看sql服务是否开启,首先按F5启动项目,报错之后直接终止项目,然后再执行Update-Database命令 ...

  6. 总结C语言字符检测函数:isalnum、isalpha...

    前言:最近一直在刷leetcode的题,用到isalnum函数,用man手册查找了一下,总共有13个相关函数如下: #include <ctype.h> int isalnum(int c ...

  7. 初探Parcel

    昨天趁有点时间看了前不久很火的构建工具Parcel,这里说下初步使用的感受,尤其是将其放到实际项目中和Webpack进行比较. 一.前言 首先说下笔者目前的技术栈.最近的前端项目主要以管理后台为主,技 ...

  8. swagger文档转换为WebApiClient声明式代码

    1 swagger简介 Swagger是一个规范且完整的框架,提供描述.生产.消费和可视化RESTful Web Service.其核心是使用json来规范描述RESTful接口,另外有提供UI来查看 ...

  9. docker-compose使用备忘(转)

    Docker-Compose简介 Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排. Docker-Compose将所管理的容器分为三层,分别是工 ...

  10. Jmeter API Performance Test

    笔者最近了解 Apache组织开发了基于Java的压力测试工具Apache JMeter.如有兴趣可自行搜索它的相关信息.笔者记录了一些使用方法,如有错误或遗漏,欢迎联系改正. 官方下载地址:http ...