sexta-feira, 31 de julho de 2009

Upload de arquivo no struts

Fazer upload de arquivos no struts é muito simples, basta seguir os passos abaixo:

1) Form

public class CarregarArquivoForm extends ActionForm{

private FormFile theFile;

/**
* @return Returns the theFile.
*/
public FormFile getTheFile() {
return theFile;
}
/**
* @param theFile The FormFile to set.
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}

2) Action

public class CarregarArquivoAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
CarregarArquivoPAQForm myForm = (CarregarArquivoPAQForm)form;

// Process the FormFile
FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
System.out.println("File Size: " + fileSize);

return mapping.findForward("success");
}
}

3) STRUTS CONFIG




path="/carregarArquivoAction"
type="br.org.teste.CarregarArquivoAction"
scope="request"
validate="true"
name="carregarArquivoPAQForm">



4) JSP

<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>





Projeto Produção













Selecione o Arquivo






Seguindo os passos acima é possivel fazer o upload de qualquer arquivo usando a classe FormFile, que facilita muito o código.

Palavras chaves: upload struts, FormFile, carregar arquivo struts e upload arquivo.

Nenhum comentário:

Postar um comentário