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

 

  

posted @ 2014-12-14 01:58  Silababy  阅读(185)  评论(0编辑  收藏  举报