繁体   English   中英

如何使用我从中获得输入流的临时文件

[英]How to use temp file from which i get inputStream

我正在尝试使用Rest Web Service将文件上传到Web。 我创建了一个流程,该流程接受以下形式的文件:

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(

        @FormDataParam("file") InputStream uploadInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetails,
        @FormDataParam("location") String uploadedFileLoc   ) {
        .
        .
}

在此方法的末尾,我需要返回客户端上传的文件,在下一个方法中,该文件将转换为字节数组。 基本上我以“ InputStream”格式获取文件。 我可以通过某种方式自己获取文件吗?

@Ravi Kumar

您可以使用

public Response uploadFileSimple(@FormDataParam("file") File file, @FormDataParam("file") FormDataContentDisposition formData)

它直接返回一个File对象,但是它会为您自己在temp中自动创建File对象,因此您可以说有两个大小相同的temp文件。

First MIMExxxxx.tmp from where you get you input steam

Second re234xxxx  from which you get this file object

NOTE :在调用资源方法jersey之前,需要花一些时间来创建这两个文件(根据文件大小)。

而且,如果您想直接使用MIMExx.tmp文件,那么我不认为有直接的方法可以使用JAVA REFLECTION获取临时文件的路径并使用。

使用Java Reflection:

@Path("/upload/{parentfolderid}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFileWithPart(FormDataMultiPart form)
    {

        try
        {
            FormDataBodyPart filePart = form.getField("upload");
            BodyPartEntity bodyPart = (BodyPartEntity) filePart.getEntity();

            MIMEPart mimePart = (MIMEPart) readFieldValue("mimePart", bodyPart);
            Object dataHead = readFieldValue("dataHead", mimePart);
            Object dataFile = readFieldValue("dataFile", dataHead);
            File tempFile = null;
            if (dataFile != null)
            {
                Object weakDataFile = readFieldValue("weak", dataFile);
                tempFile = (File) readFieldValue("file", weakDataFile);
            }
            else
            {
                tempFile = filePart.getValueAs(File.class);
            }

    // Here is your *tempFile*, Do what ever you want with it
}

private static Object readFieldValue(String fieldName, Object o) throws Exception
{
    Field field = o.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(o);
}

文件是文件,流是流。

如果您需要流中的文件,则需要创建文件并将流写入文件中。

您可以为此使用temp文件夹: 如何在Java中创建一个临时文件而不将随机数附加到文件名中?

并在那里创建一个临时文件。

UPD

对于您的需求,File.createTempFile()应该足够了。

您可以使用Jersey REST API上传文件。 例如,

@Path("/upload")
public class FileUploadService{
      @POST

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

      String location = "/" + fileDetail.getFileName();

    // save it
    writeToFile(uploadedInputStream, location);

    String output = "File uploaded to : " + location;

    return Response.status(200).entity(output).build();

      }

}

并使用以下方法保存文件:

private void writeToFile(InputStream uploadedInputStream,
    String location) {

    try {
        OutputStream out = new FileOutputStream(new File(
                location));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File(location));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

希望能帮助到你!

谢谢大家的建议。 我需要做的主要事情是将文件转换为字节数组。 由于我正在获取InputStream,因此可以将其作为返回到下一个流的传递,而不是等待文件本身。 在下一个流程中,而不是使用“文件到字节数组”转换器,只需使用“对象到字节数组”转换器。 终于可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM