zoukankan      html  css  js  c++  java
  • springmvc04-文件上传-JSON数据

    文件上传部分:

    1, 导入commons-fileupload-1.2.2.jar commons-io-2.4.jar 两个jar包.

    2, 在主配置文件中,添加如下信息

        <!-- 文件上传-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 配置上传的最大字节数 -->
            <property name="maxUploadSize" value="10000000"/>
            <property name="defaultEncoding" value="UTF-8"/>
            <property name="maxInMemorySize" value="102400"/> 
            <!-- 貌似上传到的路径无法配置-->
        </bean>

    3, 在上传文件的表单中一定要设置 enctype="multipart/form-data"

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    
    <%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>上传文件演示</title>
    </head>
    <body>
     
     <sf:form method="post" action="upload" enctype="multipart/form-data">
         <input type="file" name="attach" />
         
         <!--  多个文件上传时,这样使用即可
             <input type="file" name="attachs" />
             <input type="file" name="attachs" />
             <input type="file" name="attachs" />
          -->
         <input type="submit" value="提交" />
     </sf:form>
    </body>
    </html>

      后台中的action的处理

      /**
         * 如果上传多个文件,使用 @RequestParam MultipartFile[] attachs,然后遍历,一个个上传即可
         * 单个文件使用 MultipartFile attach,不需要那个注解
         */
        public String upload( MultipartFile attach,HttpServletRequest request) throws IOException{
            
            //getName()得到的是属性名
            //getOriginalFilename() 得到的是上传过来的文件名
            System.out.println(attach.getName()+","+attach.getOriginalFilename()+","+attach.getContentType());
            
            String realPath=request.getSession().getServletContext().getRealPath("/resources/upload");
            //默认上传到了--> D:workspace-esb.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsspringmvc
    esourcesupload
            
            File file = new File(realPath+"/"+attach.getOriginalFilename());
            
            FileUtils.copyInputStreamToFile(attach.getInputStream(), file);
            
            
            return "redirect:/user/users";  //这个返回的视图是随便写的.
        }
        

     返回json数据部分:

      

    /**
         * @PathVariable:路径中的变量
         *
         */
        @RequestMapping(value="/{name}",method=RequestMethod.GET)
        public String show(@PathVariable String name,Model model){
            
            model.addAttribute(users.get(name));
            
            return "/user/show"; 
        }
        
        /**
         * 返回Json类型数据:
         * 1, 导入jackson-all-1.9.11.jar 
         * 2, 直接返回对象即可
         * 3, 使用注解@ResponseBody
         * 3,为了区别上面的show()方法的requestMapping,加入params属性
         */
        @RequestMapping(value="/{name}",method=RequestMethod.GET,params="json")
        @ResponseBody
        public User show(@PathVariable String name){
            
            return users.get(name);
            
        }
        

      第一个show()方法是普通的返回jsp页面的视图,第二个show()方法就是返回json格式的数据

    ----------- 赠人玫瑰,手有余香     如果本文对您有所帮助,动动手指扫一扫哟   么么哒 -----------


    未经作者 https://www.cnblogs.com/xin1006/ 梦相随1006 同意,不得擅自转载本文,否则后果自负
  • 相关阅读:
    企业微信应用授权
    exec存储过程示例
    jquery判断对象是否存在
    IScroll5要防止重复加载
    transitionEnd不起作用解决方法
    微信接口 output {"errMsg":"translateVoice:fail, the permission value is offline verifying"}
    javascript保留两位小数
    html取消回车刷新提交
    企业微信后台登录
    企业微信开启开发者工具
  • 原文地址:https://www.cnblogs.com/xin1006/p/3458245.html
Copyright © 2011-2022 走看看