首页 > 编程语言 > 详细

springmvc下上传文件

时间:2014-12-14 07:00:15      阅读:195      评论:0      收藏:0      [点我收藏+]

使用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);
     }

 

  

springmvc下上传文件

原文:http://www.cnblogs.com/Silababy/p/4162161.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!