Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Marie McKeown
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sree Kumari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try --> MultipartFormDataRequest mrequest=new MultipartFormDataRequest(request);
mrequest.getParameter("txtEmpId");
 
Mauricio Porras P
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic