我在程序里生成一个word放到 服务器里 比如放到D盘 然后要怎么返回这个word的 下载链接 然后我在本地直接访问链接就能下载
如果d盘不是你web服务器的路径,你可以用FileInputStream
String basePath = request.getSession().getServletContext().getRealPath("d:/路径");
//System.out.println(basePath);
String filedisplay = "文件名.doc";
String filedownload = basePath + File.separator + "文件名.doc";
System.out.println("----------------------"+filedownload);
response.setContentType("applicaiton/x-download");
response.addHeader("Content-Disposition", "attachment;filename="+filedisplay);
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
is = new FileInputStream(new File(filedownload));
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
bos.write(b,0,len);
}
bis.close();
is.close();
bos.close();
os.close();
spring.mvc.static-path-pattern=/data/**
spring.resources.static-locations=file:D://doc/
用这个东西解决了
使用程序获取到绝对路径即可,或者你规定一个目录位置,配置到配置文件或者数据库
先将生成的word 存入一个地方,记住这个地址 进行下载就行
返回你服务器文件保存相对地址,拼接一下你的域名或ip就可以了