springmvc+ajaxfileupload异步上传

64 篇文章 0 订阅
希望对大家有帮助  不能直接用的  根据需求修改吧  

spring 的配置文件需要设置:
 <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为1MB -->
        <property name="maxUploadSize">
            <value>10485760</value>
        </property>
    </bean>

------------必备JS
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/ajaxfileupload.js"></script>


------------html
<form action="#" name="myform" method="post">
<input type='file'  id="uploada" name="uploada" size='50'/>
<input type="button" style="width: 5em"  οnclick="uploadImages('uploada')" value='上传'/>
<input type='file'  id="uploadb" name="uploadb" size='50'/>
<input type="button" style="width: 5em"  οnclick="uploadImages('uploadb')" value='上传'/>
<input type='file'  id="uploadc" name="uploadc" size='50'/>
<input type="button" style="width: 5em"  οnclick="uploadImages('uploadc')" value='上传'/>
</form>


-----------AJAX
  function uploadImages(type) {
$.ajaxFileUpload({
        //跟具updateP得到不同的上传文本的ID
                 url:'${ctx}/activity/uploadImg.do?updateP='+type,             //需要链接到服务器地址
                 secureuri:false,
                 fileElementId:''+type+'',                         //文件选择框的id属性(必须)
                 dataType: 'text',  
                 success: function (data, status){  
                 var data = eval("("+data+")");
                    if(data.updateP=='uploada'){
                        document.getElementById("显示文件访问路径文本的IDA").value=baseurl+"/upload/"+data.fileName;
                    }else if(data.updateP=='uploadb'){
                        document.getElementById("显示文件访问路径文本的IDB").value=baseurl+"/upload/"+data.fileName;
                    }else if(data.updateP=='uploadc'){
                        document.getElementById("显示文件访问路径文本的IDC").value=baseurl+"/upload/"+data.fileName;
                    }
            alert('上传失败');                
                 },
                 error: function (data, status, e){  
                     alert('上传失败');
                 }
               }
            );

}

--------------spring
@RequestMapping("/activity/uploadImg.do")
    public void uploadImg(MultipartHttpServletRequest request,MobileActivityJsp mobileActivityJsp,HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        SimpleDateFormat simpleFormat = new SimpleDateFormat("MMddHHmmsss");
        String generationfileName = simpleFormat.format(new Date())+new Random().nextInt(1000);
        //保存路径
        String savePath = "D:/mobilePermissions/ddmap-mobile-medit/web/upload";
        String updateP = request.getParameter("updateP");
        String fileNameSuffix=null;
        String fileName= null;
        if(null!=updateP&&!"".equals(updateP)){
            try {
                MultipartFile mf = request.getFile(updateP);
                fileName=mf.getOriginalFilename();
                if(null!=mf&&!"".equals(mf)){
                    fileNameSuffix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
                    SaveFileFromInputStream(mf.getInputStream(),savePath,generationfileName+"."+fileNameSuffix);
                    out.write("{'state':'0','fileName':'"+generationfileName+"."+fileNameSuffix+"','updateP':'"+updateP+"'}");
                }
            } catch (Exception e) {
                out.write("{'state':'1'}");
                e.printStackTrace();
            }
        }    
    }
//保存文件   1,文件   2,保存路径 3,文件名称
    public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException{      
            FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
            byte[] buffer =new byte[1024*1024];
            int bytesum = 0;
            int byteread = 0;
            while ((byteread=stream.read(buffer))!=-1){
               bytesum+=byteread;
               fs.write(buffer,0,byteread);
               fs.flush();
            }
            fs.close();
            stream.close();      
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值