Bom dia pessoal,
Eu estou tentando implementar um upload de arquivos no meu sistema.
Fiz um outro projeto para testar o procedimento e ele funcionou muito bem...porém...
quando implementei isso no meu projeto, ele começou a me retornar a seguinte Exception:
WARNING: Error setting expression 'upload' with value '[Ljava.lang.String;@f7a68bc'
ognl.MethodFailedException: Method "setUpload" failed for object br.com.jm.actions.DemandaAction@5cda8984 [java.lang.NoSuchMethodException: br.com.jm.actions.DemandaAction.setUpload([Ljava.lang.String;)]
Ou seja, eu estaria tentando setar um campo do tipo File (private File upload), com um valor em String.
Mas...wtf?!?!
Não consigo entender porque ele converteria o valor do campo
Pelo que pude notar, a unica diferença entre os 2 formulários (o da minha aplicaçao e a aplicaçao de teste), é que o formulario da minha aplicaçao envia outros valores
além do campo
Alguém de voces tem alguma tese?
Segue codigo do formulario:
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sj" uri="/struts-jquery-tags"%>
<div class="elBody">
<div class="ui-widget-header elHeader">Demandas</div>
<br>
<s:if test="%{errorLog==true}">
<sj:div id="errorLog" cssStyle="width: 99%; border-radius: 10px; background-color: #FFC1C1; padding: 4px;text-align:center;">
Existem campos obrigatórios que devem ser preenchidos.
</sj:div>
</s:if>
<s:form id="formIncluirDemanda" theme="simple" method="POST" enctype="multipart/form-data">
<table style="width: 100%;">
<tr>
<td>Solicitação da Demanda: </td>
<td><sj:autocompleter name="demanda.solic" list="listaSolics" listKey="solic" listValue="solic" selectBoxIcon="true" onSelectTopics="refreshFormDemanda"/></td>
<s:if test="%{demanda.solic.equals('OUTRO')}">
<td>Outro: </td>
<td><s:textfield name="demanda.nsolic" required="true" onchange="upper(this);" size="30"/></td>
</s:if>
<tr>
<td>Eleitor: </td>
<td>
<s:hidden name="demanda.eleitor.idEleitor"/>
<s:textfield name="demanda.eleitor.nome" onchange="resize2EleNome();upper(this);publishTopic('detectEleitorTopics');" size="30"/>
<sj:submit type="image" src="Imagens/lupa.png" cssStyle="width: 20px; height: 20px;" openDialog="popUpDialog2" href="demanda!findEleitor.action" formIds="formIncluirDemanda"/>
</td>
<td>Local: </td>
<td><sj:autocompleter required="true" name="demanda.local" list="listaLocais" listKey="local" listValue="local" selectBoxIcon="true"/></td>
</tr>
<tr>
<td>Descrição da Demanda: </td>
<td colspan="3"><s:textfield name="demanda.desc" onchange="upper(this);" size="50" maxlength="200"/></td>
</tr>
<tr>
<td>Setor: </td>
<td><sj:autocompleter required="true" name="ocorrencia.setor.idSetor" list="listaSetores" listKey="idSetor" listValue="nome" onSelectTopics="refreshFormDemanda" onBlurTopics="refreshFormDemanda" selectBoxIcon="true"/></td>
<td>Responsável: </td>
<td><sj:autocompleter name="ocorrencia.responsavel.idUsuario" list="listaUsuarios" listKey="idUsuario" listValue="nome" selectBoxIcon="true"/></td>
</tr>
<tr>
<td>Solicitação de Procedimento: </td>
<td colspan="3"><s:textfield required="true" name="ocorrencia.solicitacao" onchange="upper(this);" size="50" maxlength="200"/></td>
</tr>
<tr>
<td>Anexo: </td>
<td colspan="3"><s:file name="upload"/></td>
</tr>
<tr>
<td colspan="4" align="center">
<sj:submit value="Incluir" button="true" targets="idDivContent" href="demanda!incluirDemanda.action" formIds="formIncluirDemanda" buttonIcon="ui-icon-gear"/>
<sj:submit cssStyle="display:none;" targets="idDivContent" href="demanda!refreshFormDemanda.action" formIds="formIncluirDemanda" listenTopics="refreshFormDemanda"/>
</td>
</tr>
</table>
</s:form>
</div>
Segue codigo da Action:
package br.com.jm.actions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.exception.ConstraintViolationException;
import br.com.jm.beans.Atividade;
import br.com.jm.beans.Demanda;
import br.com.jm.beans.Eleitor;
import br.com.jm.beans.Ocorrencia;
import br.com.jm.beans.Setor;
import br.com.jm.beans.Usuario;
import br.com.jm.factory.DemandaFactory;
import br.com.jm.factory.EleitorFactory;
import br.com.jm.factory.LogFileFactory;
import br.com.jm.factory.OcorrenciaFactory;
import br.com.jm.factory.SetorFactory;
import br.com.jm.factory.UsuarioFactory;
import com.opensymphony.xwork2.ActionSupport;
public class DemandaAction extends ActionSupport implements SessionAware, Serializable, ServletContextAware{
private static final long serialVersionUID = -6309610223718420989L;
private Map<String, Object> session = new HashMap<String, Object>();
private Map<String, Object> reportParams = new HashMap<String, Object>();
private List<Demanda> listaDemandas = new ArrayList<Demanda>();
private List<Ocorrencia> listaOcorrencias = new ArrayList<Ocorrencia>();
private List<Ocorrencia> listaOcorrenciasSetor = new ArrayList<Ocorrencia>();
private ServletContext servletContext;
private List<Eleitor> listaEleitores = new ArrayList<Eleitor>();
private List<Setor> listaSetores = new ArrayList<Setor>();
private List<Demanda> listaLocais = new ArrayList<Demanda>();
private List<Demanda> listaSolics = new ArrayList<Demanda>();
private List<Usuario> listaUsuarios = new ArrayList<Usuario>();
private List<Usuario> listaResponsaveis = new ArrayList<Usuario>();
private List<Atividade> permissoes = new ArrayList<Atividade>();
private Demanda demanda = new Demanda();
private Ocorrencia ocorrencia = new Ocorrencia();
private DemandaFactory df = new DemandaFactory();
private EleitorFactory ef = new EleitorFactory();
private SetorFactory sf = new SetorFactory();
private UsuarioFactory uf = new UsuarioFactory();
private OcorrenciaFactory of = new OcorrenciaFactory();
private LogFileFactory lFac = new LogFileFactory();
private int paginaAtual = 1;
private int begin = 0;
private int end = 0;
private int paginas = 0;
private double pgaux = 0;
private int SpaginaAtual = 1;
private int Sbegin = 0;
private int Send = 0;
private int Spaginas = 0;
private int CpaginaAtual = 1;
private int Cbegin = 0;
private int Cend = 0;
private int Cpaginas = 0;
private Date reportDate = new Date();
private boolean errorLog = false;
private File upload;
private String uploadFileName;
private String uploadContentType;
private FileInputStream fileInputStream;
private String caminho;
private String contentDisposition;
public String incluirDemanda(){
if(session.get("usuario")==null){
return "errorPerm";
}else{
if(checkValid(demanda, ocorrencia)){
df.executeInsert(demanda);
demanda.setAnexo(uploadFileName);
lFac.executeInsert("INCLUSAO",(Usuario)session.get("usuario"),"DEMANDA",demanda.getIdDemanda());
demanda = df.createOne(demanda);
if(upload!=null && !upload.equals("")){
copy();
}
ocorrencia.setDemanda(demanda);
ocorrencia.setUsuario((Usuario)session.get("usuario"));
of.executeInsert(ocorrencia);
lFac.executeInsert("INCLUSAO",(Usuario)session.get("usuario"),"OCORRENCIA",ocorrencia.getIdOcorrencia());
return "incluirDemanda";
}else{
errorLog = true;
refreshFormDemanda();
return "missInfo";
}
}
}
public String download(){
try {
fileInputStream = new FileInputStream(new File(caminho));
contentDisposition = "attachment; filename=" + uploadFileName;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "download";
}
public void copy(){
try {
String filePath = servletContext.getRealPath("/Files/");
String newFileName;
if(uploadFileName.substring(uploadFileName.length()-4,uploadFileName.length()-3).equals("")){
newFileName = uploadFileName.substring(0, uploadFileName.length()-4)+"-"+demanda.getIdDemanda()+uploadFileName.substring(uploadFileName.length()-4);
}else{
newFileName = uploadFileName.substring(0, uploadFileName.length()-5)+"-"+demanda.getIdDemanda()+uploadFileName.substring(uploadFileName.length()-5);
}
File fileToCreate = new File(filePath, newFileName);
FileUtils.copyFile(this.upload, fileToCreate);
demanda.setCaminho(filePath+"\\"+newFileName);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
}
}
//GETTERS E SETTERS
}