I want to upload a file to the server from a java client but receive a (415) error code

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

I want to upload a file to the server from a java client but receive a (415) error code

ShiYu Gao
I don't want to use the FileUpload extension and the HTML. I use the post method to do the stuff. But i got the message below:

Unsupported Media Type (415) - The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method

My Resetlet version is J2EE 2.07.

And the code below is the post method and the client:

/********************************

@Post
         public FileRepresentation receive(FileRepresentation  file){
                         try {
                                file.write(new FileOutputStream(new
                                                  File("/tmp/myfile.txt")));
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                return file;
         }
*********************************/

the client:

/**********************************

ClientResource cr = new ClientResource("http://211.71.75.133:8182/testFileUpload");
File file  = new File("/var/temp/file.txt");
               
 FileRepresentation fileEntity = new FileRepresentation(file, MediaType.TEXT_PLAIN);
           
            Form fileForm = new Form();
            fileForm.add(Disposition.NAME_FILENAME,file.getName());
            Disposition disposition = new Disposition(Disposition.TYPE_INLINE, fileForm);
            fileEntity.setDisposition(disposition);
            try{
            cr.post(fileEntity);
           // resource.receive(fileEntity);
           
            }
            catch(Exception e){
            System.out.print("dd");
            e.printStackTrace();
            }

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2682066
Reply | Threaded
Open this post in threaded view
|

RE: I want to upload a file to the server from a java client but receive a (415) error code

Thierry Boileau
Hello,

in your case, you don't need to use a FileRepresentation on server side.
You can just replace the type of the parameter:
@Post
public FileRepresentation receive(Representation file){
[...]
}

Having said that, you still have access to the Representation#disposition attribute on server side.

Best regards,
Thierry Boileau

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2682443
Reply | Threaded
Open this post in threaded view
|

RE: I want to upload a file to the server from a java client but receive a (415) error code

ShiYu Gao
Hello,

I have use the Representation as the argument. But the program is always running in the client side, it never stop.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2684323
Reply | Threaded
Open this post in threaded view
|

RE: I want to upload a file to the server from a java client but receive a (415) error code

ShiYu Gao
In reply to this post by Thierry Boileau
Hello,

I change some code in the server side and debugged it.

code:
/***********************

 public FileRepresentation receive(Representation  file){
                         try {
                       
                               
                               
                                InputStream is = file.getStream();
                                FileOutputStream fo = new FileOutputStream(new
                                                  File("/usr/temp/file2.txt"));
                               
                                byte[] b = new byte[1024];
                                while(is.read(b)>=0){
                                        fo.write(b);
                                         b = new byte[1024];
                                         fo.flush();
                                }
                               
                                is.close();
                                fo.close();
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                return null;
         }

**********************/

it paused at the is.read(b)>=0 place.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2684328
Reply | Threaded
Open this post in threaded view
|

RE: I want to upload a file to the server from a java client but receive a (415) error code

Thierry Boileau
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
Reply | Threaded
Open this post in threaded view
|

Re: I want to upload a file to the server from a java client but receive a (415) error code

Xavier Méhaut
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 <[hidden email]>
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


Reply | Threaded
Open this post in threaded view
|

RE: I want to upload a file to the server from a java client but receive a (415) error code

ShiYu Gao
In reply to this post by Thierry Boileau
Hello,

Thank you for you help. I have changed the Restlet to j2se 2.0.0 and the problem resolved. Thanks again.

Best regards,
SHIYU Gao

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