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

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.

主站蜘蛛池模板: 凌海市| 江津市| 额尔古纳市| 白河县| 宣恩县| 齐齐哈尔市| 霸州市| 商水县| 海口市| 新和县| 稷山县| 通山县| 阳朔县| 扎囊县| 临潭县| 白沙| 阳城县| 盖州市| 本溪市| 娱乐| 文登市| 孝昌县| 垫江县| 淮滨县| 井冈山市| 扎赉特旗| 山西省| 莫力| 通渭县| 胶南市| 南开区| 从化市| 北票市| 旬邑县| 恩施市| 玛多县| 惠东县| 瑞安市| 垫江县| 灵寿县| 耿马|