Java 如何使用从中获取inputStream的临时文件

Java 如何使用从中获取inputStream的临时文件,java,web-services,rest,jakarta-ee,jersey-2.0,Java,Web Services,Rest,Jakarta Ee,Jersey 2.0,我正在尝试使用RESTWeb服务将文件上载到web。我创建了一个流,它接受以下格式的文件: @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file") InputStream uploadInputStream, @FormDataParam("file") FormDataContentDis

我正在尝试使用RESTWeb服务将文件上载到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”格式。有什么方法可以作为文件本身获取吗?

文件是文件,流是流

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

您可以为此使用临时文件夹:

然后在那里创建一个临时文件

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,所以我可以将其作为返回传递到下一个流,而不是等待文件本身。
在下一个流中,不要使用“文件到字节数组”转换器,只需使用“对象到字节数组”转换器即可。终于成功了。

@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
注意:
在调用资源方法之前,请花时间创建这两个文件(根据文件大小)

如果您想直接使用
MIMExx.tmp
文件,那么我认为没有一种简单的方法可以使用
JAVA反射来获取临时文件路径并使用它

使用Java反射:

@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);
}

“文件本身”是什么意思?你到底想做什么?流程是在mule studio中设计的。组件“文件到字节数组转换器”需要转换文件,为此,我们需要通过上面的类发送文件-通过客户端接受。我认为如果要将文件对象返回到另一个“文件到字节数组转换器”您必须将输入流写入一个文件,然后发送该文件。
@raviKumar
查看我的答案这可能会对您有所帮助!