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文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
随机推荐
- 两两交换链表中的节点(java实现)
题目: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你应该返回 ...
- HDU 5299 Circles Game
HDU 5299 思路: 圆扫描线+树上删边博弈 圆扫描线有以下四种情况,用set维护扫描线与圆的交点,重载小于号 代码: #pragma GCC optimize(2) #pragma GCC op ...
- NodeJS基础(二)
一.动态获取文件路径 var fs = require('fs') var path = require('path') // 一般在开发命令行工具的时候,这个设计是必须有用的一个特性 // npm ...
- HDU - 6054String and String
题意:给串s和t,对于串s每个位置有一个价值f,两种操作1.修改f[a]=b,2.查询串t子串Ta-b在s的子串Sc-d中出现位置的f和 题解:s和t建sam,把fail树按dfs序建bit套线段树, ...
- GDT与LDT
保护模式下的段寄存器 由 16位的选择器 与 64位的段描述符寄存器 构成段描述符寄存器: 存储段描述符选择器:存储段描述符的索引 PS:原先实模式下的各个段寄存器作为保护模式下的段选择器,80486 ...
- [luogu P2633] Count on a tree
[luogu P2633] Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点 ...
- Jenkins结合shell脚本实现(gitLab/gitHub)前端项目自动打包部署服务器
原始发布部署: 石器时代的我们,先是本地打包好项目,在去服务器上把原来的文件删了,然后回到本地copy到服务器: 这操看起来简单,实际部署的人就知道多烦了,假如来几个项目都要重新发布:那就爽了: 今天 ...
- 洛谷1855 榨取kkksc03
题目描述 洛谷2的团队功能是其他任何oj和工具难以达到的.借助洛谷强大的服务器资源,任何学校都可以在洛谷上零成本的搭建oj并高效率的完成训练计划. 为什么说是搭建oj呢?为什么高效呢? 因为,你可以上 ...
- Oracle查看存储过程最后编辑时间
场景:我们在实现一个需求编写存储过程时,在正式上线前,总会有多个修改版本,时间一长可能发现一个过程甚至有5个以上的版本,如果没有添加注释自己都分不清哪个版本是最新的,这时就可以通过查看该存储的最后编辑 ...
- js--map函数的使用
map( ) 属于操作数组的方法. 包含三个参数,item,index,arr 看一份代码: let arr = [ {title:'aaa',hot:true}, {title:'fff',hot ...