码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc下上传文件

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

标签:style   blog   io   ar   color   os   使用   sp   for   

使用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下上传文件

标签:style   blog   io   ar   color   os   使用   sp   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!