如何将HttpRequestWrapper与Filter一起使用以重用Request

时间:2014-11-05 13:38:04

标签: http servlets httprequest httpresponse

我需要实现HttpRequestWrapper和Filter才能重用我的请求。这是因为我首先需要请求才能看到用户选择的内容,其次是将文件上传到tomcat服务器。现在我有一个Servlet上传文件“public class UploadFile extends HttpServlet {}”,它为每个用户运行。一旦它返回用户选择的内容,它就会变为null,我无法将文件上传到服务器。只是想知道为了实现HttpRequestWrapper,我必须更改我的代码吗?我是否将“公共类UploadFile extends HttpServlet {}”更改为“public class UploadFile extends HttpRequestWrapper {}”?

1 个答案:

答案 0 :(得分:0)

So firstly I check the parameters to see which checkbox has been ticked : 

String ConvertFile = request.getParameter("ConvertFile"); 

String Powershell = request.getParameter("Powershell");

String LMBackup = request.getParameter("LM_Backup");

String Restful_API = request.getParameter("Restful_API");        

if (Powershell != null) {
            Powershell = request.getParameter("Powershell");
        } 

if (LMBackup != null) {
            LMBackup = request.getParameter("LM_Backup");
        }

if (Restful_API != null) {
            Restful_API = request.getParameter("Restful_API");
        }



if (ConvertFile != null && LMBackup == null && Powershell != null && Restful_API == null)
 {
            System.out.println(ConvertFile + " and " + Powershell + " selected");

doUpload(request, response); //Here the request that is being passed is now null because I've used it previously

response.sendRedirect("index.jsp");

        }

//上传方法如图所示 //项目保持为null,因为请求已被使用

protected void doUpload(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        try {
            System.out.println("Uploading File");
            boolean ismultipart = ServletFileUpload.isMultipartContent(request);
            System.out.println(request + " <<<<<<<<<<<<<+++++++++++++");
            if (ismultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List<FileItem> items = null;
                try {
                    items = upload.parseRequest(request);
                    System.out.println(items + "<<<<<<<<<--------------");
                } catch (Exception e) {
                    System.out.println(e);
                }