daba245 2017-12-15 09:29 采纳率: 100%
浏览 2756
已结题

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

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 2017-12-15 10:52
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?