Java Web 服务将文件传输到本地系统

发布于 2024-12-10 11:38:02 字数 297 浏览 0 评论 0原文

我想用两种方法在java中创建一个Web服务

1)通过返回本地URL将文件从互联网传输到本地文件服务器

2)通过获取url从同一服务器检索文件

注意:它应该与所有格式

必须使用 Java Web 服务..

任何类型:字节数组、十六进制或 MIME 类型 传输都可以

附件大小为 4mb..

我无法直接连接到数据库,因为应用程序部署在 DMZ 上,并且仅有的我可以使用 Web 服务连接到 Intranet 中的文件服务器。

与文件服务器的连接已经完成。

I want to create a web service in java with two methods

1) to transfer a file from the internet to a local file server by returning local URL

2) to retrieve the file from the same server by taking the url

Note: It should work with all the formats

Its mandatory to use Java Web service..

any Type : Byte Array , Hexadecimal or MIME Type Transfer is OK

The size of the attachment is 4mb..

I can not connect to database directly because the application is deployed on DMZ and the only way I can connect to the file server in Intranet is by using Webservices.

Connection to the fileserver is already done..

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

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

发布评论

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

评论(3

我恋#小黄人 2024-12-17 11:38:03

类似的场景:
是这个,他用一个简短的代码进行了解释

A similar scenario:
is this and he has explained using a short code

晨敛清荷 2024-12-17 11:38:03

如果您的主要问题是找到通过 Java 中的 Web 服务轻松传输文件的技巧,我会推荐 Hessian 服务,该服务在 Hessian with Large Binary Data (java) 发布在 SO 上。那里的链接提供了实现一种文件传输的示例。

如果您不想过多地搞乱 Web 服务本身的逻辑,那么 Hessian 是一个很好的解决方案。快速查看 Hessian 代码,您甚至不会意识到自己正在使用它。这是一个重量轻的解决方案。

Stefan 有一个解决方案,您可以在其中了解 Web 服务逻辑的大部分内容,因此这取决于您想要拥有多高的抽象级别。如果此任务的目的是展示如何使用 Web 服务而不仅仅是使其正常工作,那么 Stefan 就有了答案。

关于文件上传等,您想从互联网保存文件。看这个:如何下载和保存文件使用 Java 从 Internet 获取?。它使用纯 Java,并且在我的理解中不需要任何 Web 服务来完成给定的任务,但是如果将这两者结合起来,您会得到非常容易工作的东西!

If your main problem is to find tips for easy file transfer over a web service in java, I would recommend the Hessian service, discussed on Hessian with large binary data (java) post on SO. The link there goes for an example that implements one kind of file transfering.

Hessian is a good solution, if you don't want to mess up too much with the logic of the web services itself. Looking a Hessian code rapidly you would not even recognize you are using one. It is so light weight solution.

Stefan has a solution, where you get pretty much inside the Web Services logics, so it is up to you how high abstraction level you want to have. If the point of this task is to show how to use web services and not just get it work, then Stefan has the answer.

About file upload and such, you wanted to save a file from internet. Look this: How to download and save a file from Internet using Java?. This uses pure Java and in my understanding does not need any web services to accomplish the given task, but if you combine these two you get something that works very easily!

Saygoodbye 2024-12-17 11:38:02

由于您已使用 soap 标记了这个问题,因此我假设您需要 Java 中的 SOAP Web 服务。这也使得 JAX-WS(用于 XML Web 服务的 Java API)成为该库的自然选择使用。 Java(TM) Web 服务教程 将更详细地涵盖您的问题。

现在,您需要实现获取图像并返回 URL 的逻辑,以及获取 URL 并返回图像的逻辑。

@WebService
public class MyJavaWebService {
    @WebMethod
    public String takeImage(byte[] image, String imageName) {
        //You'll need to write a method to save the image to the server.
        //How you actually implement this method is up to you.  You could
        //just open a FileOutputStream and write the image to a file on your
        //local server.
        this.saveImage(image, imageName);
        //Here is where you come up with your URL for the image.
        return this.computeURLForImage(imageName);
    }
    @WebMethod
    public byte[] getImage(String url) {
        final byte[] loadedImage = this.getImage(url);
        return loadedImage;
    }
}

您可能还需要设置一些额外的配置,如部署 Metro Endpoint 中所述。本文的要点是,您需要将 sun-jaxws.xml 文件添加到表单的 WEB-INF/ 文件夹中

<endpoints
        xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
        version="2.0">
    <endpoint
            name="MyJavaWebService"
            implementation="com.mycompany.MyJavaWebService"
            url-pattern="/MyJavaWebService"/>
</endpoints>

,并添加一些 JAX-WS 内容到您的 web.xml 文件中,如下所示:

<web-app>
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>MyJavaWebServiceServlet</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyJavaWebServiceServlet</servlet-name>
        <url-pattern>/MyJavaWebService</url-pattern>
    </servlet-mapping>
</web-app>

最后,将所有内容打包到 .war 文件中并将其部署到 Java Web 服务器(例如 Tomcat)。

Since you've tagged this question with soap, I'm going to assume you want a SOAP web service in Java. This also makes JAX-WS (the Java API for XML Web Services) a natural choice for the library to use. The Java(TM) Web Services Tutorial will cover your problem in greater detail.

Now you're going to need to implement the logic to take images and return URLs, and take URLs and return images.

@WebService
public class MyJavaWebService {
    @WebMethod
    public String takeImage(byte[] image, String imageName) {
        //You'll need to write a method to save the image to the server.
        //How you actually implement this method is up to you.  You could
        //just open a FileOutputStream and write the image to a file on your
        //local server.
        this.saveImage(image, imageName);
        //Here is where you come up with your URL for the image.
        return this.computeURLForImage(imageName);
    }
    @WebMethod
    public byte[] getImage(String url) {
        final byte[] loadedImage = this.getImage(url);
        return loadedImage;
    }
}

You'll also probably need to set up some additional configuration as described in Deploying Metro Endpoint. The gist of the article is that you need to add a sun-jaxws.xml file to your WEB-INF/ folder of the form

<endpoints
        xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
        version="2.0">
    <endpoint
            name="MyJavaWebService"
            implementation="com.mycompany.MyJavaWebService"
            url-pattern="/MyJavaWebService"/>
</endpoints>

And also add some JAX-WS stuff to your web.xml file like so:

<web-app>
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>MyJavaWebServiceServlet</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyJavaWebServiceServlet</servlet-name>
        <url-pattern>/MyJavaWebService</url-pattern>
    </servlet-mapping>
</web-app>

Finally, package everything up into a .war file and deploy it to a Java web server (e.g. Tomcat).

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