简体   繁体   中英

Unable to retrieve uploaded file using url

I successfully uploaded file to the server and the path is set using

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

and I can see the file in the directory but when I tried to access the flie using url (localhost:8080/TFM/uploads/b26fc1011c164fa384fa05d845e9bd7a.png),it is giving 404.

This is the resource handler method

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

I am using the same path in both functions.

This is the console output when I send the request

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

It is showing a matching pattern for the request

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

and still I am not able to access the image using url. What is wrong with the code?

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

you must add the extension for your file after the root png , jpg , .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM