文件夾是存在的,但是浏覽器死活不彈出保存對話框
String tempPath ="E:\\Studytools\\apache-tomcat-6.0.41\\webapps\\report\\uploads\\00000001_20150525190016.zip";
System.out.println(tempPath);
File file = new File(tempPath);
/* 如果文件存在 */
if (file.exists()) {
String disName = URLEncoder.encode(file.getName(), "UTF-8");
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; filename=\"" + disName + "\"");
int fileLength = (int) file.length();
response.setContentLength(fileLength);
/* 如果文件長度大於0 */
if (fileLength != 0) {
/* 創建輸入流 */
InputStream ins = new FileInputStream(file);
byte[] buffer = new byte[4096];
/* 創建輸出流 */
ServletOutputStream ous = response.getOutputStream();
int readLength;
while (((readLength = ins.read(buffer)) != -1)) {
ous.write(buffer, 0, readLength);
}
ins.close();
ous.flush();
ous.close();
} else{
System.out.println("length is null");
}
}else{
System.out.println("not found");
}
你浏覽器直接訪問的servlet還是ajax?ajax可不會彈出保存對話框,要window.location='xxxx'來下載