先说上传:

前台上传文件的js代码:

 var demoListView = $('#demoList')
,uploadListIns = upload.render({
elem: '#testList'
,url: 'emailAction_upload'
,accept: 'file'
,multiple: true
,auto: false
,bindAction: '#testListAction'
,size:4096
,drag:true
,field:'upload'
,choose: function(obj){
var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
//读取本地文件
obj.preview(function(index, file, result){
var tr = $(['<tr id="upload-'+ index +'">'
,'<td>'+ file.name +'</td>'
,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
,'<td>等待上传</td>'
,'<td>'
,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
,'</td>'
,'</tr>'].join('')); //单个重传
tr.find('.demo-reload').on('click', function(){
obj.upload(index, file);
}); //删除
tr.find('.demo-delete').on('click', function(){
delete files[index]; //删除对应的文件
tr.remove();
uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
}); demoListView.append(tr);
});
}
,done: function(res, index, upload){
if(res.code == 0){ //上传成功
var tr = demoListView.find('tr#upload-'+ index)
,tds = tr.children();
tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
tds.eq(3).html(''); //清空操作
return delete this.files[index]; //删除文件队列已经上传成功的文件
}
this.error(index, upload);
}
,error: function(index, upload){
var tr = demoListView.find('tr#upload-'+ index)
,tds = tr.children();
tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
}
}); 

注:经本人测试 layui多文件下载为选中的文件一个个上传,有多少文件访问后台多少次

前台下载代码:

var url="<%=basePath%>/emailAction_down?fileName="+data.fileName;
window.location.href=url;

后台上传下载代码:

//上传文件名称, 文件名称= 控件名+FileName;
private String uploadFileName;
//上传文件路径
private String uploadpath;
//上传文件的控件名称
private File upload;
//标题
//上传文件的类型 ,文件的类型=控件名+ContentType;
private String uploadContentType;
//上传文件名,不包括路径
private String fileName;
//文件路径
private String inputPath;
//保存文件名
private String fileList[]; //上传
public String upload() throws Exception{
System.out.println("upload="+upload);
System.out.println("uploadContentType="+uploadContentType);
System.out.println("uploadFileName="+uploadFileName);
//获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//uploadFileName=new String(uploadFileName.getBytes("ISO-8859-1"),"UTF-8");
// fileName=uploadFileName.substring(0, uploadFileName.indexOf("."))+"("+ContextUtils.dateToStrLong(new Date())+")"+"."+uploadFileName.substring
//    (uploadFileName.lastIndexOf(".") + 1);
System.out.println("fileName="+fileName);
uploadpath=request.getRealPath("/upload")+"/"+uploadFileName; System.out.println("uploadPath="+uploadpath);
FileInputStream fis = new FileInputStream(upload);
FileOutputStream fos = new FileOutputStream(uploadpath);
//一次上传的字节
byte[] b = new byte[4096];
//循环上传
while(fis.read(b, 0, b.length)!=-1){
fos.write(b);
}
fos.flush();
fos.close();
fis.close(); try {
base.saveOrUpdate(emailFile);
}catch(Exception e) {
e.printStackTrace();
}
    return null;
}
  //下载

  public String down() {
    try {
      fileName=new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
      inputPath="upload/"+ fileName;
      System.out.println(inputPath);
      setInputPath(inputPath);
     } catch (Exception e) {
      e.printStackTrace();
     }
    return SUCCESS;
  }
  //获取文件下载输出流
  public InputStream getInputStream(){
    return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
  }

  public String getUploadFileName() {
    return uploadFileName;
  }
  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }
  public String getUploadpath() {
    return uploadpath;
  }
  public void setUploadpath(String uploadpath) {
    this.uploadpath = uploadpath;
  }
  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 getFileName() {
    return fileName;
  }
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
  public String getInputPath() {
    return inputPath;
  }
  public void setInputPath(String inputPath) {
    this.inputPath = inputPath;
  }
  public String[] getFileList() {
    return fileList;
  }
  public void setFileList(String[] fileList) {
    this.fileList = fileList;
  }

  xml代码:

<a        ction name="emailAction_*" class="com.ht.user.action.EmailAction" method="{1}">
<!-- result标签 名称不能大写 -->
<result name="success" type="stream">
<!-- 下载参数 -->
<!--由getInputStream()方法获得inputStream-->
<!-- inputStream 为action生成文件流的函数名 get + Name -->
<param name="inputName">inputStream</param>
<!-- 指定文件缓存 -->
<param name="bufferSize">4096</param>
<!--filename的值是action中动态传递的 -->
<!--contentDisposition是文件下载的处理方式,包括内联(inline)和附件(attachment),
默认是inline, 使用附件时这样配置:attachment;filename="文件名" 。-->
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

  

注:  以上使用ajax方法下载不了文件;

 ajxa传链接要获取全路径去下载 如:

public InputStream getInputStream(){
  String realPath = request.getRealPath("upload//") + uploadFileName;
  File file = new File(realPath);
  inputStream = new FileInputStream(file);
  return inputStream;
}

报该错误:

  Can not find a java.io.InputStream with the name [inputStream] in the invoca  解决方法:

  1. 检查  inputStream 是否为空

  2. 检查文件路径  、文件名称  是否正确

Struts2配合layui多文件上传--下载的更多相关文章

  1. 深入分析JavaWeb Item47 -- Struts2拦截器与文件上传下载

    一.struts2中的拦截器(框架功能核心) 1.过滤器VS拦截器 过滤器VS拦截器功能是一回事. 过滤器是Servlet规范中的技术,能够对请求和响应进行过滤. 拦截器是Struts2框架中的技术. ...

  2. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  3. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  4. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  5. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  6. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  7. ssh框架文件上传下载

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Java中实现文件上传下载的三种解决方案

    第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...

  9. 2013第38周日Java文件上传下载收集思考

    2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...

随机推荐

  1. vlog.hpp

    //vov #ifndef VLOG_HPP #define VLOG_HPP #include <sys/time.h> #include <unistd.h> #ifnde ...

  2. 第 10 章 容器监控 - 078 - Docker 最常用的监控方案

    Docker 最常用的监控方案 当 Docker 部署规模逐步变大后,可视化监控容器环境的性能和健康状态将会变得越来越重要. Docker 自带的几个监控子命令: ps .top .stats 功能更 ...

  3. 《javascript经典入门》-day02

    <javascript经典入门>-day02 1.使用函数 1.1基本语法 function sayHello() { aler('Hello'); //...其他语句... } #关于函 ...

  4. IP通信实验感想

    经过几周的学习之后,我终于开始了IP通信的实验课. 我们利用H3C进行对ip地址进行设置,在对不同交换机下的pc进行连接测试 首先,我们对交换机和pc机器进行配置设置,ip地址分配分别从192.168 ...

  5. 学习笔记(一)HTML基础

    HTML 基础 HTML 简介 HTML (Hyper textmarkup language) 中文译名为"超文本标记语言",主要是通过 HTML 标记对网页中的文本.图片.声音 ...

  6. CentOS 7 配置nginx并默认强制使用https对http进行跳转

    1.安装nginx yum install nginx 2.启动nginx服务 service nginx start 3.开启防火墙80端口,云服务器和本地虚拟服务器各有不同,不再赘述. 4.访问你 ...

  7. 双目深度估计传统算法流程及OpenCV的编译注意事项

    起因: 1. 双目立体视觉中双目深度估计是非常重要且基础的部分,而传统的立体视觉的算法基本上都在opencv中有相对优秀的实现.同时考虑了性能和效率.因此,学习使用opencv接口是非常重要的. 2. ...

  8. extern "C"的用法

    文章转自开源电子论坛:http://www.openedv.com/forum.php?mod=viewthread&tid=7986 看一些程序的时候老是有 “#ifdef __cplusp ...

  9. 关于ajax的跨域

    在前端开发中,跨域是经常遇到的问题,也是面试最喜欢问的问题,究其根本原因,是浏览器的同源策略所致,是浏览器为了避免不同域名不能共享cookie以及locationstorage等等,发起请求的时候无法 ...

  10. DHCP服务

    DHCP服务 DHCP服务(需要dhcp命令):负责ip,掩码,网关地址,DNS地址等自动分发的软件服务 /usr/sbin/dhcpd或/usr/sbin/dhcrelay(中继命令):执行程序 / ...