You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Meena Rajvaidya <ra...@gmail.com> on 2008/10/30 00:38:21 UTC

Filupload and request parameters

Hello,

I am using FileUpload for multipart request handling, my request
contains three different parameter and one file, I want to read those
parameters first and then read the file as those paramters construct
directory where this file needs to be stored, here is the code

protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException {
		int dir1 = 0 ;
		int dir2 = 0 ;
		int dir3 = 0;
		String uploadLocation = null;
		String fileName = null;
		// parse request
		if(ServletFileUpload.isMultipartContent(request)){
			ServletFileUpload upload = new ServletFileUpload();
			try {
				FileItemIterator iterator = upload.getItemIterator(request);
				while(iterator.hasNext()){
					FileItemStream item = iterator.next();
					String name = item.getFieldName();
					InputStream stream = item.openStream();
					if(item.isFormField()){
						if("dir1".equals(name)){
							dir1 = Integer.parseInt(Streams.asString(stream));
						}else if("dir2".equals(name)){
							dir2 = Integer.parseInt(Streams.asString(stream));
						}else if("dir3".equals(name)){
							dir3 = Integer.parseInt(Streams.asString(stream));
						}
						uploadLocation =
server_url+File.separatorChar+tenantId+File.separatorChar+collectionId+File.separatorChar+rowId
;
				new File(uploadLocation).mkdirs();
						}else {
							System.out.println("File field" + name + "with file name" +
item.getName());
							fileName = item.getName();
							//perform file storing operations
				
				File uploadedFile = new File(uploadLocation+File.separatorChar+fileName);
				
				FileOutputStream outputstream = new FileOutputStream(uploadedFile);
				long number = Streams.copy(testStream, outputstream, true);
				System.out.println("result from outpoutstream operation" + number);
					}
					
				}
				
				
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch(NumberFormatException e){
				e.printStackTrace();
			}
		}
	
    }


however for some reason FileItem is being read first so I can not
store this file at desired location it is (/usr/sim/0/0/0/filename),
can some one please suggest how can I make sure that I read form item
first then file items.

I do appreciate al the help and time. Thanks

-sim

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


RE: Filupload and request parameters

Posted by "F. Andy Seidl" <fa...@myst-technology.com>.
Meena:

Check the form post you use to generate the request.  Make sure the file
field is *after* the other fields.  The fields are posted in the order they
appear in the form and will be accessed in the same order on the server
side.

-- fas

-----Original Message-----
From: Meena Rajvaidya [mailto:rajvaidya.meena@gmail.com] 
Sent: Wednesday, October 29, 2008 7:38 PM
To: user@commons.apache.org
Subject: Filupload and request parameters

Hello,

I am using FileUpload for multipart request handling, my request
contains three different parameter and one file, I want to read those
parameters first and then read the file as those paramters construct
directory where this file needs to be stored, here is the code

protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException {
		int dir1 = 0 ;
		int dir2 = 0 ;
		int dir3 = 0;
		String uploadLocation = null;
		String fileName = null;
		// parse request
		if(ServletFileUpload.isMultipartContent(request)){
			ServletFileUpload upload = new ServletFileUpload();
			try {
				FileItemIterator iterator =
upload.getItemIterator(request);
				while(iterator.hasNext()){
					FileItemStream item =
iterator.next();
					String name = item.getFieldName();
					InputStream stream =
item.openStream();
					if(item.isFormField()){
						if("dir1".equals(name)){
							dir1 =
Integer.parseInt(Streams.asString(stream));
						}else
if("dir2".equals(name)){
							dir2 =
Integer.parseInt(Streams.asString(stream));
						}else
if("dir3".equals(name)){
							dir3 =
Integer.parseInt(Streams.asString(stream));
						}
						uploadLocation =
server_url+File.separatorChar+tenantId+File.separatorChar+collectionId+File.
separatorChar+rowId
;
				new File(uploadLocation).mkdirs();
						}else {
	
System.out.println("File field" + name + "with file name" +
item.getName());
							fileName =
item.getName();
							//perform file
storing operations
				
				File uploadedFile = new
File(uploadLocation+File.separatorChar+fileName);
				
				FileOutputStream outputstream = new
FileOutputStream(uploadedFile);
				long number = Streams.copy(testStream,
outputstream, true);
				System.out.println("result from
outpoutstream operation" + number);
					}
					
				}
				
				
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch(NumberFormatException e){
				e.printStackTrace();
			}
		}
	
    }


however for some reason FileItem is being read first so I can not
store this file at desired location it is (/usr/sim/0/0/0/filename),
can some one please suggest how can I make sure that I read form item
first then file items.

I do appreciate al the help and time. Thanks

-sim

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org