下載檔案,並彈出儲存提示框,選擇位置,絕對路徑 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
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");