• springmvc下上传文件


    使用ajax+表单+jQuery;

    function sendFile() {
        var action = "c/goFile.do";
        $("#form").ajaxSubmit( {
            url : action,
            success : function(data) {
                var htm = "<per>"+data
                "</per>"
                $("#content").text(data);
                console.log(data);
            }
        });
        return false;
    }
    <form id="form" enctype="multipart/form-data"
                method="post" onsubmit="return saveReport()">
                <input id="file" name="file" type="file" onchange="fileChange()"/>
            </form>
    @RequestMapping("/goFile")
        @ResponseBody
        public String goFile(@RequestParam MultipartFile file, Model model) throws Exception{
            String str = InputStreamTOString(file.getInputStream(), "utf-8");
            return str ;
        }
         public String InputStreamTOString(InputStream in,String encoding) throws Exception{
             int BUFFER_SIZE = 1024;
             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
             byte[] data = new byte[BUFFER_SIZE];
             int count = -1;
             while((count = in.read(data,0,BUFFER_SIZE)) != -1)
                 outStream.write(data, 0, count);
             data = null;
             return new String(outStream.toByteArray(),encoding);
         }

      

  • 相关阅读:
    JvisualVM、JMC监控远程服务器
    MVC学习笔记3
    MVC学习笔记2
    菜鸟级appium 必看
    关于redis一些问题记录
    git和github的区别
    VMware快照
    LR创建数据源读取excel
    mysql 5.7.18 源码安装笔记
    IDEA 配置Junit4
  • 原文地址:https://www.cnblogs.com/Silababy/p/4162161.html
Copyright © 2020-2023  润新知