`

SpringMVC杂记(六) 下载文件

阅读更多
SpringMVC杂记(六) 下载文件
1) jar依赖
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.1</version>
</dependency>


2) 代码例子
@Controller
@RequestMapping(value = "/download")
public class DownloadController {

	@RequestMapping(value = "/test")
	public ResponseEntity<byte[]> test() throws IOException {
		HttpHeaders headers = new HttpHeaders();
		
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", "1.txt");
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("e://1.txt")), headers, HttpStatus.CREATED);
	}
}
分享到:
评论
5 楼 zliw0531hf 2013-03-05  
为什么我下载没有速度?啊啊啊啊
4 楼 Healthy183 2012-08-02  
yingzhor 写道
commons-io.2.1.jar

我博客里写出来了,如果你不用maven的话,请到commons-io 项目官方网站下载。

习惯用eclipse!待会儿下个jar包试一下,谢谢啦!
2 楼 yingzhor 2012-07-31  
commons-io.2.1.jar

我博客里写出来了,如果你不用maven的话,请到commons-io 项目官方网站下载。
1 楼 Healthy183 2012-07-31  
楼主,你好,IOUtils.write()和IOUtils.closeQuietly()是那个jar提供的java类里面的?怎么我的poi.jar没有这个方法?

相关推荐

Global site tag (gtag.js) - Google Analytics