Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Spring
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Spring
unable to fetch image using springMVC
Pankaj Pathania
Greenhorn
Posts: 5
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
while fetching Image from browser to local file I am getting null
following is my code
ControllerClass
@Controller public class HomeController { @Autowired RepositiryClass repositiry; @RequestMapping("/") public String getHomePage(Model model) { model.addAttribute("domains", repositiry.getList()); return "home"; } @RequestMapping(value = "/form", method = RequestMethod.GET) public String formInputGet(Model model) { model.addAttribute("domain", new Domain()); return "form"; } @RequestMapping(value = "/form", method = RequestMethod.POST) public String formInputPost(@ModelAttribute("domain") Domain domain, HttpServletRequest httpServletRequest, BindingResult result) { String[] supressedFeilds = result.getSuppressedFields(); if (supressedFeilds.length > 0) throw new RuntimeException( "Attempting to bind disallowed feilds " + StringUtils.arrayToCommaDelimitedString(supressedFeilds)); String rootDirectory = httpServletRequest.getServletContext().getRealPath("/"); System.out.println("root directory : " + rootDirectory); File file = new File(rootDirectory + "\\images\\" + domain.getImage()); System.out.println(file.getAbsolutePath()); MultipartFile image = domain.getImage(); if (image != null && !image.isEmpty()) { try { System.out.println("image is not empty"); image.transferTo(file); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } } else System.out.println("inside else block"); repositiry.addToList(domain); return "redirect:/"; }
dispatcher
Servlet
<mvc:annotation-driven /> <mvc:resources location="/images/" mapping="/images/**" /> <context:component-scan base-package="com" /> <bean id="multipartReslover" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10240000" /> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean>
and my form
<form:form modelAttribute="domain" enctype="multipart/form-data" method="post"> First Name<br> <form:input path="firstName" /> <br>Last Name :<br> <form:input path="lastName" /> <br>upload Image<br> <form:input path="image" type="file" /> <hr> <input type="submit"> </form:form>
if you can see there is a statement
System.out.println("inside else block");
inside method formInputPost(,,) and this is the output I am getting instead of image and expected names to home.jsp
how to sort that
Thanks
Pankaj Pathania
Greenhorn
Posts: 5
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
while correction some metadata in configuration file it resolved the issue
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
how to resolve this null pointer exception
controller is not mapping properly
How can I get a normal input tag's value?
Help With File Upload
Passing a new object in Spring MVC model.addAttribute() method
More...