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

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.

主站蜘蛛池模板: 五原县| 奉贤区| 新乡市| 沙坪坝区| 镇平县| 乐山市| 永吉县| 大姚县| 都安| 上林县| 昌平区| 广水市| 定日县| 新晃| 东城区| 河间市| 铁岭县| 铜陵市| 修文县| 黄大仙区| 鹤庆县| 简阳市| 西宁市| 萨嘎县| 老河口市| 淮阳县| 九江县| 贵溪市| 正镶白旗| 桃园县| 保康县| 张家口市| 策勒县| 玉林市| 永顺县| 保靖县| 定州市| 赤水市| 黄浦区| 凤城市| 凤翔县|