我正在制作一个春季项目,我正在尝试上传图片
editProfile.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<tr>
<td>
<form:form action="${pageContext.request.contextPath}/editprofilehandler" method="POST" modelAttribute="userForm" enctype="multipart/form-data">
<table>
<tr>
<td>
First Name :
</td>
<td>
<form:input path="firstName" value="${userDetails[0].firstName }" /> <form:errors path="firstName" class="error" />
</td>
</tr>
<tr>
<td>
Last Name :
</td>
<td>
<form:input path="lastName" value="${userDetails[0].lastName }" /> <form:errors path="lastName" class="error" />
</td>
</tr>
<tr>
<td>
EmailID Name :
</td>
<td>
<form:input path="emailID" value="${userDetails[0].emailID }" /> <form:errors path="emailID" class="error" />
</td>
</tr>
<tr>
<td>
Mobile No :
</td>
<td>
<form:input path="mobileNo" value="${userDetails[0].mobileNo }" /> <form:errors path="mobileNo" class="error" />
</td>
</tr>
<tr>
<td>
Date of birth :
</td>
<td>
<form:input path="dateOfBirth" value="${userDetails[0].dateOfBirth }" /> <form:errors path="dateOfBirth" class="error" />
</td>
</tr>
<tr>
<td>
Gender :
</td>
<td>
<form:input path="gender" value="${userDetails[0].gender }" /> <form:errors path="gender" class="error" />
</td>
</tr>
<tr>
<td>
Profile image :
</td>
<td>
<input type="file" name="uploadImage" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Update" />
</td>
</tr>
</table>
</form:form>
</td>
</tr>
</table>
Controllerr.java
@SuppressWarnings("resource")
@RequestMapping("/editprofilehandler")
public ModelAndView editProfileHandlerController(@ModelAttribute("userForm") Users users , ModelMap model, HttpSession session)
{
if(session.getAttribute("session_user") != null){
try
{
InputStream inputStream = null;
OutputStream outputStream = null;
MultipartFile file = users.getImage();
String fileName = file.getOriginalFilename();
inputStream = file.getInputStream();
File newFile = new File("C:/Documents and Settings/smart/workspace/Pir/WebContent/resources/images/profile/" + fileName);
if(!newFile.exists())
{
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while((read = inputStream.read(bytes)) != -1)
outputStream.write(bytes, 0, read);
}
catch(Exception e){}
List<Object[]> userDetails = this.userFunctionsService.getUserDetails(((UserLoginDetails)session.getAttribute("session_user")).getEmailID());
model.addAttribute("userDetails", userDetails);
return new ModelAndView("editProfile", model);
}
else
return new ModelAndView("redirect:/");
}
Users.java
@Entity
@Table(name = "users")
public class Users {
@NotNull
@Id
@Column(name = "userID")
private int userID;
@NotNull
@Size(min=3, max=50)
@Pattern(regexp = ".+@.+\\.[a-z]+")
@Column(name = "emailID")
private String emailID;
@NotNull
@Size(min=3, max=50)
@Column(name = "firstName")
private String firstName;
@NotNull
@Size(min=3, max=50)
@Column(name = "lastName")
private String lastName;
@NotNull
@Size(min=3, max=50)
@Column(name = "password")
private String password;
@NotNull
@Column(name = "mobileNo")
private String mobileNo;
@Column(name = "imageURL")
private String imageURL;
@NotNull
@DateTimeFormat(pattern="MM/dd/yyyy")
@Column(name = "dateOfBirth")
private Date dateOfBirth;
@NotNull
@Column(name = "gender")
private String gender;
@NotNull
@Column(name = "userType")
private String userType;
@Lob
private MultipartFile image;
public void setUserID(int userID)
{
this.userID = userID;
}
public int getUserID()
{
return userID;
}
public void setEmailID(String emailID)
{
this.emailID = emailID;
}
public String getEmailID()
{
return emailID;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return password;
}
public void setMobileNo(String mobileNo)
{
this.mobileNo = mobileNo;
}
public String getMobileNo()
{
return mobileNo;
}
public void setImageURL(String imageURL)
{
this.imageURL = imageURL;
}
public String getImageURL()
{
return imageURL;
}
public void setDateOfBirth(Date dateOfBirth)
{
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfBirth()
{
return dateOfBirth;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getGender()
{
return gender;
}
public void setUsetType(String userType)
{
this.userType = userType;
}
public String getUserType()
{
return userType;
}
public void setImage(MultipartFile image)
{
this.image = image;
}
public MultipartFile getImage()
{
return image;
}
}
错误:
java.lang.NumberFormatException: For input string: "imageURL"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
org.apache.jsp.view.profile_jsp._jspService(profile_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
org.apache.jsp.template.mainTemplate_jsp._jspx_meth_tiles_005finsertAttribute_005f2(mainTemplate_jsp.java:340)
org.apache.jsp.template.mainTemplate_jsp._jspService(mainTemplate_jsp.java:130)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221)
org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:132)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
imageURL
列是mysql中的varchar。如何解决这个错误?
修改
profile.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<tr>
<td valign="top">
<a href="${pageContext.request.contextPath }/changeimage">
<img src="${pageContext.request.contextPath }/resources/images/profile/${userDetails[0].imageURL }" height="145" width="200" />
</a>
</td>
<td>
<table>
<tr>
<td>
First Name :
</td>
<td>
<c:out value="${userDetails[0].firstName }" />
</td>
</tr>
<tr>
<td>
Last Name :
</td>
<td>
<c:out value="${userDetails[0].lastName }" />
</td>
</tr>
<tr>
<td>
EmailID Name :
</td>
<td>
<c:out value="${userDetails[0].emailID }" />
</td>
</tr>
<tr>
<td>
Mobile No :
</td>
<td>
<c:out value="${userDetails[0].mobileNo }" />
</td>
</tr>
<tr>
<td>
Date of birth :
</td>
<td>
<c:out value="${userDetails[0].dateOfBirth }" />
</td>
</tr>
<tr>
<td>
Gender :
</td>
<td>
<c:out value="${userDetails[0].gender }" />
</td>
</tr>
</table>
</td>
<td align="right" valign="top">
<a href="${pageContext.request.contextPath }/editprofile">Edit</a>
</td>
</tr>
</table>
答案 0 :(得分:1)
在将列表放入 Controllerr.java 中的模型之前,您错过了一个将List<Object[]>
转换为List<User>
的循环。
或者您应该更改代码
将返回List<User>
而不是List<Object[]>
。
希望这将解决问题