如何在 rest api jersey 中获取文件大小
How to get size of file in rest api jersey
我已经休息了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());
我得到了正确的文件名。
尝试使用 HttpServletRequest 。请参阅文档以获取更多详细信息。
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response putFile(@Context HttpServletRequest request){
Part part = request.getPart("filename");
long fileSize = part.getSize();
}
希望这就是您想要的。我已经在我的系统中验证过了。这将以字节为单位打印文件的大小。
@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>
还将此添加到扩展资源配置的应用程序 class。这会将您的 class with jersey 注册为具有多部分内容。
super(YourClass.class, MultiPartFeature.class);
首先,我认为限制数据大小的最好方法是在服务器配置上进行设置。请参阅相关ref.
其次,由于是Stream自带的,所以一直到遇到EOF之类的。这意味着您无法首先找到尺寸。
然而,第三种判断球衣尺寸的替代方法之一是使用 HTTP header 的内容尺寸。服务器首先得到 'Content-Length' header 以了解 body 的大小。 @Context HttpServletRequest request
有 request.getContentLength()
方法。但是因为它有多个部分,所以你需要注意 body 大小包含协议开销的数据总和 (seperator/contents infos)
祝你好运
我已经休息了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());
我得到了正确的文件名。
尝试使用 HttpServletRequest 。请参阅文档以获取更多详细信息。
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response putFile(@Context HttpServletRequest request){
Part part = request.getPart("filename");
long fileSize = part.getSize();
}
希望这就是您想要的。我已经在我的系统中验证过了。这将以字节为单位打印文件的大小。
@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>
还将此添加到扩展资源配置的应用程序 class。这会将您的 class with jersey 注册为具有多部分内容。
super(YourClass.class, MultiPartFeature.class);
首先,我认为限制数据大小的最好方法是在服务器配置上进行设置。请参阅相关ref.
其次,由于是Stream自带的,所以一直到遇到EOF之类的。这意味着您无法首先找到尺寸。
然而,第三种判断球衣尺寸的替代方法之一是使用 HTTP header 的内容尺寸。服务器首先得到 'Content-Length' header 以了解 body 的大小。 @Context HttpServletRequest request
有 request.getContentLength()
方法。但是因为它有多个部分,所以你需要注意 body 大小包含协议开销的数据总和 (seperator/contents infos)
祝你好运