先说上传:
前台上传文件的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 = $([''
,''+ file.name +' '
,''+ (file.size/1014).toFixed(1) +'kb '
,'等待上传 '
,''
,''
,''
,' '
,' '].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('上传成功');
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('上传失败');
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代码:
注: 以上使用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. 检查文件路径 、文件名称 是否正确