开发者

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

开发者 https://www.devze.com 2023-04-09 14:03 出处:网络
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.

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开发者_Python百科 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>


try replace <url-pattern>/gwtapp/conection</url-pattern> by <url-pattern>/conection</url-pattern> and tell us if that works :)


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消