Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: RESTful WebService and XML file transfer

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default RESTful WebService and XML file transfer

    Hi everyone,

    First, sorry for my english. Secondly, I know there are a lot of existing posts about what I am going to talk about, but as far as I've searched I found nothing (really) relevant on my problem. So here we go.

    I am currently developping a JAVA RESTful web service. The goal of this web service is to send some XML files (more precisely, some OVAL definitions) to connected clients. I have seen 4 'solutions' :

    • put all the XML file content in a String (encode this String in base64 if needed) and send the String to the client
    • put all the XML file content in a Byte[] and send the Byte[] to the client
    • use a FTP auxiliary server
    • send/receive the file by streaming on the socket (InputStream/OutputStream)

    For the moment, I've decided to choose the solution n4. You can see below some parts of my code concerning the file transfer.

    Server side:
    @GET
    @Path("/sendXML")
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    	@Path("/sendXML")
    	@Produces(MediaType.TEXT_PLAIN)
    	public String getHtml(@Context HttpServletResponse  serv) throws IOException {
    		ServletOutputStream o =serv.getOutputStream();
            try {
                FileInputStream fis = new FileInputStream(new File("xml file to send"));
                int c;
     
                while ((c = fis.read()) != -1) {
                   o.write(c);
                }
     
                fis.close();
                o.close();
            } catch (Exception e) {
                System.err.println("e");
            } 
     
    		return "OK" ;
    	}

    Client side:
    try {
                URL u = new URL("http://localhost:8080/ws.example.filetransfer/rest/sendXML");
                HttpURLConnection uc = (HttpURLConnection) u.openConnection();
     
     
                InputStream is = uc.getInputStream();
     
                try {
                    FileOutputStream fos = new FileOutputStream(new File("path where the file will be saved"));
                    int c;
     
                    while ((c = is.read()) != -1) {
                       fos.write(c);
                    }
     
     
                    is.close();
                    fos.close();
                } catch (Exception e) {
                    System.err.println("FileStreamsTest: " + e);
                } 
     
            } catch (Exception e) {
                e.printStackTrace();
            }

    I am developping and testing my programs under Eclipse. Everything seems OK, the XML file is well sent from server to client, which saves it on the disk. Here is an console output when I launch the Client program (Server is already started):

    <?xml version="1.0"?><hello> Hello Jersey</hello>
    <html> <title>Hello Jersey</title><body><h1>Hello Jersey</body></h1></html> 
     
     
    <?xml version="1.0" encoding="UTF-8"?>
    <oval_definitions xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#hpux hpux-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5">
      <generator>
        <oval:product_name>The OVAL Repository</oval:product_name>
        <oval:schema_version>5.10.1</oval:schema_version>
        <oval:timestamp>2012-06-29T05:09:57.714-04:00</oval:timestamp>
      </generator>
     
     [...]

    As you can see, when the client is receiving the XML file, it writes the content on stdout in addition to write this content on the disk (in the file which is specified in code). Writing on stdout is a minor problem, since the content is also written on disk, but is this normal, or a kind of Eclipse bug, or a bug from me ..?

    To conclude, here are my questions:

    • according to you, what is the best method to transfer XML file with a REST web service ?
    • do you think the streaming solution is a good method?
    • have I well implemented this solution (I'm in doubt with some pieces of code, like using the @Contex attribute)?

    Thanks to all for your attention and your answers!
    Heisen89


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: RESTful WebService and XML file transfer

    up up up up (4 times for the message length restriction..) ?

Similar Threads

  1. TFTP Server not allowing data transfer
    By ZX81 in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:26 AM
  2. how to transfer PPT files using socket programming
    By deepsree1417 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 2nd, 2012, 08:04 AM
  3. Creating a transfer function for a ATM simulation
    By D3C in forum Java Theory & Questions
    Replies: 10
    Last Post: August 13th, 2011, 10:20 AM
  4. Runnable to WebService
    By fran_jo in forum Threads
    Replies: 1
    Last Post: April 12th, 2011, 05:12 PM
  5. Program with transfer value from one class to another
    By Adam22 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 6th, 2011, 12:26 AM