0

I want to upload multiple files to server and get a parameter "testname" in a JSP page . But it always return a null value. I found the cause of error. Because of "enctype="multipart/form-data"". If I remove it from the form, I can get a parameter but I can't upload multiple files to server. How can I do both of them?

lib: http://www.java2s.com/Code/Jar/c/Downloadcosmultipartjar.htm

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="UploadServlet" method="post" enctype="multipart/form-data">
            <input name="testname" type="text">
            <input type="file" id="file" name="file1" accept="image/*"  multiple="muliple" required/><br>
            <input type="submit"/>
           <br><br> ${requestScope.message}
        </form>
    </body>
</html>

UploadServlet.java

package MyPackage;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.Part;
import com.oreilly.servlet.multipart.FilePart;
/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
    private String fileSavePath;
    private static final String UPLOAD_DIRECTORY = "Upload";

    public void init() {
        fileSavePath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;/*save uploaded files to a 'Upload' directory in the web app*/
        if (!(new File(fileSavePath)).exists()) {
            (new File(fileSavePath)).mkdir();    // creates the directory if it does not exist        
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
        String testname=request.getParameter("testname");
        System.out.print(testname);
        String resp = "";
        int i = 1;
        resp += "<br>Here is information about uploaded files.<br>";
        try {
            MultipartParser parser = new MultipartParser(request, 1024 * 1024 * 1024);  /* file limit size of 1GB*/
            Part _part;
            while ((_part = parser.readNextPart()) != null) {
                if (_part.isFile()) {
                    FilePart fPart = (FilePart) _part;  // get some info about the file
                    String name = fPart.getFileName();
                    if (name != null) {
                        long fileSize = fPart.writeTo(new File(fileSavePath));
                        resp += i++ + ". " + fPart.getFilePath() + "[" + fileSize / 1024 + " KB]<br>";
                    } else {
                        resp = "<br>The user did not upload a file for this part.";
                    }
                }
            }// end while 
        } catch (java.io.IOException ioe) {
            resp = ioe.getMessage();
        }
        request.setAttribute("message", resp);
        getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    }

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Thế Hiệp
  • 35
  • 3
  • 11
  • 1
    possible duplicate of http://stackoverflow.com/questions/15729777/servlet-get-parameter-from-multipart-form-in-tomcat-7 and http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – Srinu Chinka Dec 01 '15 at 11:02
  • thanks a lot. i'll try it – Thế Hiệp Dec 01 '15 at 15:14

1 Answers1

2

While using enctype="multipart/form-data", you will not get your parameters by calling request.getParameter(). The parameters are a part of the stream now.

I suggest you check the question How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
Halley
  • 521
  • 10
  • 35