| 21976 |
ashik.ali |
1 |
package in.shop2020.dtrapi.controllers;
|
|
|
2 |
import java.io.ByteArrayInputStream;
|
|
|
3 |
import java.io.ByteArrayOutputStream;
|
|
|
4 |
import java.io.InputStream;
|
|
|
5 |
import java.io.OutputStream;
|
|
|
6 |
|
|
|
7 |
import javax.servlet.http.HttpServletResponse;
|
|
|
8 |
|
|
|
9 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
10 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
11 |
|
|
|
12 |
import com.opensymphony.xwork2.Action;
|
|
|
13 |
import com.opensymphony.xwork2.ActionSupport;
|
|
|
14 |
|
|
|
15 |
import in.shop2020.dtrapi.utils.PdfGenerator;
|
|
|
16 |
|
|
|
17 |
@Result(
|
|
|
18 |
name = "success",
|
|
|
19 |
type = "stream",
|
|
|
20 |
params = {
|
|
|
21 |
"contentType", "${type}",
|
|
|
22 |
"inputName", "${stream}",
|
|
|
23 |
"bufferSize", "1024",
|
|
|
24 |
"contentDisposition", "attachment;filename=\"${filename}\""
|
|
|
25 |
}
|
|
|
26 |
)
|
|
|
27 |
public class FileDisplayController extends ActionSupport implements ServletResponseAware{
|
|
|
28 |
|
|
|
29 |
//private String type = "application/pdf";
|
|
|
30 |
private String filename;
|
|
|
31 |
private InputStream stream;
|
|
|
32 |
//private HttpServletRequest request;
|
|
|
33 |
private HttpServletResponse response;
|
|
|
34 |
private int id;
|
|
|
35 |
|
|
|
36 |
//@org.apache.struts2.convention.annotation.Action(value = "/fofoDetailPdf", params="fofoId")
|
|
|
37 |
public String show() throws Exception {
|
|
|
38 |
//int fofoId = Integer.parseInt(request.getParameter("fofoId"));
|
|
|
39 |
//filename = "myimage.jpg";
|
|
|
40 |
//File img = new File("/path/to/image/image.jpg");
|
|
|
41 |
OutputStream outputStream = response.getOutputStream();
|
|
|
42 |
PdfGenerator.generateAndWrite(outputStream, id);
|
|
|
43 |
stream = this.toInputStream(outputStream);
|
|
|
44 |
|
|
|
45 |
response.setHeader("Cache-Control", "no-cache");
|
|
|
46 |
response.setHeader("Content-Disposition", "attachment;filename=\"fofoDetail.pdf\"");
|
|
|
47 |
response.setContentType("application/pdf");
|
|
|
48 |
response.setContentLength(((ByteArrayOutputStream)outputStream).size());
|
|
|
49 |
|
|
|
50 |
return Action.SUCCESS;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
private ByteArrayInputStream toInputStream(OutputStream outputStream){
|
|
|
54 |
ByteArrayOutputStream buffer = (ByteArrayOutputStream) outputStream;
|
|
|
55 |
byte[] bytes = buffer.toByteArray();
|
|
|
56 |
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
|
|
57 |
return inputStream;
|
|
|
58 |
}
|
|
|
59 |
/*private String getType() {
|
|
|
60 |
return this.type;
|
|
|
61 |
}*/
|
|
|
62 |
|
|
|
63 |
private String getFilename() {
|
|
|
64 |
return this.filename;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private InputStream getStream() {
|
|
|
68 |
return this.stream;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/*@Override
|
|
|
72 |
public void setServletRequest(HttpServletRequest arg0) {
|
|
|
73 |
this.request = arg0;
|
|
|
74 |
}*/
|
|
|
75 |
|
|
|
76 |
@Override
|
|
|
77 |
public void setServletResponse(HttpServletResponse arg0) {
|
|
|
78 |
this.response = arg0;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public int getId() {
|
|
|
82 |
return id;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public void setId(int id) {
|
|
|
86 |
this.id = id;
|
|
|
87 |
}
|
|
|
88 |
}
|