从中获取文件时 spring form
我正进入(状态 null
值和如果我尝试这个代码的其余字段我的意思是 非 multipart input types
它的工作正常。在调试时我得到了 null
来自行的价值。如果我试着去取 image
从现有文件夹我在webapp下的图像,该网址能够在浏览器中显示图像,但无法从中读取值 files
使用浏览器并抱歉我的英语不好
编辑 如果我评论图像代码,应用程序工作正常,但当我介绍图像的代码我收到错误
MultipartFile file = domain.getImage(); //this is getting null
这是相关的代码 调节器
@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) {
MultipartFile file = domain.getImage();
if (image== null)
throw new NullPointerException("unable to fetch "+file); //getting NPE everytime
String rootDirectory = httpServletRequest.getSession().getServletContext().getRealPath("/");
if (domain.getImage() != null && !domain.getImage().isEmpty())
try {
File path = new File(rootDirectory + "images\\" + domain.getFirstName() + ".png");
file.transferTo(path);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
repositiry.addToList(domain);
return "redirect:/";
}
form.jsp
<form:form modelAttribute="domain" enctype="multipart/form-data">
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>
DispatcherServlet的
<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>
我添加了一些额外的代码来查找我是否收到了 domain
如 null
变得如此。我不知道如何解决这个问题。
添加文件检查后我收到错误
java.lang.NullPointerException: unable to fetch : null
domain.java
public class Domain {
private String firstName;
private String lastName;
private MultipartFile image;
//getters and setters
注意 任何有用的答案,如果它有其他工作方式也欢迎:)
任何帮助表示赞赏,谢谢:)