1
|
/*
|
2
|
* Copyright EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.remote.view;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.FileInputStream;
|
13
|
import java.io.IOException;
|
14
|
import java.io.InputStreamReader;
|
15
|
import java.io.Writer;
|
16
|
import java.util.Locale;
|
17
|
import java.util.Map;
|
18
|
|
19
|
import javax.servlet.http.HttpServletRequest;
|
20
|
import javax.servlet.http.HttpServletResponse;
|
21
|
|
22
|
import org.apache.commons.io.IOUtils;
|
23
|
import org.springframework.web.servlet.View;
|
24
|
|
25
|
/**
|
26
|
* This view will return a simple HTTP file download,
|
27
|
* please specifiy content type, file name, suffix of
|
28
|
* file name and the encoding of the file.
|
29
|
*
|
30
|
*
|
31
|
* @author a.oppermann
|
32
|
* @date 20.03.2014
|
33
|
*
|
34
|
*/
|
35
|
public class FileDownloadView implements View{
|
36
|
|
37
|
private String contentType;
|
38
|
|
39
|
private String fileName;
|
40
|
|
41
|
private String suffix;
|
42
|
|
43
|
private String encoding;
|
44
|
|
45
|
|
46
|
|
47
|
/**
|
48
|
* This view will render a simple HTTP file download
|
49
|
*
|
50
|
* <p>
|
51
|
*
|
52
|
* Please make sure you set contentType, fileName, suffix and encoding via setter-methods.
|
53
|
* Otherwise you'll run into an exception.
|
54
|
*/
|
55
|
public FileDownloadView(){
|
56
|
//empty constructor
|
57
|
}
|
58
|
|
59
|
|
60
|
/**
|
61
|
* This view will render a simple HTTP file download.
|
62
|
*
|
63
|
* @param contentType like "text/csv" or "application/pdf"
|
64
|
* @param fileName specifiy the name of the file you want to save
|
65
|
* @param suffix file ending like "txt" or "pdf"
|
66
|
* @param encoding charset like "utf-8"
|
67
|
*/
|
68
|
public FileDownloadView(String contentType, String fileName, String suffix, String encoding){
|
69
|
this.contentType = contentType;
|
70
|
this.fileName = fileName;
|
71
|
this.suffix = suffix;
|
72
|
this.encoding = encoding;
|
73
|
}
|
74
|
|
75
|
|
76
|
/* (non-Javadoc)
|
77
|
* @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
78
|
*/
|
79
|
@Override
|
80
|
public void render(Map<String, ?> model, HttpServletRequest arg1, HttpServletResponse response) throws Exception {
|
81
|
response.setContentType(getContentType()+"; charset="+getEncoding().toLowerCase(Locale.ENGLISH));
|
82
|
response.setCharacterEncoding(getEncoding().toUpperCase(Locale.ENGLISH));
|
83
|
response.setHeader("Content-Disposition", "attachment; filename=\""+getFileName()+"."+getSuffix()+"\"");
|
84
|
Writer out = response.getWriter();
|
85
|
//set File
|
86
|
if(model.containsKey("file")){
|
87
|
Object object = model.get("file");
|
88
|
if(object instanceof File){
|
89
|
try{
|
90
|
File file = (File) object;
|
91
|
FileInputStream fis = new FileInputStream(file);
|
92
|
InputStreamReader isr = new InputStreamReader(fis, getEncoding().toUpperCase(Locale.ENGLISH));
|
93
|
IOUtils.copy(isr, out);
|
94
|
response.flushBuffer();
|
95
|
}catch(IOException e){
|
96
|
throw new IOException("IOERROR writing file to outputstream \n" + e);
|
97
|
}
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/* (non-Javadoc)
|
103
|
* @see org.springframework.web.servlet.View#getContentType()
|
104
|
*/
|
105
|
@Override
|
106
|
public String getContentType() {
|
107
|
return contentType;
|
108
|
}
|
109
|
|
110
|
public void setContentType(String contentType){
|
111
|
this.contentType = contentType;
|
112
|
}
|
113
|
|
114
|
public String getFileName() {
|
115
|
return fileName;
|
116
|
}
|
117
|
|
118
|
public void setFileName(String fileName){
|
119
|
this.fileName = fileName;
|
120
|
}
|
121
|
|
122
|
public String getSuffix() {
|
123
|
return suffix;
|
124
|
}
|
125
|
|
126
|
public void setSuffix(String suffix){
|
127
|
this.suffix = suffix;
|
128
|
}
|
129
|
|
130
|
public String getEncoding() {
|
131
|
return encoding;
|
132
|
}
|
133
|
public void setEncoding(String encoding) {
|
134
|
this.encoding = encoding;
|
135
|
}
|
136
|
}
|