如何将通用文件发送到球衣服务并正确接收?

发布于 2024-12-25 15:13:36 字数 2319 浏览 0 评论 0原文

我正在开发一项使用 Dropbox API 的 Jersey 服务。

我需要将通用文件发布到我的服务(该服务将能够管理每种类型的文件,就像使用 Dropbox API 一样)。

客户端

因此,我实现了一个简单的客户端:

  • 打开文件,
  • 创建到 URL 的连接,
  • 设置正确的 HTTP 方法,
  • 创建 FileInputStream 并写入使用字节缓冲区连接的输出流上的文件。

这是客户端测试代码。

public class Client {

  public static void main(String args[]) throws IOException, InterruptedException {
    String target = "http://localhost:8080/DCService/REST/professor/upload";
    URL putUrl = new URL(target);
    HttpURLConnection connection = (HttpURLConnection) putUrl.openConnection();

    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("content-Type", "application/pdf");

    OutputStream os = connection.getOutputStream();

    InputStream is = new FileInputStream("welcome.pdf");
    byte buf[] = new byte[1024];
    int len;
    int lung = 0;
    while ((len = is.read(buf)) > 0) {
      System.out.print(len);
      lung += len;
      is.read(buf);
      os.write(buf, 0, len);
    }
  }
}

服务器端

我有一个方法:

  • 获取一个InputStream作为参数,
  • 创建一个与原始文件具有相同名称和类型的文件。

以下代码实现了接收特定PDF文件的测试方法。

@PUT
@Path("/upload")
@Consumes("application/pdf")
public Response uploadMaterial(InputStream is) throws IOException {
  String name = "info";
  String type = "exerc";
  String description = "not defined";
  Integer c = 10;
  Integer p = 131;
  File f = null;
  try {
    f = new File("welcome.pdf");

    OutputStream out = new FileOutputStream(f);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0)
      out.write(buf, 0, len);
    out.close();
    is.close();
    System.out.println("\nFile is created........");
  } catch (IOException e) {
    throw new WebApplicationException(Response.Status.BAD_REQUEST);
  }

  //I need to pass a java.io.file object to this method
  professorManager.uploadMaterial(name, type, description, c,p, f);

  return Response.ok("<result>File " + name + " was uploaded</result>").build();
}

此实现仅适用于文本文件。如果我尝试发送一个简单的 PDF,收到的文件将不可读(在我将其保存到磁盘上之后)。

我怎样才能满足我的要求?有人可以建议我解决方案吗?

I'm developing a Jersey service that uses Dropbox's API.

I need to post a generic file to my service (the service would be able to manage every kind of file as well as you can do with the Dropbox API).

Client Side

So, I've implemented a simple client that:

  • opens the file,
  • creates a connection to the URL,
  • sets the correct HTTP method,
  • creates a FileInputStream and writes the file on the connection's outputstream using byte buffer.

This is the client test code.

public class Client {

  public static void main(String args[]) throws IOException, InterruptedException {
    String target = "http://localhost:8080/DCService/REST/professor/upload";
    URL putUrl = new URL(target);
    HttpURLConnection connection = (HttpURLConnection) putUrl.openConnection();

    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("content-Type", "application/pdf");

    OutputStream os = connection.getOutputStream();

    InputStream is = new FileInputStream("welcome.pdf");
    byte buf[] = new byte[1024];
    int len;
    int lung = 0;
    while ((len = is.read(buf)) > 0) {
      System.out.print(len);
      lung += len;
      is.read(buf);
      os.write(buf, 0, len);
    }
  }
}

Server Side

I've a method that:

  • gets an InputStream as an argument,
  • creates a file with the same name and type of the original file.

The following code implements a test method to receive a specific PDF file.

@PUT
@Path("/upload")
@Consumes("application/pdf")
public Response uploadMaterial(InputStream is) throws IOException {
  String name = "info";
  String type = "exerc";
  String description = "not defined";
  Integer c = 10;
  Integer p = 131;
  File f = null;
  try {
    f = new File("welcome.pdf");

    OutputStream out = new FileOutputStream(f);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0)
      out.write(buf, 0, len);
    out.close();
    is.close();
    System.out.println("\nFile is created........");
  } catch (IOException e) {
    throw new WebApplicationException(Response.Status.BAD_REQUEST);
  }

  //I need to pass a java.io.file object to this method
  professorManager.uploadMaterial(name, type, description, c,p, f);

  return Response.ok("<result>File " + name + " was uploaded</result>").build();
}

This implementation works only with text files. If I try to send a simple PDF the received file is not readable (after I've saved it on disk).

How can I satisfy my requirements? Could anyone suggest me solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2025-01-01 15:13:37

您的客户端代码有错误。

while ((len = is.read(buf)) > 0) {
  ...
  is.read(buf);
  ...
}

您在每次迭代中都会从 InputStream 读取两次。从循环体中删除 read 语句就可以了。

您还说过问题中提供的代码适用于文本文件。我认为这也行不通。从您尝试上传的文件中读取两次意味着您只上传了一半的内容。一半的文本文件仍然是文本文件,但一半的PDF只是垃圾,所以你无法打开后者。您应该仔细检查上传和保存的文本文件的内容是否与原始文件相同。

You're client code is faulty.

while ((len = is.read(buf)) > 0) {
  ...
  is.read(buf);
  ...
}

You're reading from the InputStream twice in every iteration. Remove the read statement from the loop's body and you'll be fine.

You've also said that the code presented in your question works with text files. I think that doesn't work either. Reading twice from the file you're trying to upload means you're uploading only half of its contents. Half a text file is still a text file, but half a PDF is only rubbish, so you can't open the latter. You should have double checked if the contents of your uploaded and saved text file is the same as the original.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文