Lsetea
2016-08-01 07:25
采纳率: 100%
浏览 3.7k
已采纳

spring mvc图片上传,怎么在Controller中接收?不用上传的,只用接收?

图片说明
就在红色框框里接收,或者在这个方法里接收,就好了,后面有个方法给接口就行了,求大神!在线等
大神写好的接口,我只用上传就好了
配置如下
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">

<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.wadata.hspjkpt.v1.xt.web.controller"/>
<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="com.wadata.hspjkpt.v1.interceptor.CommonHandlerMethodArgumentResolver" />
    </mvc:argument-resolvers>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>application/javascript;charset=UTF-8</value>
                    <value>text/css;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <value>text/plain;charset=UTF-8</value>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>


<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
<mvc:default-servlet-handler/>

<!-- 定义无需Controller的url<->view直接映射 -->
<mvc:view-controller path="/xzzx*" view-name="redirect:/" />

UTF-81073741824

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • GreenRookie 2016-08-01 07:36
    已采纳

    在springmvc.xml文件中配置一个CommonsMultipartResolver 的bean,然后前台上传时候命名name=“file”,在 Controller方法
    public void uploadFile(@RequestParam("file") CommonsMultipartFile files){
    for(CommonsMultipartFile file:files){
    if(!file.isEmpty){
    //在此操作上传
    }else{
    System.out.println("文件上传失败,请确定文件是否正确");
    }
    }

    }

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • onetwonumberlzw 2016-08-01 08:24

    public String save(@RequestParam("file") MultipartFile path,HttpServlet request,@ModelAttribute Pic pic,Model model){
    String serverPath = request.getSession().getServletContext().getRealPath(FILE_PATH);
    if(!path.isEmpty()){
    String picName = "filename";
    File target = new File(serverPath,picName);
    if(!target.exists){
    target.mkdirs();
    }
    try{
    //保存文件
    path.transferTo(target);
    }catcg(Exception e){
    return "fail";
    }

                return "success";
        }
    

    }

    评论
    解决 无用
    打赏 举报
  • RelievedAbility 2016-08-01 08:30
    配置文件中加上如下
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>1073741824</value>
        </property>
    </bean>
    后端代码这样拿,memberFile为file的name
        @RequestMapping("/batchInsertMember")
    @ResponseBody
    public String tobatchInsertMember(HttpServletRequest request,String memberCode)  {
        String operateName = getUserInfo(request);
        logger.info(operateName+"批量导入会员start");
        String fileName = "";
        MultipartFile file = null;
        MultipartHttpServletRequest multipartRequest = null;
        multipartRequest = (MultipartHttpServletRequest) request;
    
        file = multipartRequest.getFile("memberFile");
                }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题