The jersy Service sends images to another server and returns them asynchronously.

Source: Internet
Author: User

The jersy Service sends images to another server and returns them asynchronously.

Today, when I was learning about a new project, I met the jersy service and completed it. I sent the image to another server. The following is a simple example of the jersy service.

1. Create a java project of jersy and import the corresponding jar package of the jersy service first.

Commons-io-1.3.2.jar

Jersey-client-1.18.1.jar

Jersey-core-1.18.1.jar

2. Create another tomcat server. As an image server, set <servlet> Settings in tomcat/conf/web. xml


<Servlet>
<Servlet-name> default </servlet-name>
<Servlet-class> org. apache. catalina. servlets. DefaultServlet </servlet-class>


  <init-param> <param-name>readonly</param-name> <param-value>false</param-value>  </init-param>
</servlet>

 

This server is readable and writable.

3. Change the servlet. xml port in tomcat to three ports different from the local tomcat port.

4. Create a javaweb project, image-web project, create an upload file, and deploy the imge-web file to another tomcat

5. Create a JersyDemo. java in jersy

1 package jersy; 2 3 import java. io. file; 4 import java. io. IOException; 5 6 import org. apache. commons. io. fileUtils; 7 8 import com. sun. jersey. api. client. client; 9 import com. sun. jersey. api. client. webResource; 10 11 public class JersyDemo {12 public static void main (String [] args) throws IOException {13 // instantiate a jersy14 Client client = new Client (); 15 // Request Path of another server 16 String url = "http: // localhost: 8088/image-web/upload/qqq.jpg "; 17 // set the Request Path 18 WebResource resource = client. resource (url); 19 // local path 20 String path = "C: \ 1.jpg"; 21 // read the image to the memory 22 byte [] readFileToByteArray = FileUtils. readFileToByteArray (new File (path); 23 // start sending. put is used and post is generally used. put24 resource is used for get. put (String. class, readFileToByteArray); 25 System. out. println ("sent"); 26 27} 28 29}

 

  

In this way, the test is successful, and the image is successfully sent to another tomcat. This knowledge only publishes local images to another server. In actual development, the image in the server is also returned asynchronously.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.