将文件从一个servlet传输到另一个servlet

时间:2014-02-19 07:56:12

标签: java servlets file-upload

我有两个servlet的Java应用程序。第一个servlet将上传文件。我想将文件从第一个servlet发送到第二个servlet。

上传文件:

public void post(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        // set location for saving uploaded file
        UploadBean upb = new UploadBean();
        /*
         * Path path = Paths.get(UPLOAD_DIR).toAbsolutePath();
         * System.out.println(path); String strPath = path.toString();
         */
        upb.setFolderstore(UPLOAD_DIR);
        upb.setFilesizelimit(1073741824);
        upb.setOverwrite(true);

        // 1024 * 1024 * 100
        // 1073741824
        // 1048576

        MultipartFormDataRequest nreq = new MultipartFormDataRequest(
                request);
        // completed file uploading

        upb.store(nreq);
        Hashtable<?, ?> ht = nreq.getFiles();// gives the uploaded file
        Enumeration<?> e = ht.elements();
        while (e.hasMoreElements()) {
            upfile = (UploadFile) e.nextElement();
            uploadFilePath = UPLOAD_DIR + File.separator;
            File fileSaveDir = new File(uploadFilePath);
            if (!fileSaveDir.exists()) {
                fileSaveDir.mkdirs();
            }
            file = UPLOAD_DIR + File.separator + upfile.getFileName();
            filePath = new File(file).getAbsolutePath();
            fileName = upfile.getFileName()
                    .substring(0, upfile.getFileName().indexOf("."))
                    .replaceAll("\\s+", "").trim().concat("_")
                    .concat("data");
        }
        ServletContext sc = getServletContext();
        RequestDispatcher rd = sc.getRequestDispatcher("./vdr.h‌​tml");
        request.setAttribute("filePath", filePath);
        rd.forward(request, response);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

分析:

protected void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    PrintWriter pw = res.getWriter();
    res.setContentType("text/html");

    double base_x = Double.parseDouble(req.getParameter("n1"));
    double base_y = Double.parseDouble(req.getParameter("n2"));
    double m0 = Double.parseDouble(req.getParameter("n3"));
    double a = Double.parseDouble(req.getParameter("n4"));
    double b = Double.parseDouble(req.getParameter("n5"));
    double k1 = Double.parseDouble(req.getParameter("n6"));
    double k2 = Double.parseDouble(req.getParameter("n7"));
    double k3 = Double.parseDouble(req.getParameter("n8"));
    double k4 = Double.parseDouble(req.getParameter("n9"));
    double k5 = Double.parseDouble(req.getParameter("n10"));
    double k6 = Double.parseDouble(req.getParameter("n11"));
    double k7 = Double.parseDouble(req.getParameter("n12"));
    double k8 = Double.parseDouble(req.getParameter("n13"));
    double Lbp = Double.parseDouble(req.getParameter("n14"));

    double ido00 = base_x * PI / 180.0;

    double keido00 = base_y * PI / 180.0;

    double s0 = k1 * ido00 + k2 * Math.sin(2.0 * ido00) + k3
            * Math.sin(4.0 * ido00) + k4 * Math.sin(6.0 * ido00) + k5
            * Math.sin(8.0 * ido00) + k6 * Math.sin(10.0 * ido00) + k7
            * Math.sin(12.0 * ido00) + k8 * Math.sin(14.0 * ido00);
    double f = 1.0 / 299.152813;
    double e2 = (Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2);

    pw.println("VDR Analysis " + ido00 + "  " + keido00 + "  " + s0 + "  "
            + f + "  " + e2);
    pw.close();

    //get the uploaded file

}

的index.html:

<body style="background-color: #d6e6f6;">
<form action="ts2" method="post" enctype="multipart/form-data" onsubmit="return Validate(this);">
    <table style="width: 100%; text-align: center;">
        <tr>
            <td style="text-align: center;"></td>
                  .....................................
        </tr>
    </table>
</form>
</body>

vdr.html:

<body style="background-color: #d6e6f6;">
<form name="myForm" action="ts2" method="post"
    onsubmit="return Validate(this);">
    <table style="width: 100%;">
        <tr>
            <td style="text-align: center;"></td>
        </tr>
    </table>
</form>

的web.xml:

    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>ts1</servlet-name>
        <servlet-class>com.imu.reqdis.AisNmeaFileUploadServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ts2</servlet-name>
        <servlet-class>com.imu.reqdis.VDRAnalysis</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ts1</servlet-name>
        <url-pattern>/test/ser1</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ts2</servlet-name>
        <url-pattern>/ser2</url-pattern>
    </servlet-mapping>

Servlet1:

File Uploading

Servlet2:

Calculate the input values using the uploaded file

先谢谢

0 个答案:

没有答案