Search code examples
gwtservletsfile-uploadappletgwt-rpc

how to send a file from an applet to an gwt server?


I'm trying to send a file from an applet to my server GWT. In another application, JSF, I would open an HTTP connection with my servlet.

How do I make whit an GWT server? I tried to insert my servlet in web.xml but it seems to be ignored.

I need to use a RemoteService? How can I do?

The following code of the applet and servlet mapping in web.xml.

URL urlDoServlet = new URL("http://192.168.3.100:8080/gwtapp/conection?action=send");    
HttpURLConnection conexaoComServlet = (HttpURLConnection) urlDoServlet.openConnection();    

conexaoComServlet.setDoOutput(true);
conexaoComServlet.setDoInput(true);
conexaoComServlet.setUseCaches(false);
conexaoComServlet.setDefaultUseCaches(false);

File doc = new File(file);
conexaoComServlet.setRequestMethod("POST");
conexaoComServlet.setRequestProperty("Content-Type", "application/octet-stream");
FileInputStream fis = new FileInputStream(doc);
BufferedInputStream bis = new BufferedInputStream(fis);

BufferedOutputStream bos = new BufferedOutputStream(conexaoComServlet.getOutputStream());
int read;
byte[] buffer = new byte[8192];
while((read = bis.read(buffer)) != -1)
{
    bos.write(buffer, 0, read);
}
bis.close();
fis.close();

bos.flush();
bos.close();

// get the answer.
ObjectInputStream ois = new ObjectInputStream(conexaoComServlet.getInputStream());
boolean bool = (Boolean) ois.readObject();
ois.close();
conexaoComServlet.getResponseMessage();
conexaoComServlet.disconnect();

<servlet>
    <servlet-name>ConectionServlet</servlet-name>
    <servlet-class>br.com.gwtapp.server.servlets.ConectionFileServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>conectionServlet</servlet-name>
    <url-pattern>/gwtapp/conection</url-pattern>
</servlet-mapping>

Solution

  • I've found a new implementation for that with caarlos0 and raduq-santos:

    public class DispatchServletModule extends ServletModule
    {
    
        @Override
        public void configureServlets()
        {
            serveMyServlet();       
        }
    
        private void serveMyServlet()
        {
            serve("proj/servlet/MyServlet").with(MyServlet.class);
        }
    }
    

    and on my applet...

    new URL(path + "proj/servlet/MyServlet");