Opa pessoal beleza?
Estou tendo problemas ao tentar enviar meus arquivos para o servidor.
vou tentar me explicar:
Possuo uma página JSP que tem o seguinte trecho de código:
[code]<s:form action="/painel/upload!send.java" enctype=“multipart/form-data” method=“post”>
Nome do Arquivo: <s:textfield id="arquivo.nome" name="arquivo.nome"></s:textfield>
Arquivo: <s:file name="arquivo.upload"></s:file>
Ano: <s:textfield id="arquivo.ano" name="arquivo.ano"></s:textfield>
Semestre: <s:textfield id="arquivo.semestre" name="arquivo.semestre"></s:textfield>
Matéria: <sx:autocompleter id="materia" name="materia" value="materia" listKey="nome" list="disciplinas" listValue="nome" />
<s:submit></s:submit>
</s:form>[/code]
Na minha Action tenho o uma variavel de instância chamada Arquivo que recebe os parâmetros.
dentro de arquivo tenho 3 propriedades que o Struts seta para mim:
[code] @Transient
private File upload;
@Transient
private String uploadFileName;
@Transient
private String uploadContentType;[/code]
Continuando na action tenho um método chamado" send " que pega esse arquivo e salva ele em disco
Abaixo o método:
[code]public String send(){
ArquivoUtil.saveFile(arquivo);
//arquivoDao.save(arquivo);
disciplinas = materiaService.getAll();
return "adicionarArquivo";
}[/code]
Esse método send faz uma chamada à saveFile que segue abaixo:
[code]public static String saveFile(Arquivo arquivo) {
ServletContext sContext = ServletActionContext.getServletContext();
String diretorio = sContext.getRealPath("/arquivos");
String fileName = arquivo.getNome();
if(arquivo.getUploadContentType().contains("powerpoint")){
fileName += ".ppt";
}else if (arquivo.getUploadContentType().contains("word")){
fileName += ".doc";
}else if((arquivo.getUploadContentType().contains("pdf"))){
fileName += ".pdf";
}else{
fileName += ".txt";
}
try {
File file = new File(diretorio + "\\" + fileName); // Caminho que quero salvar
byte[] tempBytes = getBytesFromFile(arquivo.getUpload()); // Bytes do Arquivo temporário
FileOutputStream ouputStream = new FileOutputStream( file );
ouputStream.write(tempBytes, 0, tempBytes.length);
ouputStream.flush();
ouputStream.close();
return fileName;
} catch (IOException e) {
return fileName;
}
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
is.close();
return bytes;
}
[/code]
Quando olho na pasta arquivos/nome.extensao a pasta está vazia, alguem pode me ajudar?agradeço desde já