1

![enter image description here][1]

can any one please explain me how to achieve this output using spring 3.x when i clicked on a button i want to submit the data to the server and getting the response as text(.txt) file from the spring controller 3.x using the @ResponseBody annotation...

narendra
  • 85
  • 2
  • 7

3 Answers3

5

Try to write your @ResponseBody method like this

@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;
}
Larry.Z
  • 3,694
  • 1
  • 20
  • 17
1

This looks very similar to: Spring MVC 3 Return Content-Type: text/plain and Who sets response content-type in Spring MVC (@ResponseBody)

You should be able to set the Content-Type header of your response via @RequestMapping:

@RequestMapping(..., produces = org.springframework.http.MediaType.TEXT_PLAIN)

Which requires the client to pass in a Content-Type header to match.

Or by setting the header directly on your response object in your service method, like in: https://stackoverflow.com/a/5268157/1546662.

Or by adding a new message converter to the spring context, as noted in: https://stackoverflow.com/a/3617594/1546662

icyerasor
  • 4,973
  • 1
  • 43
  • 52
Tres' Bailey
  • 709
  • 7
  • 17
  • In my @RequestMapping it is showing no produces option available.Any way i got my answer thanks for the response....it is also usefull for another work.... – narendra Aug 16 '13 at 05:55
0

A better solution seems

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