@Component
public class TextFileDownloadView extends AbstractFileDownloadView {
@Override
protected InputStream getInputStream(Map<String, Object> model,
HttpServletRequest request) throws IOException {
Resource resource = new ClassPathResource("abc.txt");
return resource.getInputStream();
}
@Override
protected void addResponseHeader(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Content-Disposition", "attachment; filename=abc.txt");
response.setContentType("text/plain");
}
}
@RequestMapping(value = "/downloadTxt", method = RequestMethod.GET)
public String downloadTxt1() {
return "textFileDownloadView";
}
Originate from http://rensanning.iteye.com/blog/2356942
package inputstream; import java.io.IOException; import java.io.InputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import inputstream.domain.Customer; @RestController public class InputStreamController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping(value = "" , method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE ) public InputStream testClassPath() throws IOException { Resource resource = new ClassPathResource("test.txt"); String fileName = resource.getFilename(); logger.info(fileName); // resource.getFile(); //获取资源对应的文件 // resource.getURL(); //获取资源对应的URL //每次都会打开一个新的流 InputStream is = resource.getInputStream(); //this.printContent(is); logger.info(is.toString()); return is; } @RequestMapping(value = "/cs" , method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE ) public Customer testCustomer() { Customer c = new Customer(5,"Tom"); return c; } }
http://www.cnblogs.com/wangtj-19/p/5889056.html