DownloadController.java
4.41 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.bsth.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletResponse;
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;
import com.mysql.fabric.Response;
/**
* <一句话功能简述>
* <功能详细描述>
*
* @author Administrator
* @version [版本号, 2014年3月7日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Component
@Scope("prototype")
@RequestMapping("/downloadFile")
public class DownloadController
{
@RequestMapping("download2")
public ResponseEntity<byte[]> download2(String fileName) throws IOException {
String fileNames=URLDecoder.decode(fileName,"UTF-8");
fileNames = fileNames + ".xls";
String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileNames;
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", fileNames);
System.out.println( URLDecoder.decode(realFileName,"utf-8"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
@RequestMapping("download")
public void download(HttpServletResponse response,String fileName)
throws IOException {
// String fileNames=URLDecoder.decode(fileName,"UTF-8");
fileName =fileName + ".xls";
String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName;
File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
// String filename = file.getName();// 获取日志文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(moudelPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
}
@RequestMapping("downloadList")
public void downloadList(HttpServletResponse response,String fileName)
throws IOException {
// String fileNames=URLDecoder.decode(fileName,"UTF-8");
fileName =fileName + ".zip";
String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName;
File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
// String filename = file.getName();// 获取日志文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(moudelPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
}
}