qinjianhuang 2017-03-31 03:18 采纳率: 0%
浏览 1806

SpringMVC配置文件中要实现文件上传和 json字符转换,需要导入什么Jar包?急!

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
    <context:component-scan base-package="com.taotao.controller" />
    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->

    <!-- <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean> -->

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean> -->

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp" />

    <!-- <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value>上传文件大小限制为31M,31*1024*1024
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean> -->

</beans>
  • 写回答

3条回答

  • 75闪光雷 2017-03-31 03:30
    关注

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;

    /**

    • Descriptions *
    • @version 2017年3月29日
    • @author SUNTEC
    • @since JDK1.6
      *
      /
      @Controller
      public class getBooks {
      // 文件上传
      @RequestMapping(value = "/upload", headers = "content-type=multipart/
      ", method = RequestMethod.POST)
      public String upload(@RequestParam(value = "file", required = false) MultipartFile file,
      MultipartHttpServletRequest request, ModelMap model) {

          System.out.println("开始");
          String path = request.getSession().getServletContext().getRealPath("upload");
          String fileName = file.getOriginalFilename();
          // String fileName = new Date().getTime()+".jpg";
          System.out.println(path);
          File targetFile = new File(path, fileName);
          if (!targetFile.exists()) {
              targetFile.mkdirs();
          }
      
          // 保存
          try {
              file.transferTo(targetFile);
          }
          catch (Exception e) {
              e.printStackTrace();
          }
          model.addAttribute("fileUrl", request.getContextPath() + "/upload/" + fileName);
      
          return "bookstore";
      }
      

    }

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统