Problemas fileUpload + Webwork

index.vm (velocity)

<form name="frm" method="post" action="upload.action" enctype="multipart/form-data"
	<center>
      <table width="350" border="0" cellpadding="3" cellspacing="0">
      <tr>
        <td colspan="2"><input type="file" name="upload" size="50"/></td>
      </tr>
      <tr>
        <td colspan="2" align="center">
          <input type="submit" value="Submit">
        </td>
      </tr>
      </table>	
</form>

Actions.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import com.opensymphony.xwork.ActionSupport;

public class Actions extends ActionSupport {

	private File file;
    private String contentType;
    private String filename;

	
	public String index(){
		return SUCCESS;
	}
	public String upload() throws IOException{
		copyFile(file, new File("C:/temp/" + filename));
		return SUCCESS;
	}
	
	public void copyFile(File in, File out) throws IOException {
		 FileChannel sourceChannel = new FileInputStream(in).getChannel();
		 FileChannel destinationChannel = new FileOutputStream(out).getChannel();
		 sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
		 sourceChannel.close();
		 destinationChannel.close();
		}
	
	public void setUpload(File file) {
	     this.file = file;
	    }
	    public void setUploadContentType(String contentType) {
	     this.contentType = contentType;
	    }
	    public void setUploadFileName(String filename) {
	     this.filename = filename;
	    }
 
}

Fragmentos do xwork.xml

<action name="upload" class="Actions" method="upload"> <interceptor-ref name="fileUpload"/> <result name="success">/vm/upload.vm</result> </action>

webwork.properties

webwork.multipart.parser=jakarta

Erro =(

13:49:17.189 INFO   [SocketListener0-1] com.opensymphony.webwork.dispatcher.ServletDispatcher.getSaveDir(ServletDispatcher.java:434) >17> Unable to find 'webwork.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
13:49:17.610 INFO   [SocketListener0-1] com.opensymphony.webwork.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:116) >19> file upload image/gif marios_080.gif C:\DOCUME~1\MARIOS~1.NEY\CONFIG~1\Temp\Jetty_0_0_0_0_8080__pj\upload__43e398aa_10fee03b3a4__8000_00000000.tmp
13:49:17.610 ERROR! [SocketListener0-1] com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:281) >16> Could not execute action
java.lang.NullPointerException
	at java.io.FileInputStream.<init>(Unknown Source)
	at Actions.copyFile(Actions.java:25)
	at Actions.upload(Actions.java:20)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:300)
	at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:166)
	at com.opensymphony.webwork.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:135)
	at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:164)
	at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:116)
	at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:272)
	at com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:237)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
	at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:473)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)
	at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
	at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:633)
	at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
	at org.mortbay.http.HttpServer.service(HttpServer.java:909)
	at org.mortbay.http.HttpConnection.service(HttpConnection.java:816)
	at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:982)
	at org.mortbay.http.HttpConnection.handle(HttpConnection.java:833)
	at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:244)
	at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
	at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)

Alguem pode me dar uma luz ? shauhsauhs… ja tentei de tudo quanto é jeito e nada…

Cara, seguinte, eu acho que o provável problema é o “/” que você está usando no método upload.

public String upload() throws IOException{
      copyFile(file, new File("C:/temp/" + filename));
      return SUCCESS;
}

Utilize a seguinte linha e tente novamente.

public String upload() throws IOException{
      copyFile(file, new File("C:"+File.separator+"temp"+File.separator+filename));
      return SUCCESS;
}

intão cara… eu alterei aqui essa parte do codigo… o problema… é que ele não chega nem ai e da o erro… ele da erro porque o file vem null… ou seja não vem… mais eu não estou conseguindo fazer o file vir…

obrigadoo

Seguinte cara, eu faço isso usando interceptor.

no xwork

[code]
<interceptor class="interceptor.FileUploadInterceptor" name="fileUpload" />
<interceptor-stack name="MyFileUploadStack">

		&lt;interceptor-ref name=&quot;fileUpload&quot;/&gt;
			&lt;interceptor-ref name=&quot;defaultStack&quot;/&gt;				
		&lt;/interceptor-stack&gt;[/code]

Depois na sua action você chama o interceptor que você vai utilizar.

Detalhe… essa classe FileUploadInterceptor você pode baixar no site do webwork, já é uma classe pronta do webwork.

Espero ter ajudado… falow