ajaxfileupload.js 添 spring mvc 注解 文件上传

ajaxfileupload.js 加 spring mvc 注解 文件上传
	private void writeResponse(String msg, HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.flush();
		out.close();
	}

 导入JS:<script src="<%=request.getContextPath()%>/common/js/utils/ajaxfileupload.js" type="text/javascript"></script>

				    <input id="myfiles" name="myfiles" type="file" />
				    <a class="blue-btn-coom-2 tcenter smibat" id="addBtn">确认</a>

 

$(function(){
	
	$("#addBtn").click(function(){
		addimg();
	});
});

function addimg(){
	//openBlock("","");
	 jQuery.blockUI();
    $.ajaxFileUpload
    (
       {
        url: $.tsp.contextPath + "/account/upload.do", //用于文件上传的服务器端请求地址
        secureuri: false, //是否需要安全协议,一般设置为false
        fileElementId: "myfiles", //文件上传域的ID  多文件上传  id为数组
        dataType: "json", //返回值类型 一般设置为json
        success: function (data)  //服务器成功响应处理函数
        {	
        	//alert(data.c);
        	 jQuery.unblockUI();  
        },
        error: function (data, e)//服务器响应失败处理函数
        {
        	 return alertMessage(e+"失败", "default"), !1;
        }
    }
)
}

 

    /**
     *练习图片上传 
     * @param file
     * @param request
     * @param model
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/upload")  
    public void upload(@RequestParam(value = "myfiles", required = false) MultipartFile myfiles, HttpServletRequest request,HttpServletResponse response) throws Exception {  
  
    	JSONObject oj = new JSONObject();
    	try {
            String path = request.getSession().getServletContext().getRealPath("upload");  
            String fileName = myfiles.getOriginalFilename();  
          //取得根目录路径  
            String rootPath=getClass().getResource("/").getFile().toString();
            System.out.println("root"+rootPath);
            File targetFile = new File(path, fileName);  
            String allPath = path+fileName;
            
            if(!targetFile.exists()){  
                targetFile.mkdirs();  
            }  
      
            try {  
            	myfiles.transferTo(targetFile);
                
                if(targetFile.exists()){
                	//将文件存到数rnd
                	System.out.println("All_Path"+path+fileName);
                }
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            oj.put("c", allPath);
            response.setContentType("text/html");
          //  response.getWriter().write(oj.toString());
            
            writeResponse(oj.toString(), response);
		} catch (Exception e) {
			e.printStackTrace();

		}finally {
			writeResponse(oj.toString(), response);
		}
        
    }
    
    
	private void writeResponse(String msg, HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.flush();
		out.close();
	}