DownloadController.java
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.bsth.controller;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* <一句话功能简述>
* <功能详细描述>
*
* @author Administrator
* @version [版本号, 2014年3月7日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Component
@Scope("prototype")
@RequestMapping("/downloadFile")
public class DownloadController
{
@RequestMapping("download")
public ResponseEntity<byte[]> download(String fileName) throws IOException {
fileName = fileName+".xls";
String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName;
System.out.println(moudelPath);
// String path="D:\\export\\target\\"+jName+".xls";
File file=new File(moudelPath);
HttpHeaders headers = new HttpHeaders();
String realFileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
System.out.println( URLDecoder.decode(realFileName,"utf-8"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
}