/*
 * $Id$ $Revision$
 * $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.markup.html.form.upload;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.channels.FileLockInterruptionException;

import wicket.Session;
import wicket.util.file.Files;
import wicket.util.upload.FileItem;

/**
 * Model for file uploads.
 * 
 * @author Jonathan Locke
 */
public class FileUpload implements Serializable
{
	private static final long serialVersionUID = 1L;

	final FileItem item;

	/**
	 * Constructor
	 * 
	 * @param item
	 *            The uploaded file item
	 */
	FileUpload(final FileItem item)
	{
		this.item = item;
	}

	/**
	 * Deletes temp file from disk
	 */
	public void delete()
	{
		item.delete();
	}

	/**
	 * @return Uploaded file as an array of bytes
	 */
	public byte[] getBytes()
	{
		return item.get();
	}

	/**
	 * @return Content type for upload
	 */
	public String getContentType()
	{
		return item.getContentType();
	}

	/**
	 * @return File object for client-side file that was uploaded.
	 */
	public File getFile()
	{
		return new File(item.getName());
	}

	/**
	 * @return Input stream with file contents.
	 * @throws IOException
	 */
	public InputStream getInputStream() throws IOException
	{
		return item.getInputStream();
	}

	/**
	 * @return The upload's size
	 */
	public long getSize()
	{
		return item.getSize();
	}

	/**
	 * Saves this file upload to a given file on the server side.
	 * 
	 * @param file
	 *            The file
	 * @throws IOException
	 */
	public void writeTo(final File file) throws IOException
	{
		InputStream is = getInputStream();
		try
		{
			Files.writeTo(file, is);
		}
		finally
		{
			is.close();
		}
	}

	/**
	 * Convinience method that copies the input stream returned by
	 * {@link #getInputStream()} into a temporary file.
	 * <p>
	 * Only use this if you actually need a {@link File} to work with, in all
	 * other cases use {@link #getInputStream()}
	 * 
	 * @return temporary file containing the contents of this file upload
	 * @throws IOException
	 */
	public final File writeToTempFile() throws IOException
	{
		File temp = File.createTempFile(Session.get().getId(), item.getFieldName());
		writeTo(temp);
		return temp;
	}
}

@@ -2,16 +2,97 @@
 *%0A * $Id
+: FileUpload.java 5069 2006-03-21 17:50:48 -0800 (Tue, 21 Mar 2006)%0A * ivaynberg 
 $ $Revis
@@ -99,17 +99,64 @@
 ion$
-%0A *
  $Date
+: 2006-03-21 17:50:48 -0800 (Tue, 21 Mar%0A * 2006) 
 $%0A *
@@ -1817,73 +1817,344 @@
 %0A%09 *
-/%0A%09public File getFile()%0A%09%7B%0A%09%09return new File(item.getName());%0A%09%7D
+ @deprecated - this method was very counterintuitive. its been replaced by%0A%09 *             getClientFileName(). see bug 1372481.%0A%09 */%0A%09public File getFile()%0A%09%7B%0A%09%09return new File(item.getName());%0A%09%7D%0A%0A%09/**%0A%09 * @since 1.2%0A%09 * @return name of uploaded client side file%0A%09 */%0A%09public String getClientFileName()%0A%09%7B%0A%09%09return item.getName();%0A%09%7D%0A
 %0A%0A%09/
@@ -2988,16 +2988,59 @@
 tream()%7D
+ or %7B@link #getBytes()%7D%0A%09 * %0A%09 * @since 1.2
 %0A%09 * %0A%09 
@@ -3093,30 +3093,24 @@
 ts of th
-is fil
 e upload
 %0A%09 * @th
@@ -3097,24 +3097,31 @@
 f the upload
+ed file
 %0A%09 * @throws

fix [ 1372481 ] FileUpload getFile() return not existent files