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文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
随机推荐
- Project Euler 75: Singular integer right triangles
题目链接 思路: 勾股数组,又称毕达格拉斯三元组. 公式:a = s*t b = (s^2 - t^2) / 2 c = (s^2 + t^2) / 2 s > t >=1 且为互质的奇数 ...
- Vnpy官网汇总
Vnpy首页:http://www.vnpy.cn/ Vnpy社区:https://www.vnpy.com/ Vnpy论坛:http://www.vnpie.com/forum.php Github ...
- springboot缓存注解——@Cacheable
@Cacheable: 1,方法运行之前,先查询Cache(缓存组件),按照cacheName指定的名字获取(CacheManager获取相应缓存) 第一次获取缓存如果没有Cache组件会自会自动创建 ...
- 异常:Error resolving template "xxx", template might not exist or might not be accessible...解决办法
在开发环境下正常,但使用jar运行时,报错Error resolving template template might not exist or might not be accessible,意思 ...
- python 字典 dict 该注意的一些操作
在用python处理dict 的时候,有几个该注意的地方,这里跟大家提一下: 1)操作dict 时,尽量少产生新的列表对象.比如: 遍历dict的时候,如果用 dic = {"a" ...
- nigix反向代理
参考: https://www.cnblogs.com/yycc/p/8185748.html
- mysql 压缩版配置
1.解压之后可以将该文件夹改名,放到合适的位置,个人建议把文件夹改名为MySQL Server 5.6,放到C:\Program Files\MySQL路径中.当然你也可以放到自己想放的任意位置. 2 ...
- impdp导入dmp数据实例
--创建表空间 create tablespace ALIX datafile 'F:\File\ALIX.ora' size 1000m; --创建数据库目录 create directory ex ...
- 基于C#简单实现多个word文件和Excel文件的全局字符串替换
公司整理文档工作中,出现了一个需要使用全局字符替换多个word文档.excel文档中的内容的需求.虽然office.WPS都有全局替换的功能(ctrl+h),但是文件过多,且需要替换多次,工作量还是比 ...
- Vue插槽:(2.6.0以后版本弃用slot和slot-scope,改用v-slot)
关于Vue插槽的概念,大家可以从vue官网的api查看,我是看到网站的对于初接触 这个要概念的人来说不是很清楚,我来贴下原码,就比较直观了 贴下原码: 具名插槽:v-slot:header Html: ...