先說上傳:
前台上傳文件的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. 檢查文件路徑 、文件名稱 是否正確