Hello Thierry,

Finally, I found aa means to do it like below :
regards
Xavier
*
client side :*


public class MyUploadClient {

    public static void main(String[] args) throws Exception {
        FileInputStream fileStream = new FileInputStream(new File(
                "F:/workspacesEclipse/easystorage/EssaiUpload/essai.tar"));
        InputStreamBody streamBody = new InputStreamBody(fileStream,
                "application/octet-stream", "file.tar");

        StringBody comment = new StringBody("A binary file of some kind",
                Charset.forName("UTF-8"));
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("comment", comment);
        reqEntity.addPart("bin", streamBody);

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("
http://localhost:8182/testFileUpload";);
        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            resEntity.consumeContent();
        }
        httpclient.getConnectionManager().shutdown();

        ClientResource client = new ClientResource("
http://localhost:8182/testFileUpload";);
        Representation rep = client.get();
        if (rep != null) {
            BufferedInputStream in = new
BufferedInputStream(rep.getStream());
            File file = new
File("F:/workspacesEclipse/easystorage/EssaiUpload/essaidownload.tar");
            BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(file));
            byte[] buf = new byte[10240];
            int size=0;
            while((size = in.read(buf))!=-1) {
                out.write(buf,0,size);
            }
            out.close();
            in.close();
    }
    }
}


*server side*

import java.io.File;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.fileupload.RestletFileUpload;
import org.restlet.representation.FileRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;



public class UploadResource extends ServerResource  {

    private static final String TARBALLPATH =
"F:/workspacesEclipse/easystorage/EssaiUpload/essaicopie.tar";

    @Get
    synchronized public FileRepresentation download() {
        FileRepresentation rep = new FileRepresentation(new
File(TARBALLPATH), MediaType.APPLICATION_TAR);

        setStatus(Status.SUCCESS_OK);
        return rep;
    }



    @Post
    synchronized public String upload(Representation entity) throws
Exception   {
        if (entity == null) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return "";}

        if (!MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(),
                true)) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return "";
        }

         // 1/ Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(1000240);

        // 2/ Create a new file upload handler based on the Restlet
        // FileUpload extension that will parse Restlet requests and
        // generates FileItems.
        RestletFileUpload upload = new RestletFileUpload(factory);

        // 3/ Request is parsed by the handler which generates a
        // list of FileItems
        List<FileItem> items = upload.parseRepresentation(entity);

        // Process only the uploaded item called "fileToUpload" and
        // save it on disk
        for (FileItem fi : items) {
            if (fi.getFieldName().equals("bin")) {
                File file = new File(TARBALLPATH);
                fi.write(file);
            }
        }

        setStatus(Status.SUCCESS_OK);
        return "";
    }



}


2010/11/25 Thierry Boileau <[email protected]>

> Hello,
>
> I send you a sample jee project that works for me. It uses the core module
> and servlet extension of Restlet 2.0.3. I hope this helps.
>
> Here is the server code:
> @Post
> public FileRepresentation receive(Representation file) throws Exception {
>  File fichier = new File("d:\\test.txt");
>  file.write(new FileOutputStream(fichier));
>  return new FileRepresentation(fichier, file.getMediaType());
> }
>
> Best regards,
> Thierry Boileau
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2685605
>
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2685632

Reply via email to