各位高手,我想在自己的web应用中实现文件下载.代码片断如下:
File file = new File("../file/experttable.xls");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment; filename="+file.getName());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
bos.flush();
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(IOException e) {
System.out.println("io exception" + e.getMessage());
} finally {
if (bis != null) bis.close();
if (bos != null) bos.close();
}为什么总是说找不到file的路径呢?
webroot路径如下:
webroot--
|----XXX.jsp
|----file
|
|----xxx.xls
File file = new File("../file/experttable.xls");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment; filename="+file.getName());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
bos.flush();
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(IOException e) {
System.out.println("io exception" + e.getMessage());
} finally {
if (bis != null) bis.close();
if (bos != null) bos.close();
}为什么总是说找不到file的路径呢?
webroot路径如下:
webroot--
|----XXX.jsp
|----file
|
|----xxx.xls
File file = new File("file/experttable.xls");
自己顶下.