【问题标题】:How to get size of file in rest api jersey如何在rest api jersey中获取文件大小
【发布时间】:2016-04-25 07:44:12
【问题描述】:

我已经做了一个rest api,它工作正常,但我想读取文件的大小,我使用下面的代码来读取文件的大小

@POST
@Path("/test")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload( FormDataMultiPart form ){
    System.out.println(" size of file="+ filePart.getContentDisposition().getSize());
}

但我得到了文件 -1 的大小。

谁能建议我如何读取文件的实际大小。

但是使用

System.out.println(" data name ="+ filePart.getContentDisposition().getFileName());

我得到了正确的文件名。

【问题讨论】:

  • 使用@Context HttpServletRequest request在你的方法中注入HttpServletRequest并尝试request.getContentLength()

标签: java rest jersey multipartform-data


【解决方案1】:

希望这是您想要的。我已经在我的系统中验证了它。这以字节为单位打印文件的大小。

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

    String uploadedFileLocation = "/home/Desktop/" + fileDetail.getFileName();
    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);
    File file = new File(uploadedFileLocation);
    System.out.println(file.length() + " in bytes");
    String output = "File uploaded to : " + uploadedFileLocation;
    return Response.status(200).entity(output).build();
}

// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {

    try {
        OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[1024];
        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

确保你的 pom.xml 中有以下依赖项

<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.13</version>
</dependency>

还将它添加到扩展资源配置的应用程序类中。这会将您的课程与球衣注册为具有多部分内容。

super(YourClass.class, MultiPartFeature.class);

【讨论】:

  • 使用您的解决方案,您必须先将流写入文件,然后才能确定大小。这里的挑战当然是在必须通过整个流之前知道上传的大小。
  • 如何检查接收到的每个字节的大小?
【解决方案2】:

首先,我认为限制数据大小的最佳方法是在服务器配置上进行设置。请查看相关ref.

其次,由于它是 Stream 自带的,所以它会遇到 EOF 或类似的。这意味着您一开始就找不到尺寸。

然而,第三,判断球衣大小的另一种方法是使用 HTTP 标头的内容大小。服务器首先获取“Content-Length”标头以了解正文的大小。 @Context HttpServletRequest requestrequest.getContentLength() 方法。但由于它具有多部分,因此您需要注意正文大小具有协议开销(分隔符/内容信息)的数据总和

祝你好运

【讨论】:

    【解决方案3】:

    尝试使用HttpServletRequest 。请参阅文档了解更多详细信息。

       @Consumes(MediaType.MULTIPART_FORM_DATA)
        public Response putFile(@Context HttpServletRequest request){
          Part part = request.getPart("filename");
          long fileSize = part.getSize();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 2011-08-22
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多