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); } } 
1

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.