Spring mvc 如何使用spring3.0从@ResponseBy注释以.txt文件的形式做出响应

Spring mvc 如何使用spring3.0从@ResponseBy注释以.txt文件的形式做出响应,spring-mvc,Spring Mvc,!![在此处输入图像描述][1] 有人能解释一下如何使用Spring3.x实现这个输出吗 当我点击一个按钮时,我想将数据提交到服务器,并使用@ResponseBody注释从spring controller 3.x获取text.txt文件的响应…这看起来非常类似于: 和 您应该能够通过@RequestMapping设置响应的内容类型标题: @RequestMapping(..., produces = org.springframework.http.MediaType.TEXT_PLAIN)

!![在此处输入图像描述][1]

有人能解释一下如何使用Spring3.x实现这个输出吗
当我点击一个按钮时,我想将数据提交到服务器,并使用@ResponseBody注释从spring controller 3.x获取text.txt文件的响应…

这看起来非常类似于: 和

您应该能够通过@RequestMapping设置响应的内容类型标题:

@RequestMapping(..., produces = org.springframework.http.MediaType.TEXT_PLAIN)
这要求客户端传入内容类型标头以匹配

或者直接在服务方法中的响应对象上设置头,如:


或者通过向spring上下文添加新的消息转换器,如中所述:

这看起来非常类似于: 和

您应该能够通过@RequestMapping设置响应的内容类型标题:

@RequestMapping(..., produces = org.springframework.http.MediaType.TEXT_PLAIN)
这要求客户端传入内容类型标头以匹配

或者直接在服务方法中的响应对象上设置头,如:

或者通过向spring上下文添加一个新的消息转换器,如中所述:

尝试这样编写@ResponseBy方法

@ResponseBody
@RequestMapping(value ="/txt" )
public String txtResponse(HttpServletResponse response){
    String fileName = "a.txt";
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    String content = "This is txt content";
    return content;
}
试着像这样编写@ResponseBody方法

@ResponseBody
@RequestMapping(value ="/txt" )
public String txtResponse(HttpServletResponse response){
    String fileName = "a.txt";
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    String content = "This is txt content";
    return content;
}

似乎有更好的解决办法

public ResponseEntity<Resource> exportTemplate() throws IOException {
    return ResponseEntity.ok().header("Content-Disposition", "inline;filename=" + "filename.txt")
            .body(new ByteArrayResource(new pojoObject()));
}

似乎有更好的解决办法

public ResponseEntity<Resource> exportTemplate() throws IOException {
    return ResponseEntity.ok().header("Content-Disposition", "inline;filename=" + "filename.txt")
            .body(new ByteArrayResource(new pojoObject()));
}

如果有,请发布您迄今为止尝试过的代码。该图像到哪里去了?如果有,请发布您迄今为止尝试过的代码。该图像到哪里去了?在我的@RequestMapping中,它显示没有可用的产品选项。不管怎样,我得到了我的答案,感谢您的回复……它也适用于其他工作……在我的@RequestMapping中,它显示没有产品选项可用。无论怎样,我都能得到我的答案。谢谢你的回复。这对其他工作也很有用。。。。