spring mvc 在做图片上传时,我接收的参数为null

迷雾骑士 2016-07-11 11:41:23
spring mvc + spring 在做图片上传时,我接收的参数为null,这是什么原因?


@ResponseBody
@RequestMapping("/publishDemand")
public HashMap<String, Object> publish(HttpServletRequest request) {


HashMap<String, Object> dataMap = new HashMap<String, Object>();
try {
String userId = request.getParameter("user_id") == null ? "" : request.getParameter("user_id").trim();
String userName = request.getParameter("user_name") == null ? "" : request.getParameter("user_name").trim();
String budget = request.getParameter("budget") == null ? "" : request.getParameter("budget").trim();
String img1= request.getParameter("img1") == null ? "" : request.getParameter("img1").trim();
String img2= request.getParameter("img2") == null ? "" : request.getParameter("img2").trim();
String img3= request.getParameter("img3") == null ? "" : request.getParameter("img3").trim();
String img4= request.getParameter("img4") == null ? "" : request.getParameter("img4").trim();
String img5= request.getParameter("img5") == null ? "" : request.getParameter("img5").trim();

logger.info("user_id=" + userId);
logger.info("user_name=" + userName);
logger.info("budget=" + budget);
logger.info("img1=" + img1);
logger.info("img2=" + img2);
logger.info("img3=" + img3);
logger.info("img4=" + img4);
logger.info("img5=" + img5);


以上是我的controller方法,一开始我以为是上传的文件太大,超出了服务器的限制,于是修改了服务器(tomcat 8.0)的配置为 maxPostSize=-1 ,但是有时候还是会出现我logger打印出来的的参数都是空的情况,请问这是什么原因呢?
...全文
781 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
微wx笑 2017-05-17
  • 打赏
  • 举报
回复
参考:http://blog.csdn.net/qq_22956867/article/details/51437905
毛宁 2016-11-07
  • 打赏
  • 举报
回复
@RequestMapping("/publishDemand") public HashMap<String, Object> publish(@RequestParam(value="img1", required = false) MultipartFile img1,@RequestParam(value="userId ", required = false) String userId , HttpServletRequest request) { HashMap<String, Object> dataMap = new HashMap<String, Object>(); try { String userId = request.getParameter("user_id") == null ? "" : request.getParameter("user_id").trim(); String img1= request.getParameter("img1") == null ? "" : request.getParameter("img1").trim(); logger.info("user_id=" + userId); logger.info("img1=" + img1); } catch (){ } 或者使用Spring提供的MultipartHttpServletRequest mulitpartRequest = (MultipartHttpServletRequest) requet;把原来的Request装饰成新的request对象 然后 mulitpartRequest .getFileMap(); mulitpartRequest .getParameterMap();就可以得到你想要的所有参数!
迷雾骑士 2016-08-15
  • 打赏
  • 举报
回复
引用 11 楼 z421656773 的回复:
页面: <form name="uploadImgForm" id="uploadImgForm" method="post" enctype="multipart/form-data" > <input type="file" id="up" name="file"> </form>
嗯 这个方法有时间去试试,最终是用修改post限制的方法解决的
迷雾骑士 2016-08-15
  • 打赏
  • 举报
回复
引用 9 楼 lingerfeifei 的回复:
请问下解决了吗,我现在遇到同样的问题,解决了麻烦告知,谢谢
解决了,采用修改tomcat post限制的方式 解决办法:在connector中加上maxPostSize="-1" 就行了
迷雾骑士 2016-08-15
  • 打赏
  • 举报
回复
引用 7 楼 wfcdream 的回复:
你这种做法 我也做过,如果改了tomcat 的post限制 应该不存在这个问题,首先你需要查一下前端的传值,浏览器F12看看是否有post数据,然后在看看前后太名字是否一致,如果实在不行,你可以参考我的方式,我是直接在函数参数里接收的:参数里加String img1-img5
嗯,我最后采用了修改post限制的方式
Red2us 2016-07-29
  • 打赏
  • 举报
回复
页面: <form name="uploadImgForm" id="uploadImgForm" method="post" enctype="multipart/form-data" > <input type="file" id="up" name="file"> </form>
Red2us 2016-07-29
  • 打赏
  • 举报
回复
public void save( HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file) { ......} 图片上传可以使用MultipartFile ,LZ可以试一下,很方便
脚踝程序员 2016-07-28
  • 打赏
  • 举报
回复
请问下解决了吗,我现在遇到同样的问题,解决了麻烦告知,谢谢
Intboy 2016-07-26
  • 打赏
  • 举报
回复
前台页面看报文,看传参数没
wfcdream 2016-07-26
  • 打赏
  • 举报
回复
你这种做法 我也做过,如果改了tomcat 的post限制 应该不存在这个问题,首先你需要查一下前端的传值,浏览器F12看看是否有post数据,然后在看看前后太名字是否一致,如果实在不行,你可以参考我的方式,我是直接在函数参数里接收的:参数里加String img1-img5
哈喽门 2016-07-25
  • 打赏
  • 举报
回复
文件跟文本是有区别的,方法接收的方式也不同,有文件就没文本,有文本就没文件,血海深仇的说,当然现在好像有挺多控件能同时传输的,你现在是用接收文本的方式在接收文件肯定不行,得改成接收文件流的方式。 spring可以这样写:public xxx files( @RequestParam(value = "file") MultipartFile imageFile) throws Exception {} 当然你也可以在页面直接把文件转成base64再用你那种文本接收方式来接收之后再转成图片
迷雾骑士 2016-07-25
  • 打赏
  • 举报
回复
引用 4 楼 u010087908 的回复:
spring 上传文件 有成型的方法。网上例子很多。 另外,你应该用post,而不是get。
是用的post传的,我只是用的request来接收而已
君奉天丶 2016-07-11
  • 打赏
  • 举报
回复
那就检查你的参数名是否对应上了,还有你前台压缩是否正确,查看http请求看是否有值传到后台。
迷雾骑士 2016-07-11
  • 打赏
  • 举报
回复
引用 1 楼 qpwoal1379 的回复:
你这图片是传的什么?文件流?
是用base64将图片编码后的字符串,然后用post方法传过来的
君奉天丶 2016-07-11
  • 打赏
  • 举报
回复
你这图片是传的什么?文件流?
NANU-NANA 2016-07-11
  • 打赏
  • 举报
回复
spring 上传文件 有成型的方法。网上例子很多。 另外,你应该用post,而不是get。

81,122

社区成员

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

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