直接返回String发现页面还是会提示下载 res = "{\"success\": true,\"msg\": \"服务器还原成功!\"}"; return res; 后来修改为 response.setContentType("text/html;charset=utf-8"); response.getWriter().write("{\"success\": true,\"msg\": \"服务器还原成功!\"}"); return null; 解决问题。 贴下代码 js $("#uploadserver").form("submit",{ url:'system/uploadServerFile.do', type:'post', dataType:'text/html', onsubmit:function(){ return $(this).form("validate"); }, success:function(data){ $.messager.progress('close'); var data = $.parseJSON(data); if(data.success==true){ ZENG.msgbox.show(data.msg, 4, 3000); }else{ ZENG.msgbox.show(data.msg, 5, 3000); } document.upserverForm.severfiles.value=""; }, error:function(data){ $.messager.alert("提示信息",data); } }); 后台 @RequestMapping("/system/uploadServerFile.do") @ResponseBody public String uploadServerFile(@RequestParam MultipartFile[] severfiles,HttpServletRequest request,HttpServletResponse response) throws IOException { String content = null; Map<String,Object> map=new HashMap<String,Object>(); ObjectMapper mapper = new ObjectMapper(); Map m = System.getenv(); String path = (String) m.get("IIS_HOME"); String[] paths = path.split(","); String IIS_HOME = paths[paths.length-1]; try { //如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解 //如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解 //并且上传多个文件时,前台表单中的所有<input type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件 String realPath = configService.queryConfig("ftpMappingUrl"); File file = new File(realPath); deleteAll(file); for(MultipartFile myfile : severfiles){ //如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中 // String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload"); // String ftpMappingUrl=configService.queryConfig("ftpMappingUrl"); //这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉 FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename())); File f = new File(realPath+"/"+myfile.getOriginalFilename()); if(f.exists()){ try { ZipFile.unzip(realPath+"/"+myfile.getOriginalFilename(), realPath); //new ZipUtil().unZipFile(realPath+"/"+myfile.getOriginalFilename(), realPath) ; } catch (Exception e) { e.printStackTrace(); } f.delete(); } String sqlfilePath = realPath+File.separator+"iisbak.sql"; File sqlFile = new File(sqlfilePath); boolean b = true; b = JavaExportMysql.load(IIS_HOME,sqlfilePath); if(!b){ // map.put("success", false); // map.put("msg", "数据库还原失败!确认后重试!"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("{\"success\": false,\"msg\": \"数据库还原失败!确认后重试!\"}"); return null; }else{ sqlFile.delete(); // map.put("success", true); // map.put("msg", "服务器还原成功!"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("{\"success\": true,\"msg\": \"服务器还原成功!\"}"); return null; } } }catch (IOException e) { // map.put("success", false); // map.put("msg", "服务器还原失败!确认后重试!"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("{\"success\": false,\"msg\": \"服务器还原失败!确认后重试!\"}"); return null; } response.setContentType("text/html;charset=utf-8"); response.getWriter().write("{\"success\": false,\"msg\": \"服务器还原失败!确认后重试!\"}"); return null; } |
|