![](/img/trans.png)
[英]How to retrieve the right URL of a file uploaded (with spring MVC) on the server (Tomcat)
[英]Unable to retrieve uploaded file using url
我已成功將文件上傳到服務器,並且使用設置了路徑
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/**]
仍然無法使用url訪問圖像。 代碼有什么問題?
String rootPath ="/home/me/Documents/tfmImages/";
File dir = new File(rootPath + File.separator + profile.getUser().getUserId()
+"_user.png");
您必須在png,jpg,....根之后添加文件擴展名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.