package foundation.io.object;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.io.PrintWriter;
|
import java.io.UnsupportedEncodingException;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import foundation.dao.DataWriter;
|
import foundation.handler.LetterWriter;
|
import foundation.handler.ResultPool;
|
import foundation.json.JSONWriter;
|
|
public class DownloadWriter extends LetterWriter {
|
|
private HttpServletResponse response;
|
|
public DownloadWriter(DataWriter dataWriter) {
|
ResultPool resultPool = dataWriter.getResultPool();
|
|
this.response = resultPool.getResponse();
|
resultPool.setFireReplay(false);
|
}
|
|
public void write(File file, String fileName, DownloadAction action) throws IOException {
|
writeHeader(fileName, action);
|
|
OutputStream outputStream = response.getOutputStream();
|
|
try {
|
if (file != null && file.exists()) {
|
InputStream inputStream = new FileInputStream(file);
|
try {
|
byte[] buffer = new byte[1024]; int numberRead = 0;
|
|
while ((numberRead = inputStream.read(buffer)) != -1) {
|
outputStream.write(buffer, 0, numberRead);
|
}
|
}
|
finally {
|
inputStream.close();
|
}
|
}
|
else {
|
String message = "foundation/file " + fileName + " not find";
|
outputStream.write(message.getBytes());
|
}
|
}
|
finally {
|
outputStream.flush();
|
outputStream.close();
|
}
|
}
|
|
public void write(String name, StringBuilder content, DownloadAction action) throws IOException {
|
writeHeader(name, action);
|
|
PrintWriter writer = response.getWriter();
|
try {
|
writer.write(content.toString());
|
}
|
finally {
|
if (writer != null) {
|
writer.flush();
|
writer.close();
|
}
|
}
|
}
|
|
private void writeHeader(String name, DownloadAction action) throws UnsupportedEncodingException {
|
if (action == null) {
|
action = DownloadAction.SaveAs;
|
}
|
|
String prefix = "";
|
|
if (DownloadAction.SaveAs == action) {
|
response.setHeader("Content-Type", "application/octet-stream");
|
prefix = "attachment;";
|
}
|
else if (DownloadAction.AsJPEG == action) {
|
response.setHeader("Content-Type", "image/jpeg");
|
}
|
else if (DownloadAction.AsPng == action) {
|
response.setHeader("Content-Type", "image/png");
|
}
|
else if (DownloadAction.AsGIF == action) {
|
response.setHeader("Content-Type", "image/gif");
|
}
|
else if (DownloadAction.AsBmp == action) {
|
response.setHeader("Content-Type", "image/bmp");
|
}
|
else if (DownloadAction.AsExcel == action) {
|
response.setHeader("Content-Type", "application/vnd.ms-excel");
|
}
|
else if (DownloadAction.AsWord == action) {
|
response.setHeader("Content-Type", "application/msword");
|
}
|
else if (DownloadAction.AsPowerPoint == action) {
|
response.setHeader("Content-Type", "application/x-ppt");
|
}
|
else if (DownloadAction.AsPDF == action) {
|
response.setHeader("Content-Type", "application/pdf");
|
}
|
else if (DownloadAction.ASTexT == action) {
|
response.setHeader("Content-Type", "text/plain");
|
}
|
|
response.setHeader("Content-Disposition", prefix + "filename=" + JSONWriter.encodeValue(name));
|
}
|
|
}
|