下載文件,並彈出保存提示框,選擇位置,絕對路徑 or相對路徑

1.因爲Aiax不能返回流 所以用js表單提交發送請求


var form = document.createElement("form");
document.body.appendChild(form);
var nameInput = document.createElement("input");
var pathInput = document.createElement("input");
nameInput.type = "hidden";
pathInput.type = "hidden";
form.appendChild(nameInput);
form.appendChild(pathInput);
nameInput.value =data;
pathInput.value=name;
nameInput.name = "filePath";
pathInput.name = "fileName";
form.action = "${ctx}/XX/download";
form.submit();

傳入需要的參數


2.controller

  response.setContentType("application/octet-stream");
        response.setContentType("application/OCTET-STREAM;charset=UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+localName);
=   ======彈出保存框即資源選擇器  



   File file=Upload.downLoadFile(filePath,fileName);//ftp獲取文件------絕對路徑   File  file =new File("c:/xx/tt.ext");========相對路徑

   FileInputStream fis = new FileInputStream(file);

          BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
          byte[] buffer = new byte[1024];
          int len;
          while ((len = fis.read(buffer)) != -1) {
          out.write(buffer, 0, len);
          out.flush();
          }

     絕對路徑:服務器上的文件, 絕對路徑  根據路徑和名字通過Ftp查找到file並返回   ======ftp獲取文件請參照博客中ftp下載 在將所得文件輸入保存

                

    相對路徑:相對路徑只需new File(相對路徑);即可得到文件

    最後保存文件的名字如果只保存英文: 需要將localName轉成GB2312           String localName = new String(fileName.getBytes("GB2312"), "ISO_8859_1"); 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章