如何将文件从REST客户端传输到REST服务器?

时间:2013-03-14 07:28:36

标签: java rest

我正在使用REST服务进行文件传输过程。我在Java中使用REST Jersey API。我没有使用任何HTML帖子或通过ajax帖子(即我没有创建任何Web应用程序)执行此过程。我刚刚创建了一个REST Http Server,我使用一个REST Client来处理文件传输操作。 Web资源方法位于单独的类文件中。

testREST.java

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).build();
}

RESTClient.java

try {
    final ClientConfig config = new DefaultClientConfig();
    final Client client = Client.create(config);
    final WebResource resource = client.resource(REST_URI);

    File f = new File("D:/test/Doc1.rar");
    FileInputStream fs = new FileInputStream(f);
    byte[] con= new byte[(int)f.length()];
    fs.read(con);
    FormDataMultiPart form = new FormDataMultiPart();
    FormDataBodyPart fdp = new FormDataBodyPart("content", MediaType.MULTIPART_FORM_DATA);
    form.bodyPart(fdp);
    final String response = resource.path("test").path("transfer").type(MediaType.MULTIPART_FORM_DATA).post(String.class, form);
} catch (Exception ex) {
    System.out.println("Exception Occcured");
    ex.printStackTrace();
}

但是在启动REST服务器本身的时候,我得到的异常就像testREST.java中没有有效的资源方法....我猜这里使用FormDataParam ..这就产生了问题......你能不能请任何人帮助我指出我的代码有什么问题。

0 个答案:

没有答案