我在java类上,我不在servlet上
这个问题没有重复,我不在 servlet 上,请各位。
通过 java 类,我的意思是:
class HelloWorld{}
我在服务器上,我想允许客户端下载文件
这是我的代码:
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/downloadFile")
public Response getFile() {
File file = new File("/roma.txt");
System.out.println("path = " + file.getPath());
System.out.println("absoulte path = " + file.getAbsolutePath());
return Response
.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition",
"attachment; filename=\"" + file.getName() + "\"") // optional
.build();
}
the file is located in the WebContent
of my project as you see here:
我得到了这个例外:
java.io.FileNotFoundException: /roma.txt (No such file or directory)
尝试删除“/”,
File file = new File("roma.txt");
“/roma.txt”可能指的是根目录。
这可以在 Windows、Linux、Mac OSX 中工作:
File myFile = new File(System.getProperty("user.home"), "roma.txt");
然后“roma.txt”将放入当前用户的主目录中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)