- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 395字
- 2021-07-23 16:54:54
Implementing commands
Here, we have the abstract class AbstractCommand, which includes the abstract execute method. All implementations of this command extend AbstractCommand, which is an abstract class:
package com.rhuan.action.Command;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class AbstractCommand {
public abstract void execute(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, java.io.IOException ;
}
In the following block of code, we have the PdfCommand class. This is a subclass of AbstractCommand that implements the logic to download a PDF file:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class PdfCommand extends AbstractCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String fileName = request.getParameter("fileName");
// for example application/pdf, text/plain, text/html,
image/jpg
response.setContentType("application/pdf");
// Make sure to show the download dialog
response.setHeader("Content-disposition","attachment;
filename=myapp_download.pdf");
// Assume file name is retrieved from database
// For example D:\\file\\test.pdf
File file = new File(fileName);
// This should send the file to browser
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
In the preceding code block, we have the execute() method, which processes the logic to download a PDF file. At this point, all the processes and main validations of requests were executed, and the execute() method needs only to execute the download process.
Here, we have the JpgCommand class, which is a subclass of AbstractCommand that implements the logic to download a JPG file:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class JpgCommand extends AbstractCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
//Gets the file name sent by paramenter.
String fileName = request.getParameter("fileName");
//Configures the content type.
response.setContentType("image/jpg");
// Configure the dialog to download.
response.setHeader("Content-disposition","attachment;
filename=myapp_download.pdf");
//Read file and send to client.
File file = new File(fileName);
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
In the preceding code block, we have the execute() method, which processes the logic to download a JPG file. At this point, all request and main validation processes have already been done.
- Learning OpenDaylight
- 零起點(diǎn)學(xué)Linux系統(tǒng)管理
- Linux運(yùn)維實(shí)戰(zhàn):CentOS7.6操作系統(tǒng)從入門到精通
- Linux實(shí)戰(zhàn)
- BPEL and Java Cookbook
- Alfresco 4 Enterprise Content Management Implementation
- 新手學(xué)電腦從入門到精通(Windows 10+Office 2016版)
- Windows 7中文版從入門到精通(修訂版)
- Windows 7案例教程
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- 深入淺出Node.js
- iOS 10 開發(fā)指南
- 大規(guī)模分布式系統(tǒng)架構(gòu)與設(shè)計(jì)實(shí)戰(zhàn)
- Java EE 8 High Performance
- Gradle Effective Implementations Guide(Second Edition)