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文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
随机推荐
- cordova自定义插件的创建过程
最近学习了cordova插件,记录一下大概的过程,仅供参考. 前期的配置就不记录了网上好多. 在简书上从新写了一个更详细的cordova插件教程,有需要的可以点这里进去看看. 第一步 创建一个cord ...
- web安全测试--sql注入攻击
先要自行了解sql的几个概念: 1. or '1'='1' 2. order by 3. union : 联合查询需要表字段相同 sql注入攻击漏洞判断步骤: 1.‘ 2.查看数据库信息 3.绕过过 ...
- 关于隐式创建vue实例实现简化弹出框组件显示步骤
我们在使用vue写alert组件的时候,经常是定义了一个alert.vue,然后引入alert.vue,然后配置参数等等,非常繁琐,那有没有一种方式可以像window.alert("内容&q ...
- zabbix报错gd、freetype、png、jpeg
安装包位置:http://www.p-pp.cn/app/zabbix/ 1.安装freetype [root@localhost softs]# tar xf freetype-2.5.0.tar. ...
- flutter安装教程(win7)
本文是在安装flutter的时候,所遇到的问题参考的各个文档整理集合.此次是在win7上安装的问题记录.因为当初安装的时候针对win7的文档比较少,而且各个文档的解释比较散,本人遇到问题也是找了很久才 ...
- 菜鸟脱壳之脱壳的基础知识(六)——手动查找IAT和修复Dump的程序
前面讲了如何寻找OEP和脱壳,有的时候,Dump出来的时候不能正常运行,是因为还有一个输入表没有进行处理,一些加密壳会在IAT加密上面大做文章,用HOOK - API的外壳地址来代替真是的IAT的地址 ...
- 浏览器中打开PDF链接
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- jsp页面输出当前时间
<% out.print(new java.text.SimpleDateFormat("yyyy年MM月dd号 hh:mm:ss").format(new Date())) ...
- javascript高级程序设计第3版——第6章 面向对象的程序设计
第六章——面向对象的程序设计 这一章主要讲述了:面向对象的语言由于没有类/接口情况下工作的几种模式以及面向对象语言的继承: 模式:工厂模式,构造函数模式,原型模式 继承:原型式继承,寄生式继承,以及寄 ...
- mysql锁分析相关的几个系统视图
1.infomation_schema.innodb_lock_waits+-------------------+-------------+------+-----+---------+----- ...