daba245
daba245
采纳率100%
2017-12-15 09:29 阅读 2.5k
已结题

SSH使用Spring中MultipartHttpServletRequest实现文件上传

20

HttpServletRequest request = ServletActionContext.getRequest();
String UserStr = HttpServletRequestUtil.getString(request,"Userstr");
System.out.println("UserStr:"+UserStr);
MultipartHttpServletRequest multipartRequest = null;
MultipartFile profileImg = null;
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
System.out.println("multipartResolver.isMultipart(request):"+multipartResolver.isMultipart(request));
if (multipartResolver.isMultipart(request)) {
multipartRequest = multipartResolver.resolveMultipart(request);
profileImg = (MultipartFile) multipartRequest.getFile("fileupload");
}

HttpServletRequest request = ServletActionContext.getRequest();
String UserStr = HttpServletRequestUtil.getString(request,"Userstr");
System.out.println("UserStr:"+UserStr);
MultipartHttpServletRequest multipartRequest = null;
MultipartFile profileImg = null;
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
System.out.println("multipartResolver.isMultipart(request):"+multipartResolver.
问题:
multipartRequest.getFile("fileupload")为null
有时form表单中的Userstr也为null

  • 点赞
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    m0_38108896 m0_38108896 2017-12-15 10:52

    实现文件上传,需要3个条件,form表单中要post请求, enctype="multipart/form-data",input中type="file"

    你的可以上传1M以下的,这个应该不对吧 默认是能上传2M以下的,而且你也可以修改这个默认文件的大小

    点赞 1 评论 复制链接分享
  • Mr_Huang_ning Mr_Huang_ning 2017-12-15 09:44

    文件上传from表单必须在from里面设置enctype="multipart/form-data“,楼主看下你是不是少写了

    点赞 3 评论 复制链接分享
  • dcxy0 Q544471255 2017-12-16 01:11

    以前做过这个,可以参考我记录的:https://www.suancaiyu.xyz/read/2463.html

    点赞 1 评论 复制链接分享
  • qq499526040 火腿霹雳 2018-01-07 12:29

    如果验证过楼上几位的情况还存在问题,楼主检查前端input标签name属性值与后端取文件的参数名是否一致

    点赞 1 评论 复制链接分享
  • morgerton 白萝卜blb 2017-12-15 09:35

    捏可以加上自己的对空处理的逻辑,代码并不存在绝对安全性,

    点赞 评论 复制链接分享

相关推荐