Spring并从web服务器外部提供文件
我想让spring为Spring并从web服务器外部提供文件,spring,spring-mvc,Spring,Spring Mvc,我想让spring为/tmp…目录中的某些文件提供服务,特定的目录直到服务器启动后不久才确定,所以使用似乎不起作用。正如Dave Newton指出的那样-从控制器中传输它们。一个非常基本的实现: @RequestMapping("/static/temp/{path}") public void getResource(@PathVariable path, OutputStream os) { //TODO proper IO management InputStream
/tmp…
目录中的某些文件提供服务,特定的目录直到服务器启动后不久才确定,所以使用
似乎不起作用。正如Dave Newton指出的那样-从控制器中传输它们。一个非常基本的实现:
@RequestMapping("/static/temp/{path}")
public void getResource(@PathVariable path, OutputStream os) {
//TODO proper IO management
InputStream is = new BufferedInputStream(new FileInputStream("/temp/" + path));
IOUtils.copy(is, os);
}
我这样做:
@RequestMapping(value="/staticFile/{id}", method = RequestMethod.GET)
public void getPhotoRide2(HttpServletResponse response, @PathVariable int id) {
try {
FileInputStream in = new FileInputStream("your file");
OutputStream out = response.getOutputStream();
response.setContentType("your mime type");
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {}
}
将它们从控制器流回;具体是什么问题?这两个参数应该是什么?没有具体的问题,我只是不能将目录放在application-context.xml中,因为将要使用的目录是动态生成的。目录是如何生成的?当然有一个具体的问题,否则你不会问问题。如果你知道文件在哪里,或者可以通过任何方式找到,我不知道问题出在哪里——有很多方法可以提供web应用程序信息,但是如果不知道你实际上在做什么,就不可能提供更多帮助。