How can I send a file from Client to Server?

Hello Guys:

I have one portlet with controls "File Input", and I need attachment files with this controls, can you tell me that have to do to make it work? 

One example please.  

:?

package pt.infarmed;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImportarFicheiroUtilitario {

private String fileName = null;
private String filePath = null;

public boolean importarFicheiro(org.apache.commons.fileupload.FileItem fileItem, String specificFolderName)
{
	try {

		if(fileItem!=null && fileItem.getName()!=null){

			fileName = fileItem.getName();
			
			if(fileName != null || !"".equals(fileName))
			{

				System.out.println("\n+++ File Found +++");

				String parentFolderName = getServerHomePath();
				String gtaxFolderName = "some folder name";
				String docAnexFolderName = specificFolderName;
		
				File folder = new File(folderName);
				if(folder.exists()==false){
					boolean b = folder.mkdir();
				}

				int isNewFile = fileItem.getInputStream().available();
				if (isNewFile >= 1){

					String completeFileName = folderName + fileName ;		
					putFile(fileItem.getInputStream(),completeFileName);

					filePath =  "some file path";

				}
				else
				{
					filePath =  "some file path";
				}
				System.out.println("\n+++ Filepath:"+filePath);
			}
			else
			{
				return false;
			}

			fileItem.delete();
		}
		else
		{
			return false;
		}
		
	} catch (Exception e) {
		System.out.println("\n e:"+e);
		e.printStackTrace();
	}

	return true;

}

private void putFile(java.io.InputStream instream, String path){

	FileOutputStream fos = null;
	BufferedOutputStream bos = null;
	try
	{
		fos = new FileOutputStream(path, false);
		int BUFSIZE = 4096;
		bos = new BufferedOutputStream(fos, BUFSIZE);

		byte[] buf = new byte[BUFSIZE];
		// Possible values of r: (1) r > 0, or (2) r <0> 0)
		{
			bos.write(buf, 0, r);
			r = instream.read(buf, 0, BUFSIZE);
		}
		bos.close();
		instream.close(); // reached the end-of-file
	}
	catch (IOException e)
	{
		try
		{
			if (bos != null)
			{
				bos.close();
			}
			else
			{
				if (fos != null)
				{
					fos.close();
				}
			}
			instream.close();
		}
		catch (IOException e2)
		{
		}
	}
}


private String getServerHomePath() {
	String result;
	
	result = System.getenv("SERVER_HOME");
	if (result != null && result.trim() != "" && !result.trim().equalsIgnoreCase("null"))
		return result;
	
	result = System.getProperty("user.dir");
	if (result != null && result.trim() != "" && !result.trim().equalsIgnoreCase("null"))
		return result + ".." + File.separator + "server " + File.separator + "default";	// assume default server
	
	return null;
}

public String getFileName() {
	return fileName;
}

public void setFileName(String fileName) {
	this.fileName = fileName;
}

public String getFilePath() {
	return filePath;
}

public void setFilePath(String filePath) {
	this.filePath = filePath;
}

}

Hi Herve:

Before nothing Thanks for your support, I can insert and modified your code in my portlet but when I do clic in the button the portlet does nothing.

Can you tell me how can I bind the button with the code please or it this automatic?

Attachment code that my Portlet and image.

Thanks.


portlet.txt (6.23 KB)