返回JSON数据,在IE下提示下载文件解决方法整理

第一种:

jsp页面中

dataType的属性设置为:'html/text',

action中如下

  1.     @ResponseBody  
  2.     @RequestMapping(value = "/upload")  
  3.     public String upload(  
  4.             @RequestParam(value = "image223", required = false) MultipartFile file,  
  5.             HttpServletRequest request, HttpServletResponse response)  
  6.             throws IOException {  
  7.         String content = null;  
  8.         Map map = new HashMap();  
  9.         if (ValueWidget.isNullOrEmpty(file)) {  
  10.             map.put("error""not specify file!!!");  
  11.         } else {   
  12.             String fileName = file.getOriginalFilename();// 上传的文件名  
  13.             fileName=fileName.replaceAll("[\\s]",   "");//IE中识别不了有空格的json  
  14.             // 保存到哪儿  
  15.             String finalFileName = TimeHWUtil.formatDateByPattern(TimeHWUtil  
  16.                     .getCurrentTimestamp(),"yyyyMMddHHmmss")+ "_"  
  17.                             + new Random().nextInt(1000) + fileName;  
  18.             File savedFile = getUploadedFilePath(request,  
  19.                     Constant2.UPLOAD_FOLDER_NAME + "/image", finalFileName,  
  20.                     Constant2.SRC_MAIN_WEBAPP);

  21.             // 保存  
  22.             try {  
  23.                 file.transferTo(savedFile);  
  24.             } catch (Exception e) {  
  25.                 e.printStackTrace();  
  26.             }  
  27.           
  28.             ObjectMapper mapper = new ObjectMapper();  
  29.             map.put("fileName""a");  
  30.             map.put("path", savedFile.getAbsolutePath());  
  31.             try {  
  32.                 content = mapper.writeValueAsString(map);  
  33.                 System.out.println(content);  
  34.             } catch (JsonGenerationException e) {  
  35.                 e.printStackTrace();  
  36.             } catch (JsonMappingException e) {  
  37.                 e.printStackTrace();  
  38.             } catch (IOException e) {  
  39.                 e.printStackTrace();  
  40.             }  
  41.             System.out.println("map:"+map);  
  42.         }  
  43.         return content;  
  44.   
  45.     } 

第二种:

action中如下

在服务端设置res.setContentType("text/html"),该方法是把返回的字符串形式的数据进行重新定义

代码--->

@RequestMapping(value="/uploadExcel")
@ResponseBody
public String upload(Model model,@RequestParam("file") MultipartFile file,HttpServletRequest req,HttpServletResponse res)
throws IOException {
//文件上传并解析
Map map =  new HashMap();
String msg =null;
String rows=null;
String content = null;
try {
Params params = getParams();
bankStatementService.importExcel(file,req,model,params);
model.addAttribute("params",params);
msg=(String) req.getAttribute("msg");
rows = (String) req.getAttribute("rows");
}catch (Exception e){
msg=("数据导入失败!请您重新上传!");
e.printStackTrace();
}
map.put("msg", msg);
map.put("rows",rows);
//JSONObject json=new JSONObject(map);
try { 
ObjectMapper mapper = new ObjectMapper(); 
content = mapper.writeValueAsString(map);
} catch (JsonGenerationException e) {  
            e.printStackTrace();  
        } catch (JsonMappingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
//res.getWriter().write(content); 不用@ResponseBody注解可以返回信息
res.setContentType("text/html");
return content;

}

jsp页面如下

//上传
function upload(){
debugger;
$.ajaxFileUpload
       (
           {
               url: '${ctx}/upload/uploadExcel', //用于文件上传的服务器端请求地址
               secureuri: false, //一般设置为false
               fileElementId: 'file', //文件上传空间的id属性 
               dataType: 'html/text', //返回值类型 一般设置为json
               success: function (data)  //服务器成功响应处理函数
               {
                //alert("进入success方法!!!!")
                debugger;
                alert(data);
                var json = JSON.parse(data);//把string类型的data数据转为json类型
                alert(json.msg);
if (json.msg == 'success') {
art.dialog.alert("数据导入成功!" + json.rows);
} else {
art.dialog.alert(json.msg);
}
               },
               
           }
       )
       return false;

第三种:

不用@RsponseBody注解

②使用HttpServletResponse response进行数据返回

  1. response.setContentType("text/html")  
  2. response.getWriter().writer("{msg:''}"); 

你可能感兴趣的:(返回JSON数据,在IE下提示下载文件解决方法整理)