使用rest服务将zip文件作为八位字节流内容类型上传时,zip文件的大小会受到影响

时间:2019-07-04 07:25:33

标签: java

我的休息界面是:

@POST
@Path ("/xy/v1/xyz")
@Consumes({ MediaType.APPLICATION_OCTET_STREAM})
@Produces({ MediaType.APPLICATION_JSON })
Response import(InputStream fileInputStream, @Context HttpHeaders headers,
                @Context HttpServletRequest httpServletRequest,
                @Context UriInfo info) {

    try (ZipInputStream zipInputStream = new
         ZipInputStream(fileInputStream))
    {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null)
        {
            if(!zipEntry.isDirectory())
            {
                data= new String(IOUtils.toByteArray(zipInputStream));
                fileName = zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1);
                list.add(
                         //...
                                  );
            }
            zipInputStream.closeEntry();
            zipEntry = zipInputStream.getNextEntry();
        }
    }
    catch (IOException e)
    {
        LOG.error("Exceptions occured while reading",e);
    }
}

如果我使用APPLICATION_OCTET_STREAM,则会得到zipEntry null 内容类型。

是否可以使用上述其余端点配置来上传zipfile?

1 个答案:

答案 0 :(得分:0)

应用八位字节流提供文件参数

@POST
@Path("upload")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_PLAIN)
public String upload(File f) {
    // ... The file contains the data uploaded
    return "ok";
}