无法使用网址检索上传的文件

时间:2017-11-05 11:15:53

标签: java spring spring-mvc

我成功将文件上传到服务器,并使用

设置路径
String rootPath ="/home/me/Documents/tfmImages/";
File dir = new File(rootPath + File.separator + profile.getUser().getUserId());

我可以在目录中看到该文件,但是当我尝试使用url(localhost:8080 / TFM / uploads / b26fc1011c164fa384fa05d845e9bd7a.png)访问flie时,它正在给404.

这是资源处理程序方法

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/uploads/**").addResourceLocations("file:///home/me/Documents/tfmImages/");
}

我在两个函数中使用相同的路径。

这是我发送请求时的控制台输出

DispatcherServlet with name 'dispatcherServlet' processing GET request for [/TFM/uploads/0c10f6799e494574a301a2319b71fc64.png]
Looking up handler method for path /uploads/0c10f6799e494574a301a2319b71fc64.png
Did not find handler method for [/uploads/0c10f6799e494574a301a2319b71fc64.png]
Matching patterns for request [/uploads/0c10f6799e494574a301a2319b71fc64.png] are [/uploads/**]
URI Template variables for request [/uploads/0c10f6799e494574a301a2319b71fc64.png] are {}
Mapping [/uploads/0c10f6799e494574a301a2319b71fc64.png] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[URL [file:/home/me/Documents/sts-bundle/pivotal-tc-server-developer-3.2.6.RELEASE/base-instance/wtpwebapps/tfm/uploads/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@77e2411a]]] and 1 interceptor
Last-Modified value for [/TFM/uploads/0c10f6799e494574a301a2319b71fc64.png] is: -1
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Successfully completed request

显示请求的匹配模式

Matching patterns for request [/uploads/b26fc1011c164fa384fa05d845e9bd7a.png] are [/uploads/**]

仍然无法使用网址访问该图片。代码有什么问题?

1 个答案:

答案 0 :(得分:0)

 String rootPath ="/home/me/Documents/tfmImages/";
   File dir = new File(rootPath + File.separator + profile.getUser().getUserId() 
   +"_user.png");

你必须在root png,jpg,.....

之后为你的文件添加扩展名