官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 伊金霍洛旗| 山东省| 额尔古纳市| 濉溪县| 安图县| 乃东县| 怀宁县| 屏山县| 泰宁县| 女性| 谷城县| 禹州市| 高台县| 句容市| 冕宁县| 巴林右旗| 乌苏市| 乐平市| 海南省| 济南市| 通城县| 咸宁市| 石首市| 香河县| 沂南县| 英山县| 樟树市| 通城县| 景德镇市| 蚌埠市| 厦门市| 南涧| 霍城县| 马尔康县| 扎鲁特旗| 罗城| 桂东县| 睢宁县| 夏河县| 乐陵市| 微博|