泽西文件上传

时间:2012-06-21 15:24:40

标签: file file-upload jersey inputstream

我尝试将文件上传到我的泽西服务器,但是出现了错误。

 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) {
        e.printStackTrace();
    }

    InputStream is = null;

    try {
        is = new FileInputStream(file);
        is.close(); 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    FormDataMultiPart part = new FormDataMultiPart().field("file", is, MediaType.TEXT_PLAIN_TYPE);
    System.out.println(is);
    System.out.println(tenant1.getTenantId());
    System.out.println(part);
    String response = service.path("rest").path("file").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);

Syso不为空。所以文件被写入输入流。

错误:

ClientHandlerException: java.io.IOException: ReadError , when I send it to the server.

服务器端:

@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postFileTenant(@FormDataParam("file") InputStream stream) throws IOException {

    // save it

    return Response.ok(IOUtils.toString(stream)).build();
}

1 个答案:

答案 0 :(得分:1)

您正在传递一个已关闭的InputStream,因此很明显Jersey运行时无法读取它。从代码中删除is.close();行。