spring mvc return value type setting

I encountered a problem with uploading files today. Under IE7 and 8, the download box will pop up after the upload is complete.

The reason found is that the return value type is json, the content-type of the return header is: application/json, and the returned information header needs to be changed to text/html.

Another problem is that when the return type is text/plain, the returned value will be wrapped in <pre></pre> tags, affecting front-end parsing.

method one:
@RequestMapping(value = "/excelUploadtest", method = RequestMethod.POST)
    // @ResponseBody
    public void excelUploadtest(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName,
            HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("{status:1,msg:'" + "Congratulations, the upload is successful!" + "'}");
        return;
    }

Method Two:
@RequestMapping(value = "add.do")
    public ResponseEntity<?> add(HttpServletRequest request) {
    
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = new MediaType("text", "html", Charset.forName("utf-8"));
        headers.setContentType(mediaType);
        return new ResponseEntity<Map<String, Object>>("msg", headers, HttpStatus.OK);
    }
    

@RequestMapping(value = "/excelUpload", method = RequestMethod.POST)
    public ResponseEntity<JsonMessage> excelUpload(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName,
            HttpServletResponse response) throws IOException {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_HTML);
        ResponseEntity<JsonMessage> responseEntity = new ResponseEntity<JsonMessage>(new JsonMessage("无法读取上传的Excel文件,请重试。"),headers,HttpStatus.OK);
        Workbook wb = null;
        if (fileName.endsWith(".xls")) {
            try {
                wb = new HSSFWorkbook(file.getInputStream());
            } catch (Exception e) {
                return responseEntity;
            }
        } else if (fileName.endsWith(".xlsx")) {
            try {
                wb = new XSSFWorkbook(file.getInputStream());
            } catch (Exception e) {
                return responseEntity;
            }
        } else {
            return responseEntity;
        }

        // 该工具类仅仅负责解析、数据绑定(含类型转换)

        // TODO 业务值判断(存在性判断,唯一性判断等)

        // 返回错误画面
        Integer errorCount = errorResultList.size();
        if (errorCount > 0) {
            JsonMessage jsonMessage = new JsonMessage();
            jsonMessage.setResult(errorResultList);
            jsonMessage.setErrorMsg("您有<em class='red'>" + errorCount + "</em>条错误信息,上传失败!请修改模板后继续上传");
            jsonMessage.setStatus(JsonMessage.STATUS_FAIL);
            return new ResponseEntity<JsonMessage>(jsonMessage, headers, HttpStatus.OK);
        }

        List<Medicine> medicines = BeanMapper.mapList(medicineVOs, Medicine.class);
        medicineService.batchInsertMedicines(medicines);

        JsonMessage jsonMessage = new JsonMessage();
        jsonMessage.setMsg("恭喜您,上传成功!");
        jsonMessage.setStatus(JsonMessage.STATUS_SUCCESS);
        return new ResponseEntity<JsonMessage>(jsonMessage, headers, HttpStatus.OK);
    }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325544689&siteId=291194637
Recommended