form表单包含图片上传 时 控制层无法获取上传的文件 100分答谢

m0_37659175 2017-08-20 11:05:48
form表单部分:
<form action="user/save" method="post" enctype="multipart/form-data">
<label><span>用户名:</span><input type="text" name="username"></label><br>
<label><span>密码:</span><input type="password" name="password"></label><br>
<label><span>上传图片:</span><input type="file" ></label><br>
<label><input type="submit"></label>
</form>


控制层代码:

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.hd.model.User;
import com.hd.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserControl {
@Autowired
private IUserService iu;
@RequestMapping("/save")
//@RequestParam(value="file", required = false) MultipartFile file,
public String save(User user,@RequestParam(value="file", required = false) MultipartFile file){
String newname=file.getOriginalFilename();
if(newname!=null&&newname.length()>0){
user.setPpath(newname);
}
iu.save(user);
if(file.isEmpty()){
System.out.println("附件为空");
}else{
System.out.println(file);
}
return "/home.jsp";

}
}

spring-mvc xml配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
</bean>


jar包:


...全文
195 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
李德胜1995 2017-08-20
  • 打赏
  • 举报
回复
<input type="file" >name属性都没有????

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧