//获取跟目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println("path:"+path.getAbsolutePath());
//如果上传目录为/static/images/upload/,则可以如下获取:
File upload = new File(path.getAbsolutePath(),"static/images/upload/");
if(!upload.exists()) upload.mkdirs();
System.out.println("upload url:"+upload.getAbsolutePath());
public void downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = "devicesInfo-model.xls";// 文件名
if (fileName != null) {
//设置文件路径
File file= null;
try {
file = new File(ResourceUtils.getURL("classpath:").getPath()+"\\static\\exportMould\\"+fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (file.exists()) {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("设备基础信息导入模板.xls", "UTF-8"));// 设置文件名
byte[] buffer = new byte[1024];
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
//return "下载成功";
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//return "下载失败";
}
网友评论