简体   繁体   中英

Java web service to transfer file to Local system

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..

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 (eg Tomcat).

类似的情况: 是这个 ,他已经解释了使用短代码

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM