aspose file tools*
The moose likes JSP and the fly likes ENCTYPE='multipart/form-data and request.getParameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "ENCTYPE= Watch "ENCTYPE= New topic
Author

ENCTYPE='multipart/form-data and request.getParameter

Marie McKeown
Greenhorn

Joined: Nov 22, 2005
Posts: 1
Hello I have a query,

I am just testing something out and came across this problem. I have a form which has 'ENCTYPE='multipart/form-data' in its decleration.
Form contain simple input type is text with name='test' and a Submit button.

This calls process.jsp which wants to retrieve the parameter for 'test' ie - what the user has input into the text box.
Thing is using String test = request.getParameter("test"); prints NULL when form has 'ENCTYPE='multipart/form-data' if I remove this prints value input by user. I need the form to be of this ENCTYPE because I will also have input type='file' for user to browse for a file, because process.jsp will also have upload functionality.

I think there is a simple solution to this. Anyone know what?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Marie McKeown:

I think there is a simple solution to this. Anyone know what?


The getParameter method doesn't work with multipart forms.
You'll need to check with the documentation for whatever upload library you're using.
I've used both the O'Reilly Multipart Request and the jakarta/commons/fileupload libs from Apache.
Both provide easy ways to retrieve form parameters.

There is more here:
http://faq.javaranch.com/view?FileUpload


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sree Kumari
Ranch Hand

Joined: Aug 16, 2005
Posts: 47
Get jakarta-commons file-upload component this problem can be solved easily.File component as well as textfield dat can be handled together
refer this url:
http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html?page=last
Vivek Puthiyonnan
Greenhorn

Joined: Jan 26, 2005
Posts: 18
Try --> MultipartFormDataRequest mrequest=new MultipartFormDataRequest(request);
mrequest.getParameter("txtEmpId");


Vivek P
Mauricio Porras P
Greenhorn

Joined: Aug 22, 2009
Posts: 2
Marie McKeown wrote:Hello I have a query,

I am just testing something out and came across this problem. I have a form which has 'ENCTYPE='multipart/form-data' in its decleration.
Form contain simple input type is text with name='test' and a Submit button.

This calls process.jsp which wants to retrieve the parameter for 'test' ie - what the user has input into the text box.
Thing is using String test = request.getParameter("test"); prints NULL when form has 'ENCTYPE='multipart/form-data' if I remove this prints value input by user. I need the form to be of this ENCTYPE because I will also have input type='file' for user to browse for a file, because process.jsp will also have upload functionality.

I think there is a simple solution to this. Anyone know what?


Just go to http://code.google.com/p/apache-commons-fileupload-lab/ and checkout the source code
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
hi,
use concept of formField to get file path or parameter vale.
get the FileItem type as eather parameter is i"FormField" or not "FormField"
check if it's "isFormField() " :=> if it's a formfield and you wil get the parameter value
otherwise you wil get the path to :uploading file"

following code snippet can help you: (here i have take two file uploads and one input parameter (opType); based upon input parameter value i upload first file or second file"

public void doPost(HttpServletRequest request, HttpServletResponse response)
{
System.out.println("Inside File upload");
TESTDatabaseManager databaseListener =(TESTDatabaseManager)getServletContext().getAttribute("Database");
TESTConfigurationRequestHandler configureRequestHandler = new TESTConfigurationRequestHandler();
HttpSession session = request.getSession();
String DPXLSPath = null;
String DETSXLSpath = null;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
((DiskFileItemFactory) factory).setSizeThreshold(1000);
ServletFileUpload upload = new ServletFileUpload(factory);
FileUploadListener listener = new FileUploadListener();
String operationType = null;
session.setAttribute("LISTENER", listener);
upload.setProgressListener(listener);
if(!isMultipart)
{
System.out.println("error");
}

else
{
upload.setSizeMax(1000000000);
boolean uploadStatusFlag = false;
try
{
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
String inputName = null;
String inputValue = null;
String fullFileName = null;
System.out.println("Number of Items = " + items.size());
String uploadDirPath = new TESTCommonUtility().getParameterFromConfig("TEST_repository_location");

while(iter.hasNext()){
FileItem item = (FileItem)iter.next();

if(item.isFormField()){
inputName = (String)item.getFieldName();
if(inputName.equalsIgnoreCase("opType")){
operationType = (String)item.getString();
System.out.println("OPERATION TYPE:"+operationType);
}

}else if( !item.isFormField() ) {

//int res = processUploadedFile(item, request, response, databaseListener);
String name = item.getName();
System.out.println("get name value :"+name);
if(name.equalsIgnoreCase(null)||name.equalsIgnoreCase("")){
System.out.println("NULL / BLANK VALUE FOR FILE.......!!!");
}else{
String fileName = name.substring(name.lastIndexOf("\\")+1);
System.out.println("Name of Item = " + item.getName() + " fileName = " + fileName);
fullFileName = uploadDirPath + "\\" + fileName;


System.out.println("FullFileName = " + fullFileName);
try {
item.write(new File(fullFileName));
uploadStatusFlag = true;
System.out.println("Upload file is saved as : " + fullFileName);

} catch (Exception e) {

e.printStackTrace();
uploadStatusFlag = false;
}
}

}

}

if(operationType.equalsIgnoreCase("firstFileUpload")){
{

// do first file upload operation
}
}else if(operationType.equalsIgnoreCase("secondFileUpload")){

// do second file upload operation

}
else{
System.out.println("WRONG INPUT TYPE");
}


}
catch (FileUploadException e)
{

e.printStackTrace();
}

}
System.out.println("End of File upload");

}
jon wincha
Greenhorn

Joined: Apr 09, 2013
Posts: 1
you are not able to get file values if you are using enctype=multipart/form-data with request.getparameter() so here is a link for good example when working with multipart...may be it can help

http://webideaworld.blogspot.in/2013/04/woking-with-enctypemultipartform-data.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ENCTYPE='multipart/form-data and request.getParameter
 
Similar Threads
file upload in servlet?
problem with multipart/form-data ....
acessing data of the textarea within the form of multipart type
getting null when using request.getParameter()
File uploading and data collection problem