views:

157

answers:

1

Hi

I'm new to Java Web Services, so I might be doing things wrong.

I'm trying to transfer a file using the DataHandler - this is what I've got:

Web Service:

import java.net.MalformedURLException;
import java.net.URL;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlMimeType;

/**
 *
 * @author pc1
 */
@WebService()
public class WSFileSender {

    @WebMethod( operationName = "getfile" )
    public @XmlMimeType( "application/octet-stream" ) DataHandler getfile( @WebParam( name = "path" ) String path ) {

        DataHandler datahandler = null;

        try {
            datahandler = new DataHandler( new URL( path ) );
        }
        catch ( MalformedURLException e ) {
            System.out.println( "Bad" );
        }

        return datahandler;
    }

}

Client:

package fileclient;

import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;

/**
 *
 * @author pc1
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main( String[] args ) {

        try {
            fspg.WSFileSenderService service = new fspg.WSFileSenderService();
            fspg.WSFileSender port = service.getWSFileSenderPort();

            DataHandler handler = port.getfile( "FileSender/file.jpg" );

            OutputStream out = new FileOutputStream( "dest.jpg" );

            handler.writeTo( out );

            out.close();

            System.out.println( "Done" );

        } catch (Exception ex) {
        // TODO handle custom exceptions here
    }

    }

}

It seems, as if everything is being done correctly, but the file created is empty - what am I doing wrong?

================= EDIT ==================

The DataHandler object returned by getfile() is null - is it not possible to return this object from a webservice?

A: 

If the DataHandler returned is null my guess would be something goes wrong in that method (e.g. the MalformedURLException you are catching). If not, you could try to create the DataHandler in a different way, e.g. with a FileDataSource or a ByteArrayDataSource.

Fabian Steeg
FileDataSource works fine, thanks
zbigh