我无法在春季mvc上传文件,当我使用 enctype =" multipart / formdata" 时,文件将被上传,但在这种情况下,我无法将我的数据存储到数据库中,如果我删除 enctype =" multipart / formdata" ,它将成功存储到数据库中,但文件上传将无法正常工作
所以现在就像打开和关闭,如果一件事有效,那么其他的不会
告诉我解决方案我很困惑。
emp_form.jsp
<form class="stdform stdform2" method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/in_emp">
<p>
<label>Person image</label>
<span class="field"><input type="file" name="filenm" id="filenm" class="input-xxlarge" /></span>
</p>
<p>
<label>User Name</label>
<span class="field"><input type="text" name="uname" id="uname" class="input-xxlarge" /></span>
</p>
<p>
<label>Password</label>
<span class="field"><input type="text" name="pass" id="pass" class="input-xxlarge" /></span>
</p><p>
<label>Person Name</label>
<span class="field"><input type="text" name="pname" id="pname" class="input-xxlarge" /></span>
</p><p>
<label>Address</label>
<span class="field"><input type="text" name="add" id="add" class="input-xxlarge" /></span>
</p><p>
<label>Gender</label>
<span class="field">
<input type="radio" name="gen" value="Male" checked />Male
<input type="radio" name="gen" value="Female" />Female</span>
</p><p>
<label>Contact No.</label>
<span class="field"><input type="text" name="c_no" id="c_no" class="input-xxlarge" /></span>
</p><p>
<label>Date Of Birth</label>
<span class="field"><input type="text" name="dob" id="dob" class="input-xxlarge" /></span>
</p><p>
<label>Email ID</label>
<span class="field"><input type="text" name="email" id="email" class="input-xxlarge" /></span>
</p><p>
<label>Qualification</label>
<span class="field"><input type="text" name="qual" id="qual" class="input-xxlarge" /></span>
</p>
<p class="stdformbutton">
<button class="btn btn-primary">Submit Button</button>
<button type="reset" class="btn">Reset Button</button>
</p>
</form>
Controller.java
@RequestMapping(value="/in_emp" ,method = RequestMethod.POST)
@ResponseBody
public String insertEmp(@RequestParam("pname") String nm,Object command ,HttpServletRequest req, HttpServletResponse res)throws IOException
{
String path = req.getRealPath("/images/uploadimg");
path = path.substring(0, path.indexOf("\\build"));
path = path + "\\web\\images\\uploadimg\\";
DiskFileItemFactory d = new DiskFileItemFactory();
ServletFileUpload uploader = new ServletFileUpload(d);
try {
List<FileItem> lst = uploader.parseRequest(req);
for (FileItem fileItem : lst) {
if(fileItem.isFormField()==false){
fileItem.write(new File(path+"/"+fileItem.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
res.setContentType("text/html");
String abc="insert into `tbl_registration` (`person_name` ,`address` ,`gender` ,`contact_no` ,`dob` ,`email_id` ,`qualification` ,`designation_id`,`person_img`)VALUES ('"+nm+"','"+req.getParameter("add")+"','"+req.getParameter("gen")+"','"+req.getParameter("c_no")+"','2016-01-02','"+req.getParameter("email")+"','"+req.getParameter("qual")+"','"+req.getParameter("des")+"','"+req.getParameter("filenm")+"')" ;
ServletContext sc=req.getSession().getServletContext();
try{
SqlServices ss=new SqlServices(sc.getRealPath("WEB-INF/applicationContext.xml"));
if(req.getParameter("gid")==null) {
ss.persist(abc);
String qry1="select reg_id from tbl_registration where person_name='"+nm+"' and contact_no='"+req.getParameter("c_no")+"'";
List ls=ss.getlists(qry1);
HashMap hm;
if (ls != null) {
for (int i = 0; i < ls.size(); i++) {
hm = (HashMap) ls.get(i);
String inqry="insert into `tbl_login`(username,password,reg_id,user_type) values('"+req.getParameter("uname") +"','"+req.getParameter("pass")+"','"+hm.get("reg_id")+"',2)";
ss.persist(inqry);
}
}
}
}
catch(Exception e){
}
return "tbl_employee";
}
dispacher-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="WebController"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>