Trying to upload a file to a JAX-RS (jersey) server

后端 未结 4 874
轻奢々
轻奢々 2020-12-08 03:24

I\'m trying to upload a file and other form data using multipart/form-data client with Jersey. I\'m uploading to a REST web service also using Jersey. Here is the server c

4条回答
  •  我在风中等你
    2020-12-08 04:11

    Or just write a new file and upload it:

    Writer output = null;
        File file = null;
        try {
          String text = "Rajesh Kumar";
          file = new File("write.txt");
          output = new BufferedWriter(new FileWriter(file));
            output.write(text);
            output.close();
        } catch (IOException e) {
            System.out.println("IOException e");
            e.printStackTrace();
        }
    
        InputStream is = null;
    
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException e");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException e");
            e.printStackTrace();
        }
    
        FormDataMultiPart part = new FormDataMultiPart().field("file", is, MediaType.TEXT_PLAIN_TYPE);
        res = service.path("rest").path("tenant").path(tenant1.getTenantId()).path("file").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, part);
    

提交回复
热议问题