commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mathias Nilsson <wicket.program...@gmail.com>
Subject + % in file name
Date Mon, 06 Jul 2009 13:44:12 GMT

Hi,

When trying to upload a file with ( %+ ) characters in file name they are
stripped.

Here is my code

@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException {

		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("ISO-8859-1");
		FileUploadListener listener = null;

		
		if( Session.exists() ){
			if( Session.get() instanceof IUploadable ){
				listener = new FileUploadListener();
				((IUploadable) Session.get()).setFileUploadListener( listener );
				((IUploadable) Session.get()).setUploadTransfer( null );
			}
		}
		

		if( listener == null ){
			ErrorTransfer transfer = new ErrorTransfer();
			transfer.setDescription( "No listener found" );
			((IUploadable) Session.get()).setUploadTransfer(transfer);
			((IUploadable) Session.get()).setFileUploadListener( null );
			return;
		}

		upload.setProgressListener(listener);
		
		
		try {
			// iterate over all uploaded files
			List uploadedItems = upload.parseRequest(request);

			Iterator i = uploadedItems.iterator();
			String description = "";
			while (i.hasNext()) {
				FileItem fileItem = (FileItem) i.next();
				
				if (fileItem.isFormField() == false) {
					if (fileItem.getSize() > 0) {
						File uploadedFile = null;
						String myFullFileName = new String (fileItem.getName().getBytes(
"ISO-8859-1" )),
						filteredName = "",
	                    myFileName = "",
	                    slashType = (myFullFileName.lastIndexOf("\\") > 0) ?
"\\" : "/";// Windows or UNIX
						int startIndex =  myFullFileName.lastIndexOf(slashType);

						// Ignore the path and get the filename
						myFileName = myFullFileName.substring(startIndex + 1,
myFullFileName.length());
						filteredName = new String( myFileName );	
						File path = null;
						 if( Application.get( applicationName) instanceof
IUploadableApplication ){
							 path = ((IUploadableApplication) Application.get(
applicationName)).getUploadPath();
							 List<INameFilter> filters = ((IUploadableApplication)
Application.get( applicationName)).getUploadNameFilters();
							 if( filters != null ){
								 for( INameFilter filter : filters ){
									 filteredName = filter.getTransformName( filteredName );
								 }
							 }
							 
						 }
						
						// Create new File object
						uploadedFile = new File( path , filteredName);

						// Write the uploaded file to the system
						fileItem.write(uploadedFile);
						FileTransfer transfer = new FileTransfer();
						transfer.setFilteredName( new File( path , filteredName
).getAbsolutePath() );
						transfer.setOriginalName( new File( path , myFileName
).getAbsolutePath() );
	      				((IUploadable) Session.get()).setUploadTransfer(transfer); 
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
-- 
View this message in context: http://www.nabble.com/%2B---in-file-name-tp24355676p24355676.html
Sent from the Commons - User mailing list archive at Nabble.com.


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


Mime
View raw message