Struts2配合layui多檔案上傳--下載
阿新 • • 發佈:2018-12-07
先說上傳:
前臺上傳檔案的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); DetachedCriteria dc=DetachedCriteria.forClass(EmailVo.class); dc.setProjection(Projections.max("emailId")); int i=base.getCount(dc); //獲取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(); emailFile=new EmailFileVo(); emailFile.setEmailId(i); emailFile.setFileName(fileName); emailFile.setFileSize(upload.length()/1000+"KB"); 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. 檢查檔案路徑 、檔名稱 是否正確