Struts 2で簡単にアップロードするアクション.


public class UploadAction extends ActionSupport
{
	private File file; //          

	private String fileFileName; //         
        //   File xxx  :                。
        //   String xxxFileName  :                 。 

	public File getFile()
	{
		return file;
	}

	public void setFile(File file)
	{
		this.file = file;
	}

	public String getFileFileName()
	{
		return fileFileName;
	}

	public void setFileFileName(String fileFileName)
	{
		this.fileFileName = fileFileName;
	}

	@Override
	public String execute() throws Exception
	{
		InputStream is = new FileInputStream(file);
		OutputStream os = new FileOutputStream("D:\\" + fileFileName);

		byte[] buffer = new byte[1024];
		int length = 0;

		while (-1 != (length = is.read(buffer)))
		{
			os.write(buffer,0,length);
		}
		
		os.close();
		is.close();

		return SUCCESS;
	}
}