如何将文件从小程序发送到 gwt 服务器?

发布于 2024-12-07 08:59:44 字数 1712 浏览 0 评论 0原文

我正在尝试将文件从小程序发送到我的服务器 GWT。在另一个应用程序 JSF 中,我将打开与我的 servlet 的 HTTP 连接。

如何创建 GWT 服务器?我尝试将 servlet 插入 web.xml 但它似乎被忽略。

我需要使用远程服务吗?我该怎么办?

下面的代码将applet 和servlet 映射在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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-12-14 08:59:44

尝试将 /gwtapp/conection 替换为 /conection 并告诉我们如果有效的话:)

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

围归者 2024-12-14 08:59:44

我找到了一个新的实现,使用 caarlos0raduq-santos

public class DispatchServletModule extends ServletModule
{

    @Override
    public void configureServlets()
    {
        serveMyServlet();       
    }

    private void serveMyServlet()
    {
        serve("proj/servlet/MyServlet").with(MyServlet.class);
    }
}

在我的小程序上...

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文