Struts2配合layui多文件上传--下载
先说上传:
前台上传文件的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多文件上传--下载的更多相关文章
- 深入分析JavaWeb Item47 -- Struts2拦截器与文件上传下载
一.struts2中的拦截器(框架功能核心) 1.过滤器VS拦截器 过滤器VS拦截器功能是一回事. 过滤器是Servlet规范中的技术,能够对请求和响应进行过滤. 拦截器是Struts2框架中的技术. ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
- JAVA Web 之 struts2文件上传下载演示(一)(转)
JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- Struts的文件上传下载
Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...
- ssh框架文件上传下载
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java中实现文件上传下载的三种解决方案
第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...
- 2013第38周日Java文件上传下载收集思考
2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
随机推荐
- 简单了解 DLL中, .def 文件及C#调用C++方法
DLL中导出函数的声明有两种方式: 1.在函数声明中加上__declspec(dllexport) //以下内容为 .h 文件中的内容 //向外界提供的端口 extern"C" _ ...
- python爬虫采集网站数据
1.准备工作: 1.1安装requests: cmd >> pip install requests 1.2 安装lxml: cmd >> pip install lxml ...
- sysbench工具安装使用
一.sysbench简介 Sysbench是一款开源的.跨平台的.模块化的.多线程的性能测试工具,通过高负载地运行在数据库上,可以执行CPU.内存.线程.IO.数据库等方面的性能测试.用于评估操作系统 ...
- nginx配置二级目录,反向代理不同ip+端口
场景描述: 通过二级目录(虚拟目录,应用程序)的方式访问同一ip+端口的不同应用,例如location是用户使用页面,location/admin/是管理页面,location部署在192.168.1 ...
- HDFS的一些重要流程
该随笔记录HDFS学习过程中遇到的比较重要的几个过程,包括:HDFS启动流程.DataNode备份流程.流程.写流程.删除流程.HDFS合并流程.这里都是从我的学习笔记中摘取出来的,如果哪里有误,还望 ...
- C#用正则表达式替换手机中间几位为*号 代码及解析
/// <summary> /// 替换手机号中间四位为* /// </summary> /// <param name="phoneNo">& ...
- rtmp详解
文件下载地址: 中文:https://files.cnblogs.com/files/bugutian/rtmp_specification_1.0_cn.zip 英文:http://www.adob ...
- gitlab安装随记
gitlab安装 配置yum源 sudo vim /etc/yum.repos.d/gitlab-ce.repo 按照网上别人的例子,修改为清华的源 [gitlab-ce] name=Gitlab C ...
- 文科生打开python的大门
作为唯一的一名教育学院的学生,加入python课程,一定要声明我可不是并不是被迫选课的!虽然是文科生,但是是对编程这种东西很感兴趣的文科生.从站在python门口的张望,到现在悄悄把门打开,越来越感觉 ...
- fatal error C1010: 在查找预编译头时遇到意外的文件结尾
错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "stdafx.h"”? 错误分析: 此错误发生 ...